added potential log levels to #log comments
This commit is contained in:
+8
-9
@@ -14,14 +14,14 @@ import (
|
|||||||
|
|
||||||
func (s *Server) handlePackage(w http.ResponseWriter, req *http.Request) {
|
func (s *Server) handlePackage(w http.ResponseWriter, req *http.Request) {
|
||||||
|
|
||||||
// most mirrors don't have a *db.sig so we 404 it here instead of spamming the mirror
|
// db files are not signed so we ignore as to not spam mirrors
|
||||||
if strings.HasSuffix(req.PathValue("file"), ".db.sig") {
|
if strings.HasSuffix(req.PathValue("file"), ".db.sig") {
|
||||||
w.WriteHeader(http.StatusNotFound)
|
w.WriteHeader(http.StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// record the useragent from requestor
|
// record the useragent from requestor
|
||||||
// #log
|
// #log level debug
|
||||||
log.Printf("Requestors UA: %s", req.Header.Get("User-Agent"))
|
log.Printf("Requestors UA: %s", req.Header.Get("User-Agent"))
|
||||||
|
|
||||||
// build file paths from the request, they follow archlinux repo
|
// build file paths from the request, they follow archlinux repo
|
||||||
@@ -29,18 +29,17 @@ func (s *Server) handlePackage(w http.ResponseWriter, req *http.Request) {
|
|||||||
repo := req.PathValue("repo")
|
repo := req.PathValue("repo")
|
||||||
arch := req.PathValue("arch")
|
arch := req.PathValue("arch")
|
||||||
file := req.PathValue("file")
|
file := req.PathValue("file")
|
||||||
repoPath := filepath.Join(repo, "os", arch, file) //path from mirror root to pkg or db file
|
repoPath := filepath.Join(repo, "os", arch, file) //path from mirror root to requested file
|
||||||
|
|
||||||
cachedFile, err := s.c.Fetch(repoPath)
|
cachedFile, err := s.c.Fetch(repoPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
var upstreamErr *cache.UpstreamError
|
if upstreamErr, ok := errors.AsType[*cache.UpstreamError](err); ok {
|
||||||
if errors.As(err, &upstreamErr) {
|
// #log level warn
|
||||||
// #log
|
log.Printf("upstream Error: %v", upstreamErr.Error())
|
||||||
log.Printf("upstream error: %v", err)
|
|
||||||
http.Error(w, "Not found upstream", upstreamErr.StatusCode)
|
http.Error(w, "Not found upstream", upstreamErr.StatusCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// #log
|
// #log level warn
|
||||||
log.Printf("fetch error: %v", err)
|
log.Printf("fetch error: %v", err)
|
||||||
http.Error(w, "Failed to fetch from upstream", http.StatusBadGateway)
|
http.Error(w, "Failed to fetch from upstream", http.StatusBadGateway)
|
||||||
return
|
return
|
||||||
@@ -52,7 +51,7 @@ func (s *Server) handlePackage(w http.ResponseWriter, req *http.Request) {
|
|||||||
w.Header().Set("Content-Length", strconv.FormatInt(cachedFile.Size, 10))
|
w.Header().Set("Content-Length", strconv.FormatInt(cachedFile.Size, 10))
|
||||||
_, err = io.Copy(w, cachedFile.Reader)
|
_, err = io.Copy(w, cachedFile.Reader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// #log
|
// #log error
|
||||||
log.Printf("error streaming file to client: %v", err)
|
log.Printf("error streaming file to client: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Vendored
+3
-1
@@ -15,7 +15,7 @@ func (c *Cache) Fetch(relPath string) (*CacheFile, error) {
|
|||||||
|
|
||||||
// fetch file from upstream
|
// fetch file from upstream
|
||||||
_, err, _ = c.sf.Do(relPath, func() (any, error) {
|
_, err, _ = c.sf.Do(relPath, func() (any, error) {
|
||||||
// #log
|
// #log info
|
||||||
log.Print("calling fetch")
|
log.Print("calling fetch")
|
||||||
return nil, c.fetch(relPath)
|
return nil, c.fetch(relPath)
|
||||||
})
|
})
|
||||||
@@ -46,6 +46,8 @@ func (c *Cache) fetch(relPath string) error {
|
|||||||
if err == nil {
|
if err == nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
// #log warn or info
|
||||||
|
log.Printf("mirror %s returned %v", url, err)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
Vendored
+4
-4
@@ -13,13 +13,13 @@ func (c *Cache) nextMirror() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func downloadToDisk(url, destPath string, c http.Client) error {
|
func downloadToDisk(url, destPath string, c http.Client) error {
|
||||||
// #log
|
// #log info
|
||||||
log.Printf("fetching %v", url)
|
log.Printf("fetching %v", url)
|
||||||
|
|
||||||
// set the user agent
|
// set the user agent
|
||||||
req, err := http.NewRequest("GET", url, nil)
|
req, err := http.NewRequest("GET", url, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// #log
|
// #log info
|
||||||
log.Printf("failed to create request: %v", err)
|
log.Printf("failed to create request: %v", err)
|
||||||
return &UpstreamError{StatusCode: http.StatusInternalServerError}
|
return &UpstreamError{StatusCode: http.StatusInternalServerError}
|
||||||
}
|
}
|
||||||
@@ -27,12 +27,12 @@ func downloadToDisk(url, destPath string, c http.Client) error {
|
|||||||
|
|
||||||
resp, err := c.Do(req)
|
resp, err := c.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// #log
|
// #log warn
|
||||||
log.Printf("error fetching %s: %v", url, err)
|
log.Printf("error fetching %s: %v", url, err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if resp.StatusCode != 200 {
|
if resp.StatusCode != 200 {
|
||||||
// #log
|
// #log info
|
||||||
log.Printf("GET %s returned %d", url, resp.StatusCode)
|
log.Printf("GET %s returned %d", url, resp.StatusCode)
|
||||||
return &UpstreamError{StatusCode: resp.StatusCode}
|
return &UpstreamError{StatusCode: resp.StatusCode}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ func main() {
|
|||||||
|
|
||||||
cfg, err := ReadConfig(configPath)
|
cfg, err := ReadConfig(configPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// #log
|
// #log error
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -37,7 +37,7 @@ func main() {
|
|||||||
mux.HandleFunc("POST /api/refresh", srv.handlerRefresh)
|
mux.HandleFunc("POST /api/refresh", srv.handlerRefresh)
|
||||||
|
|
||||||
if err := srv.c.Refresh(); err != nil {
|
if err := srv.c.Refresh(); err != nil {
|
||||||
// #log
|
// #log error
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -46,7 +46,7 @@ func main() {
|
|||||||
Handler: mux,
|
Handler: mux,
|
||||||
}
|
}
|
||||||
|
|
||||||
// #log
|
// #log error
|
||||||
log.Printf("serving pkgstash root: %v on port: %v", cfg.CacheRoot, cfg.Port)
|
log.Printf("serving pkgstash root: %v on port: %v", cfg.CacheRoot, cfg.Port)
|
||||||
log.Fatal(httpServe.ListenAndServe())
|
log.Fatal(httpServe.ListenAndServe())
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user