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 {
sessions types.SessionManager
websocket types.WebSocketHandler
}
func New(
sessions types.SessionManager,
websocket types.WebSocketHandler,
) *MemberHandler {
// Init
return &MemberHandler{
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) {
// 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)
}

View File

@ -9,24 +9,21 @@ import (
type RoomHandler struct {
sessions types.SessionManager
remote types.RemoteManager
broadcast types.BroadcastManager
websocket types.WebSocketHandler
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,
desktop: desktop,
capture: capture,
}
}

View File

@ -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{

View File

@ -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 {
type ApiManagerCtx struct {
sessions types.SessionManager
remote types.RemoteManager
broadcast types.BroadcastManager
websocket types.WebSocketHandler
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))
}

View File

@ -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"))
}

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/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()