mirror of
https://github.com/m1k1o/neko.git
synced 2024-07-24 14:40:50 +12:00
api manager.
This commit is contained in:
parent
dabd7e368e
commit
dc32e64190
32
internal/api/member/handler.go
Normal file
32
internal/api/member/handler.go
Normal file
@ -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
|
||||||
|
}
|
45
internal/api/room/handler.go
Normal file
45
internal/api/room/handler.go
Normal file
@ -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
|
||||||
|
}
|
@ -1 +1,61 @@
|
|||||||
package room
|
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.",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
@ -1,40 +1,42 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/go-chi/chi"
|
"github.com/go-chi/chi"
|
||||||
|
|
||||||
// "demodesk/neko/internal/api/member"
|
"demodesk/neko/internal/api/member"
|
||||||
// "demodesk/neko/internal/api/room"
|
"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
|
// all member routes
|
||||||
router.Mount("/member", MemberRoutes())
|
memberHandler := member.New(a.sessions, a.websocket)
|
||||||
|
router.Mount("/member", memberHandler.Router())
|
||||||
|
|
||||||
// all room routes
|
// get room routes
|
||||||
router.Mount("/room", RoomRoutes())
|
roomHandler := room.New(a.sessions, a.remote, a.broadcast, a.websocket)
|
||||||
}
|
router.Mount("/room", roomHandler.Router())
|
||||||
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,13 @@ type Server struct {
|
|||||||
conf *config.Server
|
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()
|
logger := log.With().Str("module", "http").Logger()
|
||||||
|
|
||||||
router := chi.NewRouter()
|
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
|
router.Use(Logger) // Log API request calls using custom logger function
|
||||||
|
|
||||||
// Mount REST API
|
// 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) {
|
router.Get("/ws", func(w http.ResponseWriter, r *http.Request) {
|
||||||
if webSocketHandler.Upgrade(w, r) != nil {
|
if webSocketHandler.Upgrade(w, r) != nil {
|
||||||
|
8
neko.go
8
neko.go
@ -133,7 +133,13 @@ func (neko *Neko) Start() {
|
|||||||
webSocketHandler := websocket.New(sessionManager, remoteManager, broadcastManager, webRTCManager, neko.WebSocket)
|
webSocketHandler := websocket.New(sessionManager, remoteManager, broadcastManager, webRTCManager, neko.WebSocket)
|
||||||
webSocketHandler.Start()
|
webSocketHandler.Start()
|
||||||
|
|
||||||
server := http.New(neko.Server, webSocketHandler)
|
server := http.New(
|
||||||
|
sessionManager,
|
||||||
|
remoteManager,
|
||||||
|
broadcastManager,
|
||||||
|
webSocketHandler,
|
||||||
|
neko.Server,
|
||||||
|
)
|
||||||
server.Start()
|
server.Start()
|
||||||
|
|
||||||
neko.sessionManager = sessionManager
|
neko.sessionManager = sessionManager
|
||||||
|
Loading…
Reference in New Issue
Block a user