From dc32e64190cd24e396e6fb0d9b807702b8919732 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Fri, 30 Oct 2020 18:16:21 +0100 Subject: [PATCH] api manager. --- internal/api/member/handler.go | 32 +++++++++++++++++ internal/api/room/handler.go | 45 ++++++++++++++++++++++++ internal/api/room/resolution.go | 60 +++++++++++++++++++++++++++++++ internal/api/router.go | 62 +++++++++++++++++---------------- internal/http/http.go | 11 ++++-- neko.go | 8 ++++- 6 files changed, 185 insertions(+), 33 deletions(-) create mode 100644 internal/api/member/handler.go create mode 100644 internal/api/room/handler.go diff --git a/internal/api/member/handler.go b/internal/api/member/handler.go new file mode 100644 index 00000000..088212db --- /dev/null +++ b/internal/api/member/handler.go @@ -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 +} diff --git a/internal/api/room/handler.go b/internal/api/room/handler.go new file mode 100644 index 00000000..2ec948a6 --- /dev/null +++ b/internal/api/room/handler.go @@ -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 +} diff --git a/internal/api/room/resolution.go b/internal/api/room/resolution.go index 01912655..138e91fd 100644 --- a/internal/api/room/resolution.go +++ b/internal/api/room/resolution.go @@ -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.", + }) +} diff --git a/internal/api/router.go b/internal/api/router.go index b03f1a8d..c7e0285c 100644 --- a/internal/api/router.go +++ b/internal/api/router.go @@ -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()) } diff --git a/internal/http/http.go b/internal/http/http.go index 973405aa..cab997d1 100644 --- a/internal/http/http.go +++ b/internal/http/http.go @@ -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 { diff --git a/neko.go b/neko.go index be46757b..6c631dee 100644 --- a/neko.go +++ b/neko.go @@ -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