mirror of
https://github.com/m1k1o/neko.git
synced 2024-07-24 14:40:50 +12:00
use websocket authentication & refactor.
This commit is contained in:
@ -3,45 +3,29 @@ package room
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/render"
|
||||
|
||||
"demodesk/neko/internal/api/utils"
|
||||
"demodesk/neko/internal/utils"
|
||||
)
|
||||
|
||||
type ClipboardData struct {
|
||||
Text string `json:"text"`
|
||||
}
|
||||
|
||||
func (a *ClipboardData) Bind(r *http.Request) error {
|
||||
// Bind will run after the unmarshalling is complete, its a
|
||||
// good time to focus some post-processing after a decoding.
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *ClipboardData) Render(w http.ResponseWriter, r *http.Request) error {
|
||||
// Pre-processing before a response is marshalled and sent
|
||||
// across the wire
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *RoomHandler) ClipboardRead(w http.ResponseWriter, r *http.Request) {
|
||||
// TODO: error check?
|
||||
text := h.desktop.ReadClipboard()
|
||||
|
||||
render.JSON(w, r, ClipboardData{
|
||||
utils.HttpSuccess(w, ClipboardData{
|
||||
Text: text,
|
||||
})
|
||||
}
|
||||
|
||||
func (h *RoomHandler) ClipboardWrite(w http.ResponseWriter, r *http.Request) {
|
||||
data := &ClipboardData{}
|
||||
if err := render.Bind(r, data); err != nil {
|
||||
_ = render.Render(w, r, utils.ErrBadRequest(err))
|
||||
if !utils.HttpJsonRequest(w, r, data) {
|
||||
return
|
||||
}
|
||||
|
||||
// TODO: error check?
|
||||
h.desktop.WriteClipboard(data.Text)
|
||||
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
utils.HttpSuccess(w)
|
||||
}
|
||||
|
@ -3,7 +3,6 @@ package room
|
||||
import (
|
||||
"github.com/go-chi/chi"
|
||||
|
||||
"demodesk/neko/internal/api/utils"
|
||||
"demodesk/neko/internal/types"
|
||||
)
|
||||
|
||||
@ -27,20 +26,18 @@ func New(
|
||||
}
|
||||
}
|
||||
|
||||
func (h *RoomHandler) Router(
|
||||
usersOnly utils.HttpMiddleware,
|
||||
adminsOnly utils.HttpMiddleware,
|
||||
) *chi.Mux {
|
||||
func (h *RoomHandler) Router() *chi.Mux {
|
||||
r := chi.NewRouter()
|
||||
|
||||
|
||||
// TODO: Authorizaton.
|
||||
r.Route("/screen", func(r chi.Router) {
|
||||
r.With(usersOnly).Get("/", h.ScreenConfiguration)
|
||||
r.With(adminsOnly).Post("/", h.ScreenConfigurationChange)
|
||||
r.Get("/", h.ScreenConfiguration)
|
||||
r.Post("/", h.ScreenConfigurationChange)
|
||||
|
||||
r.With(adminsOnly).Get("/configurations", h.ScreenConfigurationsList)
|
||||
r.Get("/configurations", h.ScreenConfigurationsList)
|
||||
})
|
||||
|
||||
r.With(adminsOnly).Route("/clipboard", func(r chi.Router) {
|
||||
r.Route("/clipboard", func(r chi.Router) {
|
||||
r.Get("/", h.ClipboardRead)
|
||||
r.Post("/", h.ClipboardWrite)
|
||||
})
|
||||
|
@ -3,9 +3,7 @@ package room
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/render"
|
||||
|
||||
"demodesk/neko/internal/api/utils"
|
||||
"demodesk/neko/internal/utils"
|
||||
)
|
||||
|
||||
type ScreenConfiguration struct {
|
||||
@ -14,27 +12,15 @@ type ScreenConfiguration struct {
|
||||
Rate int `json:"rate"`
|
||||
}
|
||||
|
||||
func (a *ScreenConfiguration) Bind(r *http.Request) error {
|
||||
// Bind will run after the unmarshalling is complete, its a
|
||||
// good time to focus some post-processing after a decoding.
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *ScreenConfiguration) Render(w http.ResponseWriter, r *http.Request) error {
|
||||
// Pre-processing before a response is marshalled and sent
|
||||
// across the wire
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *RoomHandler) ScreenConfiguration(w http.ResponseWriter, r *http.Request) {
|
||||
size := h.desktop.GetScreenSize()
|
||||
|
||||
if size == nil {
|
||||
_ = render.Render(w, r, utils.ErrMessage(500, "Unable to get screen configuration."))
|
||||
utils.HttpInternalServer(w, "Unable to get screen configuration.")
|
||||
return
|
||||
}
|
||||
|
||||
render.JSON(w, r, ScreenConfiguration{
|
||||
utils.HttpSuccess(w, ScreenConfiguration{
|
||||
Width: size.Width,
|
||||
Height: size.Height,
|
||||
Rate: int(size.Rate),
|
||||
@ -43,28 +29,27 @@ func (h *RoomHandler) ScreenConfiguration(w http.ResponseWriter, r *http.Request
|
||||
|
||||
func (h *RoomHandler) ScreenConfigurationChange(w http.ResponseWriter, r *http.Request) {
|
||||
data := &ScreenConfiguration{}
|
||||
if err := render.Bind(r, data); err != nil {
|
||||
_ = render.Render(w, r, utils.ErrBadRequest(err))
|
||||
if !utils.HttpJsonRequest(w, r, data) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.desktop.ChangeScreenSize(data.Width, data.Height, data.Rate); err != nil {
|
||||
_ = render.Render(w, r, utils.ErrUnprocessableEntity(err))
|
||||
utils.HttpUnprocessableEntity(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
// TODO: Broadcast change to all sessions.
|
||||
|
||||
render.JSON(w, r, data)
|
||||
utils.HttpSuccess(w, data)
|
||||
}
|
||||
|
||||
func (h *RoomHandler) ScreenConfigurationsList(w http.ResponseWriter, r *http.Request) {
|
||||
list := []render.Renderer{}
|
||||
list := []ScreenConfiguration{}
|
||||
|
||||
ScreenConfigurations := h.desktop.ScreenConfigurations()
|
||||
for _, size := range ScreenConfigurations {
|
||||
for _, fps := range size.Rates {
|
||||
list = append(list, &ScreenConfiguration{
|
||||
list = append(list, ScreenConfiguration{
|
||||
Width: size.Width,
|
||||
Height: size.Height,
|
||||
Rate: int(fps),
|
||||
@ -72,5 +57,5 @@ func (h *RoomHandler) ScreenConfigurationsList(w http.ResponseWriter, r *http.Re
|
||||
}
|
||||
}
|
||||
|
||||
_ = render.RenderList(w, r, list)
|
||||
utils.HttpSuccess(w, list)
|
||||
}
|
||||
|
Reference in New Issue
Block a user