-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.go
More file actions
136 lines (126 loc) · 3.26 KB
/
Copy pathapp.go
File metadata and controls
136 lines (126 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
import (
"bytes"
"encoding/json"
"fmt"
"github.com/pion/webrtc/v3"
"io"
"net/http"
"os"
"os/signal"
"codeberg.org/greatape/goldgorilla/controllers"
"codeberg.org/greatape/goldgorilla/models"
"codeberg.org/greatape/goldgorilla/repositories"
"codeberg.org/greatape/goldgorilla/routers"
"syscall"
"time"
)
type App struct {
conf *models.ConfigModel
router *routers.Router
src string
}
func (a *App) Init(srcListenAddr string, logjamBaseUrl string, iceTCPMUXListenPort uint, customICEHostCandidateIP string) {
println("initializing ..")
a.src = srcListenAddr
var iceServers []webrtc.ICEServer
iceconfjson, err := os.ReadFile("./ice.servers.json")
if err != nil {
println("[E] error reading ice.servers.json: " + err.Error())
} else {
err = json.Unmarshal(iceconfjson, &iceServers)
if err != nil {
panic("[E] can't parse ice.servers.json: " + err.Error())
}
}
startRejoinCH := make(chan models.RejoinMode, 2)
a.conf = &models.ConfigModel{
LogjamBaseUrl: logjamBaseUrl,
ICEServers: iceServers,
ICETCPMUXListenPort: iceTCPMUXListenPort,
CustomICEHostCandidateIP: customICEHostCandidateIP,
StartRejoinCH: &startRejoinCH,
}
roomRepo := repositories.NewRoomRepository(a.conf)
a.router = &routers.Router{}
respHelper := controllers.NewResponseHelper()
roomCtrl := controllers.NewRoomController(respHelper, roomRepo, a.conf)
err = a.router.RegisterRoutes(roomCtrl)
panicIfErr(err)
{
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL, syscall.SIGILL)
go func() {
a.onDie(<-sigs)
}()
}
}
func (a *App) Run() {
go func() {
//*a.conf.StartRejoinCH <- true
c := &http.Client{
Timeout: 8 * time.Second,
}
for data := range *a.conf.StartRejoinCH {
if data.SimplyJoin {
buffer, _ := json.Marshal(map[string]any{"roomId": data.RoomId})
body := bytes.NewReader(buffer)
res, err := c.Post(a.conf.LogjamBaseUrl+"/join", "application/json", body)
if err != nil {
println(err.Error())
time.Sleep(4 * time.Second)
*a.conf.StartRejoinCH <- data
continue
}
if res.StatusCode > 204 {
resbody, _ := io.ReadAll(res.Body)
println("get /join "+res.Status, string(resbody))
time.Sleep(4 * time.Second)
*a.conf.StartRejoinCH <- data
continue
}
} else {
data.SimplyJoin = true
reqModel := struct {
RoomId string `json:"roomId"`
}{
RoomId: data.RoomId,
}
serializedReqBody, err := json.Marshal(reqModel)
if err != nil {
println(err.Error())
*a.conf.StartRejoinCH <- data
continue
}
resp, err := c.Post(a.conf.LogjamBaseUrl+"/rejoin", "application/json", bytes.NewReader(serializedReqBody))
if err != nil {
println(err.Error())
continue
}
if resp.StatusCode > 204 {
println("/rejoin", resp.Status)
}
/*if err != nil {
println(err.Error())
*a.conf.StartRejoinCH <- data
continue
}
if resp.StatusCode > 204 {
println("/rejoin", resp.Status)
*a.conf.StartRejoinCH <- data
}*/
}
}
}()
err := a.router.Serve(a.src)
panicIfErr(err)
}
func (a *App) onDie(sig os.Signal) {
fmt.Println("<-", sig)
os.Exit(0)
}
func panicIfErr(err error) {
if err != nil {
panic(err)
}
}