package rest import ( "net/http" "github.com/goccy/go-json" "github.com/marcopeocchi/yt-dlp-web-ui/server/internal" ) type Handler struct { service *Service } 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") req := internal.DownloadRequest{} if err := json.NewDecoder(r.Body).DecodeContext(r.Context(), &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).EncodeContext(r.Context(), 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).EncodeContext(r.Context(), res) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } } 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).DecodeContext(r.Context(), 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).EncodeContext(r.Context(), "ok") if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } }