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).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) } } }