2023-07-26 21:15:39 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2023-07-31 12:27:40 +00:00
|
|
|
"fmt"
|
2023-08-24 13:40:18 +00:00
|
|
|
"github.com/pion/webrtc/v3"
|
2023-07-31 12:27:40 +00:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
2023-07-26 21:15:39 +00:00
|
|
|
"sourcecode.social/greatape/goldgorilla/controllers"
|
|
|
|
"sourcecode.social/greatape/goldgorilla/models"
|
|
|
|
"sourcecode.social/greatape/goldgorilla/repositories"
|
|
|
|
"sourcecode.social/greatape/goldgorilla/routers"
|
2023-07-31 12:27:40 +00:00
|
|
|
"syscall"
|
2023-07-26 21:15:39 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type App struct {
|
|
|
|
conf *models.ConfigModel
|
|
|
|
router *routers.Router
|
|
|
|
src string
|
|
|
|
}
|
|
|
|
|
2023-09-21 14:37:05 +00:00
|
|
|
func (a *App) Init(srcListenAddr string, svcAddr string, logjamBaseUrl string, targetRoom string, iceTCPMUXListenPort uint, customICEHostCandidateIP string) {
|
2023-07-31 12:27:40 +00:00
|
|
|
println("initializing ..")
|
2023-07-26 21:15:39 +00:00
|
|
|
a.src = srcListenAddr
|
2023-08-24 13:40:18 +00:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|
2023-07-26 21:15:39 +00:00
|
|
|
a.conf = &models.ConfigModel{
|
2023-09-21 14:37:05 +00:00
|
|
|
LogjamBaseUrl: logjamBaseUrl + "/auxiliary-node",
|
|
|
|
TargetRoom: targetRoom,
|
|
|
|
ServiceAddress: svcAddr,
|
|
|
|
ICEServers: iceServers,
|
|
|
|
ICETCPMUXListenPort: iceTCPMUXListenPort,
|
|
|
|
CustomICEHostCandidateIP: customICEHostCandidateIP,
|
2023-07-26 21:15:39 +00:00
|
|
|
}
|
|
|
|
roomRepo := repositories.NewRoomRepository(a.conf)
|
|
|
|
a.router = &routers.Router{}
|
|
|
|
respHelper := controllers.NewResponseHelper()
|
|
|
|
roomCtrl := controllers.NewRoomController(respHelper, roomRepo, a.conf)
|
|
|
|
|
2023-08-24 13:40:18 +00:00
|
|
|
err = a.router.RegisterRoutes(roomCtrl)
|
2023-07-26 21:15:39 +00:00
|
|
|
panicIfErr(err)
|
2023-07-31 12:27:40 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
sigs := make(chan os.Signal, 1)
|
2023-08-07 12:10:29 +00:00
|
|
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL, syscall.SIGILL)
|
2023-07-31 12:27:40 +00:00
|
|
|
go func() {
|
|
|
|
a.onDie(<-sigs)
|
|
|
|
}()
|
|
|
|
}
|
2023-07-26 21:15:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) Run() {
|
|
|
|
go func() {
|
|
|
|
start:
|
2023-07-31 12:27:40 +00:00
|
|
|
buffer, _ := json.Marshal(map[string]any{"roomId": a.conf.TargetRoom, "svcAddr": a.conf.ServiceAddress})
|
2023-07-26 21:15:39 +00:00
|
|
|
body := bytes.NewReader(buffer)
|
2023-07-31 12:27:40 +00:00
|
|
|
c := &http.Client{
|
|
|
|
Timeout: 8 * time.Second,
|
|
|
|
}
|
|
|
|
res, err := c.Post(a.conf.LogjamBaseUrl+"/join", "application/json", body)
|
2023-07-26 21:15:39 +00:00
|
|
|
if err != nil {
|
|
|
|
println(err.Error())
|
|
|
|
time.Sleep(4 * time.Second)
|
|
|
|
goto start
|
|
|
|
}
|
|
|
|
if res.StatusCode > 204 {
|
|
|
|
resbody, _ := io.ReadAll(res.Body)
|
|
|
|
println("get /join "+res.Status, string(resbody))
|
|
|
|
time.Sleep(4 * time.Second)
|
|
|
|
goto start
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
err := a.router.Serve(a.src)
|
|
|
|
panicIfErr(err)
|
|
|
|
}
|
|
|
|
|
2023-07-31 12:27:40 +00:00
|
|
|
func (a *App) onDie(sig os.Signal) {
|
|
|
|
fmt.Println("<-", sig)
|
|
|
|
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2023-07-26 21:15:39 +00:00
|
|
|
func panicIfErr(err error) {
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|