added livestream endpoints to REST API
This commit is contained in:
@@ -26,6 +26,8 @@ func ApplyRouter(args *ContainerArgs) func(chi.Router) {
|
|||||||
r.Use(openid.Middleware)
|
r.Use(openid.Middleware)
|
||||||
}
|
}
|
||||||
r.Post("/exec", h.Exec())
|
r.Post("/exec", h.Exec())
|
||||||
|
r.Post("/execPlaylist", h.ExecPlaylist())
|
||||||
|
r.Post("/execLivestream", h.ExecLivestream())
|
||||||
r.Get("/running", h.Running())
|
r.Get("/running", h.Running())
|
||||||
r.Get("/version", h.GetVersion())
|
r.Get("/version", h.GetVersion())
|
||||||
r.Get("/cookies", h.GetCookies())
|
r.Get("/cookies", h.GetCookies())
|
||||||
|
|||||||
@@ -41,6 +41,51 @@ func (h *Handler) Exec() http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *Handler) ExecPlaylist() 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)
|
||||||
|
}
|
||||||
|
|
||||||
|
err := h.service.ExecPlaylist(req)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := json.NewEncoder(w).Encode("ok"); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) ExecLivestream() 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)
|
||||||
|
}
|
||||||
|
|
||||||
|
h.service.ExecLivestream(req)
|
||||||
|
|
||||||
|
err := json.NewEncoder(w).Encode("ok")
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (h *Handler) Running() http.HandlerFunc {
|
func (h *Handler) Running() http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
defer r.Body.Close()
|
defer r.Body.Close()
|
||||||
|
|||||||
@@ -12,12 +12,14 @@ import (
|
|||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/marcopeocchi/yt-dlp-web-ui/server/config"
|
"github.com/marcopeocchi/yt-dlp-web-ui/server/config"
|
||||||
"github.com/marcopeocchi/yt-dlp-web-ui/server/internal"
|
"github.com/marcopeocchi/yt-dlp-web-ui/server/internal"
|
||||||
|
"github.com/marcopeocchi/yt-dlp-web-ui/server/internal/livestream"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Service struct {
|
type Service struct {
|
||||||
mdb *internal.MemoryDB
|
mdb *internal.MemoryDB
|
||||||
db *sql.DB
|
db *sql.DB
|
||||||
mq *internal.MessageQueue
|
mq *internal.MessageQueue
|
||||||
|
lm *livestream.Monitor
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) Exec(req internal.DownloadRequest) (string, error) {
|
func (s *Service) Exec(req internal.DownloadRequest) (string, error) {
|
||||||
@@ -36,10 +38,18 @@ func (s *Service) Exec(req internal.DownloadRequest) (string, error) {
|
|||||||
return id, nil
|
return id, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Service) ExecPlaylist(req internal.DownloadRequest) error {
|
||||||
|
return internal.PlaylistDetect(req, s.mq, s.mdb)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) ExecLivestream(req internal.DownloadRequest) {
|
||||||
|
s.lm.Add(req.URL)
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Service) Running(ctx context.Context) (*[]internal.ProcessResponse, error) {
|
func (s *Service) Running(ctx context.Context) (*[]internal.ProcessResponse, error) {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return nil, errors.New("context cancelled")
|
return nil, context.Canceled
|
||||||
default:
|
default:
|
||||||
return s.mdb.All(), nil
|
return s.mdb.All(), nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user