diff --git a/internal/api/room/clipboard.go b/internal/api/room/clipboard.go index 01912655..aaf7d2b2 100644 --- a/internal/api/room/clipboard.go +++ b/internal/api/room/clipboard.go @@ -1 +1,47 @@ package room + +import ( + "net/http" + + "github.com/go-chi/render" + + "demodesk/neko/internal/api/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.remote.ReadClipboard() + + render.JSON(w, r, 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)) + return + } + + // TODO: error check? + h.remote.WriteClipboard(data.Text) + + w.WriteHeader(http.StatusNoContent) +} diff --git a/internal/api/room/handler.go b/internal/api/room/handler.go index 2dbbb455..2a3caa83 100644 --- a/internal/api/room/handler.go +++ b/internal/api/room/handler.go @@ -43,5 +43,10 @@ func (h *RoomHandler) Router( r.With(adminsOnly).Get("/configurations", h.ScreenConfigurationsList) }) + r.With(adminsOnly).Route("/clipboard", func(r chi.Router) { + r.Get("/", h.ClipboardRead) + r.Post("/", h.ClipboardWrite) + }) + return r }