From b197bb303e135d3bb5294b4dc053a76ee1d2b97f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Sat, 12 Dec 2020 18:37:19 +0100 Subject: [PATCH] add support for modules. --- cmd/serve.go | 5 +++-- internal/types/api.go | 1 + internal/types/websocket.go | 3 +++ internal/websocket/manager.go | 8 +++----- neko.go | 13 +++++++++++++ 5 files changed, 23 insertions(+), 7 deletions(-) diff --git a/cmd/serve.go b/cmd/serve.go index 09352739..225d9c77 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -5,6 +5,7 @@ import ( "github.com/spf13/cobra" "demodesk/neko" + "demodesk/neko/modules" "demodesk/neko/internal/config" ) @@ -16,13 +17,13 @@ func init() { Run: neko.Service.ServeCommand, } - configs := []config.Config{ + configs := append([]config.Config{ neko.Service.Configs.Desktop, neko.Service.Configs.Capture, neko.Service.Configs.WebRTC, neko.Service.Configs.Session, neko.Service.Configs.Server, - } + }, modules.ConfigsList()...) cobra.OnInitialize(func() { for _, cfg := range configs { diff --git a/internal/types/api.go b/internal/types/api.go index cd821453..16571b91 100644 --- a/internal/types/api.go +++ b/internal/types/api.go @@ -6,4 +6,5 @@ import ( type ApiManager interface { Route(r chi.Router) + AddRouter(path string, router func(chi.Router)) } diff --git a/internal/types/websocket.go b/internal/types/websocket.go index a55a9ee5..836e3d31 100644 --- a/internal/types/websocket.go +++ b/internal/types/websocket.go @@ -2,6 +2,8 @@ package types import "net/http" +type HandlerFunction func(Session, []byte) bool + type WebSocketPeer interface { Send(v interface{}) error Destroy() error @@ -10,5 +12,6 @@ type WebSocketPeer interface { type WebSocketManager interface { Start() Shutdown() error + AddHandler(handler HandlerFunction) Upgrade(w http.ResponseWriter, r *http.Request) error } diff --git a/internal/websocket/manager.go b/internal/websocket/manager.go index 8aa3faa8..b4133538 100644 --- a/internal/websocket/manager.go +++ b/internal/websocket/manager.go @@ -15,8 +15,6 @@ import ( "demodesk/neko/internal/types" ) -type HandlerFunction func(types.Session, []byte) bool - func New( sessions types.SessionManager, desktop types.DesktopManager, @@ -35,7 +33,7 @@ func New( }, }, handler: handler.New(sessions, desktop, capture, webrtc), - handlers: []HandlerFunction{}, + handlers: []types.HandlerFunction{}, } } @@ -48,7 +46,7 @@ type WebSocketManagerCtx struct { sessions types.SessionManager desktop types.DesktopManager handler *handler.MessageHandlerCtx - handlers []HandlerFunction + handlers []types.HandlerFunction shutdown chan bool } @@ -149,7 +147,7 @@ func (ws *WebSocketManagerCtx) Shutdown() error { return nil } -func (ws *WebSocketManagerCtx) AddHandler(handler HandlerFunction) { +func (ws *WebSocketManagerCtx) AddHandler(handler types.HandlerFunction) { ws.handlers = append(ws.handlers, handler) } diff --git a/neko.go b/neko.go index c77537a8..12815f47 100644 --- a/neko.go +++ b/neko.go @@ -14,6 +14,7 @@ import ( "demodesk/neko/internal/websocket" "demodesk/neko/internal/api" "demodesk/neko/internal/http" + "demodesk/neko/modules" "github.com/rs/zerolog" "github.com/rs/zerolog/log" @@ -169,6 +170,12 @@ func (neko *Neko) Start() { neko.Configs.Server, ) + modules.Start( + neko.sessionManager, + neko.webSocketManager, + neko.apiManager, + ) + neko.httpManager = http.New( neko.webSocketManager, neko.apiManager, @@ -208,6 +215,12 @@ func (neko *Neko) Shutdown() { neko.logger.Debug().Msg("websocket manager shutdown") } + if err := modules.Shutdown(); err != nil { + neko.logger.Err(err).Msg("modules shutdown with an error") + } else { + neko.logger.Debug().Msg("modules shutdown") + } + if err := neko.httpManager.Shutdown(); err != nil { neko.logger.Err(err).Msg("http manager shutdown with an error") } else {