changed all "log" and "fmt" to "slog" in package main
This commit is contained in:
+2
-3
@@ -1,7 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -11,8 +11,7 @@ func (s *Server) handlerRefresh(w http.ResponseWriter, req *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err := s.c.Refresh(); err != nil {
|
if err := s.c.Refresh(); err != nil {
|
||||||
// #log
|
slog.Error("refresh failed", "err", err)
|
||||||
log.Printf("refresh failed: %v", err)
|
|
||||||
http.Error(w, "refresh failed", http.StatusInternalServerError)
|
http.Error(w, "refresh failed", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-9
@@ -3,7 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -21,8 +21,7 @@ func (s *Server) handlePackage(w http.ResponseWriter, req *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// record the useragent from requestor
|
// record the useragent from requestor
|
||||||
// #log level debug
|
slog.Debug("Requestors User Agent", "UA", 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
|
||||||
// <mirrorroot>/[core, extra, etc]/os/[x86_64, arm, etc]/package.pkg.tar.zst[.sig]
|
// <mirrorroot>/[core, extra, etc]/os/[x86_64, arm, etc]/package.pkg.tar.zst[.sig]
|
||||||
@@ -34,13 +33,11 @@ func (s *Server) handlePackage(w http.ResponseWriter, req *http.Request) {
|
|||||||
cachedFile, err := s.c.Fetch(repoPath)
|
cachedFile, err := s.c.Fetch(repoPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if upstreamErr, ok := errors.AsType[*cache.UpstreamError](err); ok {
|
if upstreamErr, ok := errors.AsType[*cache.UpstreamError](err); ok {
|
||||||
// #log level warn
|
slog.Warn("upstream error", "err", upstreamErr.Error())
|
||||||
log.Printf("upstream Error: %v", upstreamErr.Error())
|
|
||||||
http.Error(w, "Not found upstream", upstreamErr.StatusCode)
|
http.Error(w, "Not found upstream", upstreamErr.StatusCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// #log level warn
|
slog.Warn("fetch error", "err", 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
|
||||||
}
|
}
|
||||||
@@ -51,8 +48,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 error
|
slog.Warn("streaming error", "err", err)
|
||||||
log.Printf("error streaming file to client: %v", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,8 +2,10 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"log"
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/ewpt3ch/pkgstash/internal/cache"
|
"github.com/ewpt3ch/pkgstash/internal/cache"
|
||||||
)
|
)
|
||||||
@@ -15,18 +17,33 @@ type Server struct {
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
// set config from flag if available
|
// get options from cli flags
|
||||||
var configPath string
|
var configPath string
|
||||||
flag.StringVar(&configPath, "config", "", "path to config file")
|
flag.StringVar(&configPath, "config", "", "path to config file")
|
||||||
|
logFlag := flag.String("loglevel", "INFO", "loglevel: DEBUG, INFO, WARN, ERROR")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
|
// set config from flag if available
|
||||||
if len(configPath) == 0 {
|
if len(configPath) == 0 {
|
||||||
configPath = "/etc/pkgstash/pkgstash.toml"
|
configPath = "/etc/pkgstash/pkgstash.toml"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//set log level from flag if available
|
||||||
|
var logLevel slog.Level
|
||||||
|
if err := logLevel.UnmarshalText([]byte(*logFlag)); err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "invalid log level %q, defaulting to INFO\n", *logFlag)
|
||||||
|
logLevel = slog.LevelInfo
|
||||||
|
}
|
||||||
|
|
||||||
|
logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
|
||||||
|
Level: logLevel,
|
||||||
|
}))
|
||||||
|
slog.SetDefault(logger)
|
||||||
|
|
||||||
cfg, err := ReadConfig(configPath)
|
cfg, err := ReadConfig(configPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// #log error
|
slog.Error("failed to read config", "err", err)
|
||||||
log.Fatal(err)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
c := cache.NewCache(cfg.CacheRoot, cfg.MirrorURLs, cfg.MirroredRepos)
|
c := cache.NewCache(cfg.CacheRoot, cfg.MirrorURLs, cfg.MirroredRepos)
|
||||||
@@ -37,8 +54,8 @@ 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 error
|
slog.Error("failed to refesh db files", "err", err)
|
||||||
log.Fatal(err)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
httpServe := &http.Server{
|
httpServe := &http.Server{
|
||||||
@@ -46,8 +63,10 @@ func main() {
|
|||||||
Handler: mux,
|
Handler: mux,
|
||||||
}
|
}
|
||||||
|
|
||||||
// #log error
|
slog.Info("serving pkgstash", "root", cfg.CacheRoot, "port", cfg.Port)
|
||||||
log.Printf("serving pkgstash root: %v on port: %v", cfg.CacheRoot, cfg.Port)
|
if err = httpServe.ListenAndServe(); err != nil {
|
||||||
log.Fatal(httpServe.ListenAndServe())
|
slog.Error("server failed", "err", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user