diff --git a/README.md b/README.md index f29ee1b..2d531ea 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/server/config/parser.go b/server/config/parser.go index bedff3b..72b2edc 100644 --- a/server/config/parser.go +++ b/server/config/parser.go @@ -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 { diff --git a/server/dbutils/migrate.go b/server/dbutils/migrate.go index 5ec1de3..4aa9693 100644 --- a/server/dbutils/migrate.go +++ b/server/dbutils/migrate.go @@ -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 { diff --git a/server/handlers/archive.go b/server/handlers/archive.go index dfcc67b..a6a4481 100644 --- a/server/handlers/archive.go +++ b/server/handlers/archive.go @@ -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"` diff --git a/server/internal/process.go b/server/internal/process.go index 2421d9d..2312386 100644 --- a/server/internal/process.go +++ b/server/internal/process.go @@ -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, diff --git a/server/middleware/jwt.go b/server/middleware/jwt.go index d7de94f..44dc309 100644 --- a/server/middleware/jwt.go +++ b/server/middleware/jwt.go @@ -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") diff --git a/server/middleware/spa_handler.go b/server/middleware/spa_handler.go deleted file mode 100644 index f729754..0000000 --- a/server/middleware/spa_handler.go +++ /dev/null @@ -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) - }) -} diff --git a/server/rest/handlers.go b/server/rest/handlers.go index b275c43..e1db849 100644 --- a/server/rest/handlers.go +++ b/server/rest/handlers.go @@ -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() diff --git a/server/rpc/handlers.go b/server/rpc/handlers.go index f901b18..9d760ae 100644 --- a/server/rpc/handlers.go +++ b/server/rpc/handlers.go @@ -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() diff --git a/server/rpc/wrapper.go b/server/rpc/wrapper.go index 631bce2..504eff9 100644 --- a/server/rpc/wrapper.go +++ b/server/rpc/wrapper.go @@ -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) diff --git a/server/rx/extensions.go b/server/rx/extensions.go index 38c8f90..2044324 100644 --- a/server/rx/extensions.go +++ b/server/rx/extensions.go @@ -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