From 507fce7862f88a27d7d7f9d830f56426f1da04aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Sun, 1 Nov 2020 16:54:06 +0100 Subject: [PATCH] port new API back to refactor. --- internal/api/member/handler.go | 7 ++----- internal/api/room/clipboard.go | 4 ++-- internal/api/room/handler.go | 19 ++++++++----------- internal/api/room/screen.go | 12 ++++-------- internal/api/router.go | 31 ++++++++++++++----------------- internal/http/http.go | 6 ++++-- internal/types/api.go | 9 +++++++++ neko.go | 10 ++++++++++ 8 files changed, 53 insertions(+), 45 deletions(-) create mode 100644 internal/types/api.go diff --git a/internal/api/member/handler.go b/internal/api/member/handler.go index 1330bbe8..82383fed 100644 --- a/internal/api/member/handler.go +++ b/internal/api/member/handler.go @@ -8,19 +8,16 @@ import ( ) type MemberHandler struct { - sessions types.SessionManager - websocket types.WebSocketHandler + sessions types.SessionManager } func New( sessions types.SessionManager, - websocket types.WebSocketHandler, ) *MemberHandler { // Init return &MemberHandler{ - sessions: sessions, - websocket: websocket, + sessions: sessions, } } diff --git a/internal/api/room/clipboard.go b/internal/api/room/clipboard.go index 0cc4beb6..cef06f33 100644 --- a/internal/api/room/clipboard.go +++ b/internal/api/room/clipboard.go @@ -26,7 +26,7 @@ func (a *ClipboardData) Render(w http.ResponseWriter, r *http.Request) error { func (h *RoomHandler) ClipboardRead(w http.ResponseWriter, r *http.Request) { // TODO: error check? - text := h.remote.ReadClipboard() + text := h.desktop.ReadClipboard() render.JSON(w, r, ClipboardData{ Text: text, @@ -41,7 +41,7 @@ func (h *RoomHandler) ClipboardWrite(w http.ResponseWriter, r *http.Request) { } // TODO: error check? - h.remote.WriteClipboard(data.Text) + h.desktop.WriteClipboard(data.Text) w.WriteHeader(http.StatusNoContent) } diff --git a/internal/api/room/handler.go b/internal/api/room/handler.go index 2a3caa83..7be10bcc 100644 --- a/internal/api/room/handler.go +++ b/internal/api/room/handler.go @@ -8,25 +8,22 @@ import ( ) type RoomHandler struct { - sessions types.SessionManager - remote types.RemoteManager - broadcast types.BroadcastManager - websocket types.WebSocketHandler + sessions types.SessionManager + desktop types.DesktopManager + capture types.CaptureManager } func New( sessions types.SessionManager, - remote types.RemoteManager, - broadcast types.BroadcastManager, - websocket types.WebSocketHandler, + desktop types.DesktopManager, + capture types.CaptureManager, ) *RoomHandler { // Init return &RoomHandler{ - sessions: sessions, - remote: remote, - broadcast: broadcast, - websocket: websocket, + sessions: sessions, + desktop: desktop, + capture: capture, } } diff --git a/internal/api/room/screen.go b/internal/api/room/screen.go index aa29be0c..6f762d5e 100644 --- a/internal/api/room/screen.go +++ b/internal/api/room/screen.go @@ -6,7 +6,6 @@ import ( "github.com/go-chi/render" "demodesk/neko/internal/api/utils" - "demodesk/neko/internal/websocket/broadcast" ) type ScreenConfiguration struct { @@ -28,7 +27,7 @@ func (a *ScreenConfiguration) Render(w http.ResponseWriter, r *http.Request) err } func (h *RoomHandler) ScreenConfiguration(w http.ResponseWriter, r *http.Request) { - size := h.remote.GetScreenSize() + size := h.desktop.GetScreenSize() if size == nil { _ = render.Render(w, r, utils.ErrMessage(500, "Unable to get screen configuration.")) @@ -49,15 +48,12 @@ func (h *RoomHandler) ScreenConfigurationChange(w http.ResponseWriter, r *http.R return } - if err := h.remote.ChangeResolution(data.Width, data.Height, data.Rate); err != nil { + if err := h.capture.ChangeResolution(data.Width, data.Height, data.Rate); err != nil { _ = render.Render(w, r, utils.ErrUnprocessableEntity(err)) return } - if err := broadcast.ScreenConfiguration(h.sessions, "-todo-session-id-", data.Width, data.Height, data.Rate); err != nil { - _ = render.Render(w, r, utils.ErrInternalServer(err)) - return - } + // TODO: Broadcast change to all sessions. render.JSON(w, r, data) } @@ -65,7 +61,7 @@ func (h *RoomHandler) ScreenConfigurationChange(w http.ResponseWriter, r *http.R func (h *RoomHandler) ScreenConfigurationsList(w http.ResponseWriter, r *http.Request) { list := []render.Renderer{} - ScreenConfigurations := h.remote.ScreenConfigurations() + ScreenConfigurations := h.desktop.ScreenConfigurations() for _, size := range ScreenConfigurations { for _, fps := range size.Rates { list = append(list, &ScreenConfiguration{ diff --git a/internal/api/router.go b/internal/api/router.go index 551c1ba1..247144b9 100644 --- a/internal/api/router.go +++ b/internal/api/router.go @@ -8,15 +8,14 @@ import ( "demodesk/neko/internal/api/member" "demodesk/neko/internal/api/room" "demodesk/neko/internal/types" - "demodesk/neko/internal/types/config" + "demodesk/neko/internal/config" "demodesk/neko/internal/api/utils" ) -type API struct { - sessions types.SessionManager - remote types.RemoteManager - broadcast types.BroadcastManager - websocket types.WebSocketHandler +type ApiManagerCtx struct { + sessions types.SessionManager + desktop types.DesktopManager + capture types.CaptureManager } var AdminToken []byte @@ -24,27 +23,25 @@ var UserToken []byte func New( sessions types.SessionManager, - remote types.RemoteManager, - broadcast types.BroadcastManager, - websocket types.WebSocketHandler, + desktop types.DesktopManager, + capture types.CaptureManager, conf *config.Server, -) *API { +) *ApiManagerCtx { AdminToken = []byte(conf.AdminToken) UserToken = []byte(conf.UserToken) - return &API{ + return &ApiManagerCtx{ sessions: sessions, - remote: remote, - broadcast: broadcast, - websocket: websocket, + desktop: desktop, + capture: capture, } } -func (a *API) Mount(r *chi.Mux) { - memberHandler := member.New(a.sessions, a.websocket) +func (a *ApiManagerCtx) Mount(r *chi.Mux) { + memberHandler := member.New(a.sessions) r.Mount("/member", memberHandler.Router(UsersOnly, AdminsOnly)) - roomHandler := room.New(a.sessions, a.remote, a.broadcast, a.websocket) + roomHandler := room.New(a.sessions, a.desktop, a.capture) r.Mount("/room", roomHandler.Router(UsersOnly, AdminsOnly)) } diff --git a/internal/http/http.go b/internal/http/http.go index 22f1b83e..172ccffa 100644 --- a/internal/http/http.go +++ b/internal/http/http.go @@ -23,7 +23,7 @@ type ServerCtx struct { conf *config.Server } -func New(webSocketHandler types.WebSocketManager, conf *config.Server) *ServerCtx { +func New(WebSocketManager types.WebSocketManager, ApiManager types.ApiManager, conf *config.Server) *ServerCtx { logger := log.With().Str("module", "http").Logger() router := chi.NewRouter() @@ -31,8 +31,10 @@ func New(webSocketHandler types.WebSocketManager, conf *config.Server) *ServerCt router.Use(middleware.RequestID) // Create a request ID for each request router.Use(Logger) // Log API request calls using custom logger function + ApiManager.Mount(router) + router.Get("/ws", func(w http.ResponseWriter, r *http.Request) { - if webSocketHandler.Upgrade(w, r) != nil { + if WebSocketManager.Upgrade(w, r) != nil { //nolint w.Write([]byte("unable to upgrade your connection to a websocket")) } diff --git a/internal/types/api.go b/internal/types/api.go new file mode 100644 index 00000000..f0efae68 --- /dev/null +++ b/internal/types/api.go @@ -0,0 +1,9 @@ +package types + +import ( + "github.com/go-chi/chi" +) + +type ApiManager interface { + Mount(r *chi.Mux) +} diff --git a/neko.go b/neko.go index b845cc69..1ba5117b 100644 --- a/neko.go +++ b/neko.go @@ -12,6 +12,7 @@ import ( "demodesk/neko/internal/webrtc" "demodesk/neko/internal/session" "demodesk/neko/internal/websocket" + "demodesk/neko/internal/api" "demodesk/neko/internal/http" "github.com/rs/zerolog" @@ -116,6 +117,7 @@ type Neko struct { webRTCManager *webrtc.WebRTCManagerCtx sessionManager *session.SessionManagerCtx webSocketManager *websocket.WebSocketManagerCtx + apiManager *api.ApiManagerCtx server *http.ServerCtx } @@ -155,8 +157,16 @@ func (neko *Neko) Start() { ) neko.webSocketManager.Start() + neko.apiManager = api.New( + neko.sessionManager, + neko.desktopManager, + neko.captureManager, + neko.Configs.Server, + ) + neko.server = http.New( neko.webSocketManager, + neko.apiManager, neko.Configs.Server, ) neko.server.Start()