Files
yt-dlp-webui/server/rest/handlers.go
Marco 8327d1e94c Download REST API endpoints (#72)
* backend and frontend hotfixes, see message

Improved rendering on the frontend by cutting unecessary useStates.
Backend side, downloads now auto resume even on application kill.

* download rest api endpoints, general code refactor

* download request json mappings
2023-07-31 08:30:09 +02:00

57 lines
1.2 KiB
Go

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