templates editor #62
This commit is contained in:
@@ -1,26 +1,31 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/marcopeocchi/yt-dlp-web-ui/server/internal"
|
||||
middlewares "github.com/marcopeocchi/yt-dlp-web-ui/server/middleware"
|
||||
)
|
||||
|
||||
func Container(db *internal.MemoryDB, mq *internal.MessageQueue) *Handler {
|
||||
func Container(db *sql.DB, mdb *internal.MemoryDB, mq *internal.MessageQueue) *Handler {
|
||||
var (
|
||||
service = ProvideService(db, mq)
|
||||
service = ProvideService(db, mdb, mq)
|
||||
handler = ProvideHandler(service)
|
||||
)
|
||||
return handler
|
||||
}
|
||||
|
||||
func ApplyRouter(db *internal.MemoryDB, mq *internal.MessageQueue) func(chi.Router) {
|
||||
h := Container(db, mq)
|
||||
func ApplyRouter(db *sql.DB, mdb *internal.MemoryDB, mq *internal.MessageQueue) func(chi.Router) {
|
||||
h := Container(db, mdb, mq)
|
||||
|
||||
return func(r chi.Router) {
|
||||
r.Use(middlewares.Authenticated)
|
||||
r.Post("/exec", h.Exec())
|
||||
r.Get("/running", h.Running())
|
||||
r.Post("/cookies", h.SetCookies())
|
||||
r.Post("/template", h.AddTemplate())
|
||||
r.Get("/template/all", h.GetTemplates())
|
||||
r.Delete("/template/{id}", h.DeleteTemplate())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/marcopeocchi/yt-dlp-web-ui/server/internal"
|
||||
)
|
||||
|
||||
@@ -81,3 +82,75 @@ func (h *Handler) SetCookies() http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) AddTemplate() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
defer r.Body.Close()
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
req := new(internal.CustomTemplate)
|
||||
|
||||
err := json.NewDecoder(r.Body).Decode(req)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if req.Name == "" || req.Content == "" {
|
||||
http.Error(w, "Invalid template", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
err = h.service.SaveTemplate(r.Context(), req)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
err = json.NewEncoder(w).Encode("ok")
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) GetTemplates() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
defer r.Body.Close()
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
templates, err := h.service.GetTemplates(r.Context())
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
err = json.NewEncoder(w).Encode(templates)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) DeleteTemplate() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
defer r.Body.Close()
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
id := chi.URLParam(r, "id")
|
||||
|
||||
err := h.service.DeleteTemplate(r.Context(), id)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
err = json.NewEncoder(w).Encode("ok")
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"sync"
|
||||
|
||||
"github.com/marcopeocchi/yt-dlp-web-ui/server/internal"
|
||||
@@ -14,11 +15,12 @@ var (
|
||||
handlerOnce sync.Once
|
||||
)
|
||||
|
||||
func ProvideService(db *internal.MemoryDB, mq *internal.MessageQueue) *Service {
|
||||
func ProvideService(db *sql.DB, mdb *internal.MemoryDB, mq *internal.MessageQueue) *Service {
|
||||
serviceOnce.Do(func() {
|
||||
service = &Service{
|
||||
db: db,
|
||||
mq: mq,
|
||||
mdb: mdb,
|
||||
db: db,
|
||||
mq: mq,
|
||||
}
|
||||
})
|
||||
return service
|
||||
|
||||
@@ -2,15 +2,18 @@ package rest
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"os"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/marcopeocchi/yt-dlp-web-ui/server/internal"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
db *internal.MemoryDB
|
||||
mq *internal.MessageQueue
|
||||
mdb *internal.MemoryDB
|
||||
db *sql.DB
|
||||
mq *internal.MessageQueue
|
||||
}
|
||||
|
||||
func (s *Service) Exec(req internal.DownloadRequest) (string, error) {
|
||||
@@ -23,7 +26,7 @@ func (s *Service) Exec(req internal.DownloadRequest) (string, error) {
|
||||
},
|
||||
}
|
||||
|
||||
id := s.db.Set(p)
|
||||
id := s.mdb.Set(p)
|
||||
s.mq.Publish(p)
|
||||
|
||||
return id, nil
|
||||
@@ -34,7 +37,7 @@ func (s *Service) Running(ctx context.Context) (*[]internal.ProcessResponse, err
|
||||
case <-ctx.Done():
|
||||
return nil, errors.New("context cancelled")
|
||||
default:
|
||||
return s.db.All(), nil
|
||||
return s.mdb.All(), nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,3 +52,64 @@ func (s *Service) SetCookies(ctx context.Context, cookies string) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) SaveTemplate(ctx context.Context, template *internal.CustomTemplate) error {
|
||||
conn, err := s.db.Conn(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer conn.Close()
|
||||
|
||||
_, err = conn.ExecContext(
|
||||
ctx,
|
||||
"INSERT INTO templates (id, name, content) VALUES (?, ?, ?)",
|
||||
uuid.NewString(),
|
||||
template.Name,
|
||||
template.Content,
|
||||
)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Service) GetTemplates(ctx context.Context) (*[]internal.CustomTemplate, error) {
|
||||
conn, err := s.db.Conn(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer conn.Close()
|
||||
|
||||
rows, err := conn.QueryContext(ctx, "SELECT * FROM templates")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
templates := make([]internal.CustomTemplate, 0)
|
||||
|
||||
for rows.Next() {
|
||||
t := internal.CustomTemplate{}
|
||||
|
||||
err := rows.Scan(&t.Id, &t.Name, &t.Content)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
templates = append(templates, t)
|
||||
}
|
||||
|
||||
return &templates, nil
|
||||
}
|
||||
|
||||
func (s *Service) DeleteTemplate(ctx context.Context, id string) error {
|
||||
conn, err := s.db.Conn(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer conn.Close()
|
||||
|
||||
_, err = conn.ExecContext(ctx, "DELETE FROM templates WHERE id = ?", id)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user