api manager.

This commit is contained in:
Miroslav Šedivý 2020-10-30 18:16:21 +01:00
parent dabd7e368e
commit dc32e64190
6 changed files with 185 additions and 33 deletions

View File

@ -0,0 +1,32 @@
package member
import (
"github.com/go-chi/chi"
"demodesk/neko/internal/types"
)
type MemberHandler struct {
sessions types.SessionManager
websocket types.WebSocketHandler
}
func New(
sessions types.SessionManager,
websocket types.WebSocketHandler,
) *MemberHandler {
// Init
return &MemberHandler{
sessions: sessions,
websocket: websocket,
}
}
func (h *MemberHandler) Router() *chi.Mux {
r := chi.NewRouter()
// TODO
return r
}

View File

@ -0,0 +1,45 @@
package room
import (
"github.com/go-chi/chi"
"demodesk/neko/internal/types"
)
type RoomHandler struct {
sessions types.SessionManager
remote types.RemoteManager
broadcast types.BroadcastManager
websocket types.WebSocketHandler
}
func New(
sessions types.SessionManager,
remote types.RemoteManager,
broadcast types.BroadcastManager,
websocket types.WebSocketHandler,
) *RoomHandler {
// Init
return &RoomHandler{
sessions: sessions,
remote: remote,
broadcast: broadcast,
websocket: websocket,
}
}
func (h *RoomHandler) Router() *chi.Mux {
r := chi.NewRouter()
r.Route("/resolution", func(r chi.Router) {
r.Get("/", h.ResolutionGet)
r.Post("/", h.ResolutionChange)
r.Get("/list", h.ResolutionList)
})
// TODO
return r
}

View File

@ -1 +1,61 @@
package room
import (
"net/http"
"github.com/go-chi/render"
)
type ErrResponse struct {
Code int `json:"code"`
Message string `json:"message"`
}
type ResolutionStruct struct {
Width int `json:"width"`
Height int `json:"height"`
Rate int `json:"rate"`
}
func (h *RoomHandler) ResolutionGet(w http.ResponseWriter, r *http.Request) {
size := h.remote.GetScreenSize()
if size == nil {
w.WriteHeader(http.StatusInternalServerError)
render.JSON(w, r, ErrResponse{
Code: -1,
Message: "Unable to get current screen resolution.",
})
return
}
render.JSON(w, r, ResolutionStruct{
Width: size.Width,
Height: size.Height,
Rate: int(size.Rate),
})
}
func (h *RoomHandler) ResolutionChange(w http.ResponseWriter, r *http.Request) {
// data := &ResolutionStruct{}
// if err := render.Bind(r, data); err != nil {
// render.JSON(w, r, ErrResponse{
// Code: -1,
// Message: "Invalid Request.",
// })
// return
// }
render.JSON(w, r, ErrResponse{
Code: -1,
Message: "Not implmented.",
})
}
func (h *RoomHandler) ResolutionList(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
render.JSON(w, r, ErrResponse{
Code: -1,
Message: "Not implmented.",
})
}

View File

@ -1,40 +1,42 @@
package api
import (
"net/http"
"github.com/go-chi/chi"
// "demodesk/neko/internal/api/member"
// "demodesk/neko/internal/api/room"
"demodesk/neko/internal/api/member"
"demodesk/neko/internal/api/room"
"demodesk/neko/internal/types"
)
func Mount(router *chi.Mux) {
type API struct {
sessions types.SessionManager
remote types.RemoteManager
broadcast types.BroadcastManager
websocket types.WebSocketHandler
}
func New(
sessions types.SessionManager,
remote types.RemoteManager,
broadcast types.BroadcastManager,
websocket types.WebSocketHandler,
) *API {
// Init
return &API{
sessions: sessions,
remote: remote,
broadcast: broadcast,
websocket: websocket,
}
}
func (a *API) Mount(router *chi.Mux) {
// all member routes
router.Mount("/member", MemberRoutes())
memberHandler := member.New(a.sessions, a.websocket)
router.Mount("/member", memberHandler.Router())
// all room routes
router.Mount("/room", RoomRoutes())
}
func MemberRoutes() *chi.Mux {
router := chi.NewRouter()
router.Get("/test", func(w http.ResponseWriter, r *http.Request) {
//nolint
w.Write([]byte("hello world"))
})
return router
}
func RoomRoutes() *chi.Mux {
router := chi.NewRouter()
router.Get("/test", func(w http.ResponseWriter, r *http.Request) {
//nolint
w.Write([]byte("hello world"))
})
return router
// get room routes
roomHandler := room.New(a.sessions, a.remote, a.broadcast, a.websocket)
router.Mount("/room", roomHandler.Router())
}

View File

@ -24,7 +24,13 @@ type Server struct {
conf *config.Server
}
func New(conf *config.Server, webSocketHandler types.WebSocketHandler) *Server {
func New(
sessions types.SessionManager,
remote types.RemoteManager,
broadcast types.BroadcastManager,
webSocketHandler types.WebSocketHandler,
conf *config.Server,
) *Server {
logger := log.With().Str("module", "http").Logger()
router := chi.NewRouter()
@ -33,7 +39,8 @@ func New(conf *config.Server, webSocketHandler types.WebSocketHandler) *Server {
router.Use(Logger) // Log API request calls using custom logger function
// Mount REST API
api.Mount(router)
apiManager := api.New(sessions, remote, broadcast, webSocketHandler)
apiManager.Mount(router)
router.Get("/ws", func(w http.ResponseWriter, r *http.Request) {
if webSocketHandler.Upgrade(w, r) != nil {

View File

@ -133,7 +133,13 @@ func (neko *Neko) Start() {
webSocketHandler := websocket.New(sessionManager, remoteManager, broadcastManager, webRTCManager, neko.WebSocket)
webSocketHandler.Start()
server := http.New(neko.Server, webSocketHandler)
server := http.New(
sessionManager,
remoteManager,
broadcastManager,
webSocketHandler,
neko.Server,
)
server.Start()
neko.sessionManager = sessionManager