port new API back to refactor.

This commit is contained in:
Miroslav Šedivý 2020-11-01 16:54:06 +01:00
parent 9559edf4d4
commit 507fce7862
8 changed files with 53 additions and 45 deletions

View File

@ -9,18 +9,15 @@ import (
type MemberHandler struct { type MemberHandler struct {
sessions types.SessionManager sessions types.SessionManager
websocket types.WebSocketHandler
} }
func New( func New(
sessions types.SessionManager, sessions types.SessionManager,
websocket types.WebSocketHandler,
) *MemberHandler { ) *MemberHandler {
// Init // Init
return &MemberHandler{ return &MemberHandler{
sessions: sessions, sessions: sessions,
websocket: websocket,
} }
} }

View File

@ -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) { func (h *RoomHandler) ClipboardRead(w http.ResponseWriter, r *http.Request) {
// TODO: error check? // TODO: error check?
text := h.remote.ReadClipboard() text := h.desktop.ReadClipboard()
render.JSON(w, r, ClipboardData{ render.JSON(w, r, ClipboardData{
Text: text, Text: text,
@ -41,7 +41,7 @@ func (h *RoomHandler) ClipboardWrite(w http.ResponseWriter, r *http.Request) {
} }
// TODO: error check? // TODO: error check?
h.remote.WriteClipboard(data.Text) h.desktop.WriteClipboard(data.Text)
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
} }

View File

@ -9,24 +9,21 @@ import (
type RoomHandler struct { type RoomHandler struct {
sessions types.SessionManager sessions types.SessionManager
remote types.RemoteManager desktop types.DesktopManager
broadcast types.BroadcastManager capture types.CaptureManager
websocket types.WebSocketHandler
} }
func New( func New(
sessions types.SessionManager, sessions types.SessionManager,
remote types.RemoteManager, desktop types.DesktopManager,
broadcast types.BroadcastManager, capture types.CaptureManager,
websocket types.WebSocketHandler,
) *RoomHandler { ) *RoomHandler {
// Init // Init
return &RoomHandler{ return &RoomHandler{
sessions: sessions, sessions: sessions,
remote: remote, desktop: desktop,
broadcast: broadcast, capture: capture,
websocket: websocket,
} }
} }

View File

@ -6,7 +6,6 @@ import (
"github.com/go-chi/render" "github.com/go-chi/render"
"demodesk/neko/internal/api/utils" "demodesk/neko/internal/api/utils"
"demodesk/neko/internal/websocket/broadcast"
) )
type ScreenConfiguration struct { 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) { func (h *RoomHandler) ScreenConfiguration(w http.ResponseWriter, r *http.Request) {
size := h.remote.GetScreenSize() size := h.desktop.GetScreenSize()
if size == nil { if size == nil {
_ = render.Render(w, r, utils.ErrMessage(500, "Unable to get screen configuration.")) _ = 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 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)) _ = render.Render(w, r, utils.ErrUnprocessableEntity(err))
return return
} }
if err := broadcast.ScreenConfiguration(h.sessions, "-todo-session-id-", data.Width, data.Height, data.Rate); err != nil { // TODO: Broadcast change to all sessions.
_ = render.Render(w, r, utils.ErrInternalServer(err))
return
}
render.JSON(w, r, data) 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) { func (h *RoomHandler) ScreenConfigurationsList(w http.ResponseWriter, r *http.Request) {
list := []render.Renderer{} list := []render.Renderer{}
ScreenConfigurations := h.remote.ScreenConfigurations() ScreenConfigurations := h.desktop.ScreenConfigurations()
for _, size := range ScreenConfigurations { for _, size := range ScreenConfigurations {
for _, fps := range size.Rates { for _, fps := range size.Rates {
list = append(list, &ScreenConfiguration{ list = append(list, &ScreenConfiguration{

View File

@ -8,15 +8,14 @@ import (
"demodesk/neko/internal/api/member" "demodesk/neko/internal/api/member"
"demodesk/neko/internal/api/room" "demodesk/neko/internal/api/room"
"demodesk/neko/internal/types" "demodesk/neko/internal/types"
"demodesk/neko/internal/types/config" "demodesk/neko/internal/config"
"demodesk/neko/internal/api/utils" "demodesk/neko/internal/api/utils"
) )
type API struct { type ApiManagerCtx struct {
sessions types.SessionManager sessions types.SessionManager
remote types.RemoteManager desktop types.DesktopManager
broadcast types.BroadcastManager capture types.CaptureManager
websocket types.WebSocketHandler
} }
var AdminToken []byte var AdminToken []byte
@ -24,27 +23,25 @@ var UserToken []byte
func New( func New(
sessions types.SessionManager, sessions types.SessionManager,
remote types.RemoteManager, desktop types.DesktopManager,
broadcast types.BroadcastManager, capture types.CaptureManager,
websocket types.WebSocketHandler,
conf *config.Server, conf *config.Server,
) *API { ) *ApiManagerCtx {
AdminToken = []byte(conf.AdminToken) AdminToken = []byte(conf.AdminToken)
UserToken = []byte(conf.UserToken) UserToken = []byte(conf.UserToken)
return &API{ return &ApiManagerCtx{
sessions: sessions, sessions: sessions,
remote: remote, desktop: desktop,
broadcast: broadcast, capture: capture,
websocket: websocket,
} }
} }
func (a *API) Mount(r *chi.Mux) { func (a *ApiManagerCtx) Mount(r *chi.Mux) {
memberHandler := member.New(a.sessions, a.websocket) memberHandler := member.New(a.sessions)
r.Mount("/member", memberHandler.Router(UsersOnly, AdminsOnly)) 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)) r.Mount("/room", roomHandler.Router(UsersOnly, AdminsOnly))
} }

View File

@ -23,7 +23,7 @@ type ServerCtx struct {
conf *config.Server 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() logger := log.With().Str("module", "http").Logger()
router := chi.NewRouter() 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(middleware.RequestID) // Create a request ID for each request
router.Use(Logger) // Log API request calls using custom logger function router.Use(Logger) // Log API request calls using custom logger function
ApiManager.Mount(router)
router.Get("/ws", func(w http.ResponseWriter, r *http.Request) { router.Get("/ws", func(w http.ResponseWriter, r *http.Request) {
if webSocketHandler.Upgrade(w, r) != nil { if WebSocketManager.Upgrade(w, r) != nil {
//nolint //nolint
w.Write([]byte("unable to upgrade your connection to a websocket")) w.Write([]byte("unable to upgrade your connection to a websocket"))
} }

9
internal/types/api.go Normal file
View File

@ -0,0 +1,9 @@
package types
import (
"github.com/go-chi/chi"
)
type ApiManager interface {
Mount(r *chi.Mux)
}

10
neko.go
View File

@ -12,6 +12,7 @@ import (
"demodesk/neko/internal/webrtc" "demodesk/neko/internal/webrtc"
"demodesk/neko/internal/session" "demodesk/neko/internal/session"
"demodesk/neko/internal/websocket" "demodesk/neko/internal/websocket"
"demodesk/neko/internal/api"
"demodesk/neko/internal/http" "demodesk/neko/internal/http"
"github.com/rs/zerolog" "github.com/rs/zerolog"
@ -116,6 +117,7 @@ type Neko struct {
webRTCManager *webrtc.WebRTCManagerCtx webRTCManager *webrtc.WebRTCManagerCtx
sessionManager *session.SessionManagerCtx sessionManager *session.SessionManagerCtx
webSocketManager *websocket.WebSocketManagerCtx webSocketManager *websocket.WebSocketManagerCtx
apiManager *api.ApiManagerCtx
server *http.ServerCtx server *http.ServerCtx
} }
@ -155,8 +157,16 @@ func (neko *Neko) Start() {
) )
neko.webSocketManager.Start() neko.webSocketManager.Start()
neko.apiManager = api.New(
neko.sessionManager,
neko.desktopManager,
neko.captureManager,
neko.Configs.Server,
)
neko.server = http.New( neko.server = http.New(
neko.webSocketManager, neko.webSocketManager,
neko.apiManager,
neko.Configs.Server, neko.Configs.Server,
) )
neko.server.Start() neko.server.Start()