fix(controller): ResetRoom, feat: added Start handler
parent
5fadfbfb20
commit
8d2d26c14c
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1 +1,7 @@
|
|||
package controllers
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
func HealthCheck(ctx *gin.Context) {
|
||||
ctx.Status(204)
|
||||
}
|
||||
|
|
2
main.go
2
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")
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue