refresh handler

This commit is contained in:
2026-04-15 14:13:28 -06:00
parent bbaa6396b0
commit 58f9093406
5 changed files with 59 additions and 1 deletions
+34
View File
@@ -1,5 +1,11 @@
package main
import (
"fmt"
"github.com/BurntSushi/toml"
)
type Config struct {
MirrorRoot string
MirrorURL string
@@ -19,3 +25,31 @@ func NewConfig() *Config {
Auth: AuthConfig{Token: "FakeToken"},
}
}
func ReadConfig(path string) (*Config, error) {
var cfg Config
_, err := toml.DecodeFile(path, &cfg)
if err != nil {
return nil, fmt.Errorf("Error loading config from %s: %w", path, err)
}
if err = cfg.validate(); err != nil {
return nil, fmt.Errorf("invalid config: %w", err)
}
return &cfg, nil
}
func (c *Config) validate() error {
if c.MirrorRoot == "" {
return fmt.Errorf("cache root is required")
}
if c.MirrorURL == "" {
return fmt.Errorf("mirror url is required")
}
if c.Port == "" {
c.Port = "8090"
}
return nil
}
+2
View File
@@ -3,3 +3,5 @@ module gitea.ewpt3ch.dev/ewpt3ch/pkgstash
go 1.26.2
require golang.org/x/sync v0.20.0
require github.com/BurntSushi/toml v1.6.0 // indirect
+2
View File
@@ -1,2 +1,4 @@
github.com/BurntSushi/toml v1.6.0 h1:dRaEfpa2VI55EwlIW72hMRHdWouJeRF7TPYhI+AUQjk=
github.com/BurntSushi/toml v1.6.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
golang.org/x/sync v0.20.0 h1:e0PTpb7pjO8GAtTs2dQ6jYa5BWYlMuX047Dco/pItO4=
golang.org/x/sync v0.20.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
+17
View File
@@ -0,0 +1,17 @@
package main
import (
"net/http"
)
func (s *Server) handlerRefresh(w http.ResponseWriter, req *http.Request) {
if req.Header.Get("Authorization") != "Bearer "+s.cfg.Auth.Token {
http.Error(w, "unauthorized", http.StatusInternalServerError)
return
}
if err := s.c.Refresh(); err != nil {
http.Error(w, "refresh failed", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusNoContent)
}
+4 -1
View File
@@ -13,7 +13,10 @@ type Server struct {
}
func main() {
cfg := NewConfig()
cfg, err := ReadConfig("/home/ewpt3ch/dev/pacman-cache-server/tmprepo/app.config.toml")
if err != nil {
log.Fatal(err)
}
c := cache.NewCache(cfg.MirrorRoot, cfg.MirrorURL)
srv := &Server{cfg: cfg, c: c}