ace/servers/webserver/handlers.go

71 lines
1.3 KiB
Go
Raw Normal View History

2022-09-26 03:04:07 +00:00
package webserver
import (
"net/http"
"os"
"github.com/gin-gonic/gin"
"github.com/pion/webrtc/v3"
"github.com/sirupsen/logrus"
)
type Response struct {
Succeed bool `json:"succeed"`
Message string `json:"message"`
Data interface{} `json:"data"`
}
func (s *Server) getInstruction(c *gin.Context) {
f, _ := os.ReadFile(s.options.InstructionFile)
c.JSON(http.StatusOK, Response{
Succeed: true,
Message: "",
Data: string(f),
})
}
func (s *Server) exchangeSDP(c *gin.Context) {
2022-09-26 06:54:32 +00:00
offer := &webrtc.SessionDescription{}
2022-09-26 03:04:07 +00:00
if err := c.BindJSON(offer); err != nil {
c.JSON(http.StatusBadRequest, Response{
Succeed: false,
Message: "bad request",
Data: err.Error(),
})
logrus.Error(err)
return
}
2022-09-27 02:21:06 +00:00
answer, err := s.RTCConnector.Regist(offer)
2022-09-26 03:04:07 +00:00
if err != nil {
c.JSON(http.StatusInternalServerError, Response{
Succeed: false,
Message: "internal error",
Data: err.Error(),
})
return
}
c.JSON(http.StatusOK, Response{
Succeed: true,
Message: "",
Data: answer,
})
}
func (s *Server) getICEConfig(c *gin.Context) {
c.JSON(http.StatusOK, Response{
Succeed: true,
Message: "",
Data: s.options.WebRTC.STUNServers,
})
}
func (s *Server) getName(c *gin.Context) {
c.JSON(http.StatusOK, Response{
Succeed: true,
Message: "",
Data: s.options.Name,
})
}