From 8d2d26c14c337f611dc1b16ea3c1599fdeb46315 Mon Sep 17 00:00:00 2001 From: benyamin <43001019+benyamin218118@users.noreply.github.com> Date: Mon, 31 Jul 2023 23:40:39 +0330 Subject: [PATCH] fix(controller): ResetRoom, feat: added Start handler --- controllers/room.go | 26 ++++++++++++++++++++++++-- controllers/status.go | 6 ++++++ main.go | 2 +- repositories/room.go | 12 ++++++++---- routers/room.go | 3 +++ routers/router.go | 1 + 6 files changed, 43 insertions(+), 7 deletions(-) diff --git a/controllers/room.go b/controllers/room.go index 3d5b018..50fb18b 100644 --- a/controllers/room.go +++ b/controllers/room.go @@ -4,10 +4,12 @@ import ( "bytes" "encoding/json" "github.com/gin-gonic/gin" + "io" "net/http" "sourcecode.social/greatape/goldgorilla/models" "sourcecode.social/greatape/goldgorilla/models/dto" "sourcecode.social/greatape/goldgorilla/repositories" + "time" ) type RoomController struct { @@ -141,10 +143,16 @@ func (c *RoomController) ResetRoom(ctx *gin.Context) { } roomId := "" if rid, exists := reqModel["roomId"]; !exists { - roomId = rid c.helper.ResponseUnprocessableEntity(ctx) + return + } else { + if castedrid, stringItIs := rid.(string); stringItIs { + roomId = castedrid + } else { + c.helper.ResponseUnprocessableEntity(ctx) + return + } } - err := c.repo.ResetRoom(roomId) if c.helper.HandleIfErr(ctx, err, nil) { return @@ -152,3 +160,17 @@ func (c *RoomController) ResetRoom(ctx *gin.Context) { c.helper.Response(ctx, nil, http.StatusNoContent) } + +func (c *RoomController) Start(ctx *gin.Context) { + buffer, _ := json.Marshal(map[string]any{"roomId": c.conf.TargetRoom, "svcAddr": c.conf.ServiceAddress}) + body := bytes.NewReader(buffer) + res, err := http.Post(c.conf.LogjamBaseUrl+"/join", "application/json", body) + if err != nil { + println(err.Error()) + time.Sleep(4 * time.Second) + } + if res.StatusCode > 204 { + resbody, _ := io.ReadAll(res.Body) + println("get /join "+res.Status, string(resbody)) + } +} diff --git a/controllers/status.go b/controllers/status.go index 2d32936..b17c977 100644 --- a/controllers/status.go +++ b/controllers/status.go @@ -1 +1,7 @@ package controllers + +import "github.com/gin-gonic/gin" + +func HealthCheck(ctx *gin.Context) { + ctx.Status(204) +} diff --git a/main.go b/main.go index f40c3bc..1b3949e 100644 --- a/main.go +++ b/main.go @@ -6,7 +6,7 @@ import ( ) func main() { - svcAddr := flag.String("svc-addr", "", "service to register in logjam") + svcAddr := flag.String("svc-addr", "", "service baseurl to register in logjam ( shoudln't end with / )") src := flag.String("src", ":8080", "listenhost:listenPort") logjamBaseUrl := flag.String("logjam-base-url", "https://example.com", "logjam base url(shouldn't end with /)") targetRoom := flag.String("targetRoom", "testyroom", "target room") diff --git a/repositories/room.go b/repositories/room.go index 71a496b..124fc44 100644 --- a/repositories/room.go +++ b/repositories/room.go @@ -30,7 +30,7 @@ type Room struct { Peers map[uint64]*Peer trackLock *sync.Mutex Tracks map[string]*Track - timer *time.Timer + timer *time.Ticker } type RoomRepository struct { @@ -412,18 +412,22 @@ func (r *RoomRepository) ClosePeer(roomId string, id uint64) error { func (r *RoomRepository) ResetRoom(roomId string) error { r.Lock() if !r.doesRoomExists(roomId) { - return + r.Unlock() + return nil } - r.Unlock() room := r.Rooms[roomId] + r.Unlock() + room.Lock() - defer room.Unlock() + room.timer.Stop() for _, peer := range room.Peers { peer.Conn.Close() } + room.Unlock() r.Lock() delete(r.Rooms, roomId) r.Unlock() + return nil } diff --git a/routers/room.go b/routers/room.go index 980e713..348d0a7 100644 --- a/routers/room.go +++ b/routers/room.go @@ -9,9 +9,12 @@ func registerRoomRoutes(rg *gin.RouterGroup, ctrl *controllers.RoomController) { rg.POST("/peer", ctrl.CreatePeer) rg.DELETE("/peer", ctrl.ClosePeer) + rg.POST("/ice", ctrl.AddICECandidate) rg.POST("/answer", ctrl.Answer) rg.POST("/offer", ctrl.Offer) + + rg.POST("/", ctrl.Start) rg.DELETE("/", ctrl.ResetRoom) } diff --git a/routers/router.go b/routers/router.go index 4ab7413..172e1cc 100644 --- a/routers/router.go +++ b/routers/router.go @@ -13,6 +13,7 @@ func (r *Router) RegisterRoutes(rCtrl *controllers.RoomController) error { gin.SetMode(gin.ReleaseMode) r.router = gin.Default() registerRoomRoutes(r.router.Group("/room"), rCtrl) + r.router.GET("/healthcheck", controllers.HealthCheck) return nil }