added some comments on the server side

This commit is contained in:
2024-03-18 10:19:39 +01:00
parent b45107c94b
commit 3f836d0fa6
11 changed files with 28 additions and 98 deletions

View File

@@ -1,3 +1,8 @@
> [!IMPORTANT]
> I'm looking for a co-mantainer.
> Lately I'm not feeling well both physically and mentally.
---
> [!IMPORTANT]
> Major frontend refactoring in progress.
> I won't add features or fix minor issues until completition.

View File

@@ -35,6 +35,7 @@ func Instance() *Config {
return instance
}
// Initialises the Config struct given its config file
func (c *Config) LoadFile(filename string) error {
fd, err := os.Open(filename)
if err != nil {

View File

@@ -5,6 +5,7 @@ import (
"database/sql"
)
// Run the table migration
func AutoMigrate(ctx context.Context, db *sql.DB) error {
conn, err := db.Conn(ctx)
if err != nil {

View File

@@ -17,6 +17,11 @@ import (
"github.com/marcopeocchi/yt-dlp-web-ui/server/utils"
)
/*
File based operation handlers (should be moved to rest/handlers.go) or in
a entirely self-contained package
*/
type DirectoryEntry struct {
Name string `json:"name"`
Path string `json:"path"`

View File

@@ -236,10 +236,6 @@ func (p *Process) GetFormatsSync() (DownloadFormats, error) {
wg.Add(2)
if err != nil {
return DownloadFormats{}, err
}
log.Println(
cli.BgRed, "Metadata", cli.Reset,
cli.BgBlue, "Formats", cli.Reset,

View File

@@ -39,6 +39,9 @@ func validateToken(tokenValue string) error {
return nil
}
// Authentication does NOT use http-Only cookies since there's not risk for XSS
// By exposing the server through https it's completely safe to use httpheaders
func Authenticated(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("X-Authentication")

View File

@@ -1,93 +0,0 @@
package middlewares
import (
"fmt"
"io"
"io/fs"
"mime"
"net/http"
"os"
"path/filepath"
"strings"
)
type SpaHandler struct {
Entrypoint string
Filesystem fs.FS
routes []string
}
func NewSpaHandler(index string, fs fs.FS) *SpaHandler {
return &SpaHandler{
Entrypoint: index,
Filesystem: fs,
}
}
func (s *SpaHandler) AddClientRoute(route string) *SpaHandler {
s.routes = append(s.routes, route)
return s
}
// Handler for serving a compiled react frontend
// each client-side routes must be provided
func (s *SpaHandler) Handler() http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(
w,
http.StatusText(http.StatusMethodNotAllowed),
http.StatusMethodNotAllowed,
)
return
}
path := filepath.Clean(r.URL.Path)
// basically all frontend routes are needed :/
hasRoute := false
for _, route := range s.routes {
hasRoute = strings.HasPrefix(path, route)
if hasRoute {
break
}
}
if path == "/" || hasRoute {
path = s.Entrypoint
}
path = strings.TrimPrefix(path, "/")
file, err := s.Filesystem.Open(path)
if err != nil {
if os.IsNotExist(err) {
http.NotFound(w, r)
return
}
http.Error(
w,
http.StatusText(http.StatusInternalServerError),
http.StatusInternalServerError,
)
return
}
contentType := mime.TypeByExtension(filepath.Ext(path))
w.Header().Set("Content-Type", contentType)
if strings.HasPrefix(path, "assets/") {
w.Header().Set("Cache-Control", "public, max-age=2592000")
}
stat, err := file.Stat()
if err == nil && stat.Size() > 0 {
w.Header().Set("Content-Length", fmt.Sprintf("%d", stat.Size()))
}
w.WriteHeader(http.StatusOK)
io.Copy(w, file)
})
}

View File

@@ -12,6 +12,10 @@ type Handler struct {
service *Service
}
/*
REST version of the JSON-RPC interface
*/
func (h *Handler) Exec() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()

View File

@@ -14,6 +14,7 @@ var upgrader = websocket.Upgrader{
},
}
// WebSockets JSON-RPC handler
func WebSocket(w http.ResponseWriter, r *http.Request) {
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
@@ -47,6 +48,7 @@ func WebSocket(w http.ResponseWriter, r *http.Request) {
}
}
// HTTP-POST JSON-RPC handler
func Post(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()

View File

@@ -6,13 +6,16 @@ import (
"net/rpc/jsonrpc"
)
// Wrapper for HTTP RPC request that implements io.Reader interface
// Wrapper for jsonrpc.ServeConn that simplifies its usage
type rpcRequest struct {
r io.Reader
rw io.ReadWriter
done chan bool
}
// Takes a reader that can be an *http.Request or anthing that implements
// io.ReadWriter interface.
// Call() will perform the jsonRPC call and write or read from the ReadWriter
func newRequest(r io.Reader) *rpcRequest {
var buf bytes.Buffer
done := make(chan bool)

View File

@@ -6,6 +6,9 @@ import "time"
//
// Debounce emits the most recently emitted value from the source
// withing the timespan set by the span time.Duration
//
// Soon it will be deprecated since it doesn't add anything useful.
// (It lowers the CPU usage by a negligible margin)
func Sample(span time.Duration, source chan []byte, done chan struct{}, fn func(e []byte)) {
var (
item []byte