Add proper logging

This commit is contained in:
Ben Sarmiento
2023-11-05 01:23:41 +01:00
parent 1b116c2194
commit a9c71a3e93
11 changed files with 103 additions and 96 deletions

View File

@@ -2,7 +2,6 @@ package universal
import (
"fmt"
"log"
"net/http"
"path"
"path/filepath"
@@ -11,10 +10,14 @@ import (
"github.com/debridmediamanager.com/zurg/internal/config"
"github.com/debridmediamanager.com/zurg/internal/torrent"
"github.com/debridmediamanager.com/zurg/pkg/davextra"
"github.com/debridmediamanager.com/zurg/pkg/logutil"
"github.com/hashicorp/golang-lru/v2/expirable"
)
func HandleHeadRequest(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, c config.ConfigInterface, cache *expirable.LRU[string, string]) {
rlog := logutil.NewLogger()
log := rlog.Named("unihead")
requestPath := path.Clean(r.URL.Path)
requestPath = strings.Replace(requestPath, "/http", "", 1)
if requestPath == "/favicon.ico" {
@@ -24,7 +27,7 @@ func HandleHeadRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torren
segments := strings.Split(requestPath, "/")
// If there are less than 3 segments, return an error or adjust as needed
if len(segments) < 4 {
log.Println("Method not implemented", r.Method, r.URL.Path)
log.Errorf("Request %s %s not supported yet", r.Method, r.URL.Path)
http.Error(w, "Method not implemented", http.StatusMethodNotAllowed)
return
}
@@ -45,7 +48,7 @@ func HandleHeadRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torren
torrents := t.FindAllTorrentsWithName(baseDirectory, torrentName)
if torrents == nil {
log.Println("Cannot find torrent", requestPath)
log.Errorf("Cannot find torrent %s in the directory %s", requestPath, baseDirectory)
http.Error(w, "Cannot find file", http.StatusNotFound)
return
}
@@ -53,12 +56,13 @@ func HandleHeadRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torren
filenameV2, linkFragment := davextra.ExtractLinkFragment(filename)
_, file := getFile(torrents, filenameV2, linkFragment)
if file == nil {
log.Println("Cannot find file (head)", requestPath)
log.Errorf("Cannot find file from path %s", requestPath)
http.Error(w, "Cannot find file", http.StatusNotFound)
return
}
if file.Link == "" {
log.Println("Link not found (head)", filename)
// This is a dead file, serve an alternate file
log.Errorf("File %s is no longer available", filename)
http.Error(w, "Cannot find file", http.StatusNotFound)
return
}