package rest import ( "encoding/json" "net/http" "github.com/go-chi/chi/v5" "github.com/marcopeocchi/yt-dlp-web-ui/server/internal" ) type Handler struct { service *Service } /* REST version of the JSON-RPC interface */ func (h *Handler) Exec() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { defer r.Body.Close() w.Header().Set("Content-Type", "application/json") var req internal.DownloadRequest if err := json.NewDecoder(r.Body).Decode(&req); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } id, err := h.service.Exec(req) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } err = json.NewEncoder(w).Encode(id) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } } func (h *Handler) Running() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { defer r.Body.Close() w.Header().Set("Content-Type", "application/json") res, err := h.service.Running(r.Context()) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } err = json.NewEncoder(w).Encode(res) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } } func (h *Handler) GetCookies() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") cookies, err := h.service.GetCookies(r.Context()) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } res := &internal.SetCookiesRequest{ Cookies: string(cookies), } if err := json.NewEncoder(w).Encode(res); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } } } func (h *Handler) SetCookies() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { defer r.Body.Close() w.Header().Set("Content-Type", "application/json") req := new(internal.SetCookiesRequest) err := json.NewDecoder(r.Body).Decode(req) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } err = h.service.SetCookies(r.Context(), req.Cookies) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } err = json.NewEncoder(w).Encode("ok") if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } } func (h *Handler) DeleteCookies() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") err := h.service.SetCookies(r.Context(), "") if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } err = json.NewEncoder(w).Encode("ok") if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } } func (h *Handler) AddTemplate() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { defer r.Body.Close() w.Header().Set("Content-Type", "application/json") req := new(internal.CustomTemplate) err := json.NewDecoder(r.Body).Decode(req) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } if req.Name == "" || req.Content == "" { http.Error(w, "Invalid template", http.StatusBadRequest) return } err = h.service.SaveTemplate(r.Context(), req) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } err = json.NewEncoder(w).Encode("ok") if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } } func (h *Handler) GetTemplates() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { defer r.Body.Close() w.Header().Set("Content-Type", "application/json") templates, err := h.service.GetTemplates(r.Context()) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } err = json.NewEncoder(w).Encode(templates) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } } func (h *Handler) DeleteTemplate() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { defer r.Body.Close() w.Header().Set("Content-Type", "application/json") id := chi.URLParam(r, "id") err := h.service.DeleteTemplate(r.Context(), id) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } err = json.NewEncoder(w).Encode("ok") if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } } func (h *Handler) GetVersion() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { defer r.Body.Close() w.Header().Set("Content-Type", "application/json") version, err := h.service.GetVersion(r.Context()) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } if err := json.NewEncoder(w).Encode(version); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } }