66 lines
2.6 KiB
Go
66 lines
2.6 KiB
Go
package media
|
|
|
|
import (
|
|
"net/http"
|
|
"yovision.local/sense/app/sense/identity"
|
|
"yovision.local/sense/internal/platform"
|
|
)
|
|
|
|
type Module struct{ service *Service }
|
|
|
|
func NewModule(service *Service) *Module { return &Module{service: service} }
|
|
func (m *Module) Register(app *platform.App) {
|
|
setActiveService(m.service)
|
|
app.Handle("GET /api/v1/media/routes", identity.Require(identity.PermissionMediaRead, http.HandlerFunc(m.list)))
|
|
app.Handle("POST /api/v1/media/routes", identity.Require(identity.PermissionMediaWrite, http.HandlerFunc(m.configure)))
|
|
app.Handle("POST /api/v1/media/routes/{id}/reconcile", identity.Require(identity.PermissionMediaWrite, http.HandlerFunc(m.reconcile)))
|
|
app.Handle("POST /api/v1/media/routes/{id}/stop", identity.Require(identity.PermissionMediaWrite, http.HandlerFunc(m.stop)))
|
|
app.Handle("GET /api/v1/media/process", identity.Require(identity.PermissionMediaRead, http.HandlerFunc(m.process)))
|
|
}
|
|
func (m *Module) list(w http.ResponseWriter, r *http.Request) {
|
|
items, err := m.service.List(r.Context())
|
|
if err != nil {
|
|
platform.WriteError(w, err)
|
|
return
|
|
}
|
|
platform.WriteJSON(w, http.StatusOK, map[string]any{"items": items, "total": len(items)})
|
|
}
|
|
func (m *Module) configure(w http.ResponseWriter, r *http.Request) {
|
|
var req struct {
|
|
DeviceID string `json:"device_id"`
|
|
ProfileToken string `json:"profile_token"`
|
|
}
|
|
if err := platform.DecodeJSON(r, &req); err != nil {
|
|
platform.WriteError(w, err)
|
|
return
|
|
}
|
|
actor, _ := identity.PrincipalFromContext(r.Context())
|
|
item, err := m.service.Configure(r.Context(), actor, req.DeviceID, req.ProfileToken)
|
|
if err != nil {
|
|
platform.WriteError(w, &platform.APIError{Status: http.StatusBadRequest, Code: "media_invalid", Message: err.Error()})
|
|
return
|
|
}
|
|
platform.WriteJSON(w, http.StatusCreated, item)
|
|
}
|
|
func (m *Module) reconcile(w http.ResponseWriter, r *http.Request) {
|
|
actor, _ := identity.PrincipalFromContext(r.Context())
|
|
item, err := m.service.Reconcile(r.Context(), actor, r.PathValue("id"))
|
|
if err != nil {
|
|
platform.WriteError(w, &platform.APIError{Status: http.StatusBadRequest, Code: "media_reconcile_failed", Message: err.Error()})
|
|
return
|
|
}
|
|
platform.WriteJSON(w, http.StatusOK, item)
|
|
}
|
|
func (m *Module) stop(w http.ResponseWriter, r *http.Request) {
|
|
actor, _ := identity.PrincipalFromContext(r.Context())
|
|
item, err := m.service.Stop(r.Context(), actor, r.PathValue("id"))
|
|
if err != nil {
|
|
platform.WriteError(w, err)
|
|
return
|
|
}
|
|
platform.WriteJSON(w, http.StatusOK, item)
|
|
}
|
|
func (m *Module) process(w http.ResponseWriter, _ *http.Request) {
|
|
platform.WriteJSON(w, http.StatusOK, m.service.ProcessState())
|
|
}
|