Files
yt-dlp-webui/server/rest/handlers.go
Marco 8eb2831bc6 49 feat add cookies (#98)
* build client side validation and submission

* enabled cookies submission, bug fixes
2023-10-21 15:46:24 +02:00

84 lines
1.9 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).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)
}
}
}