read config from file

This commit is contained in:
2026-04-15 14:22:05 -06:00
parent 58f9093406
commit 87d52aae58
3 changed files with 17 additions and 14 deletions
+14 -11
View File
@@ -7,22 +7,22 @@ import (
)
type Config struct {
MirrorRoot string
MirrorURL string
Port string
Auth AuthConfig
CacheRoot string `toml:"cache_root"`
MirrorURL string `toml:"mirror_url"`
Port string `toml:"port"`
Auth AuthConfig `toml:"auth"`
}
type AuthConfig struct {
Token string
Token string `toml:"token"`
}
func NewConfig() *Config {
return &Config{
MirrorRoot: "/home/ewpt3ch/dev/pacman-cache-server/tmprepo",
MirrorURL: "https://us.mirrors.cicku.me/archlinux/",
Port: "8090",
Auth: AuthConfig{Token: "FakeToken"},
CacheRoot: "/home/ewpt3ch/dev/pacman-cache-server/tmprepo",
MirrorURL: "https://us.mirrors.cicku.me/archlinux/",
Port: "8090",
Auth: AuthConfig{Token: "FakeToken"},
}
}
@@ -31,7 +31,7 @@ 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)
return nil, fmt.Errorf("error loading config from %s: %w", path, err)
}
if err = cfg.validate(); err != nil {
@@ -42,7 +42,7 @@ func ReadConfig(path string) (*Config, error) {
}
func (c *Config) validate() error {
if c.MirrorRoot == "" {
if c.CacheRoot == "" {
return fmt.Errorf("cache root is required")
}
if c.MirrorURL == "" {
@@ -51,5 +51,8 @@ func (c *Config) validate() error {
if c.Port == "" {
c.Port = "8090"
}
if c.Auth.Token == "" {
return fmt.Errorf("auth token is required")
}
return nil
}
+2 -2
View File
@@ -15,8 +15,8 @@ func (s *Server) handlePackage(w http.ResponseWriter, req *http.Request) {
repo := req.PathValue("repo")
arch := req.PathValue("arch")
file := req.PathValue("file")
repoPath := filepath.Join(repo, "os", arch, file) //path from mirror root to pkg or db file
pkgPath := filepath.Join(s.cfg.MirrorRoot, repoPath) //absolute path for local read of the file
repoPath := filepath.Join(repo, "os", arch, file) //path from mirror root to pkg or db file
pkgPath := filepath.Join(s.cfg.CacheRoot, repoPath) //absolute path for local read of the file
if _, err := os.Stat(pkgPath); err != nil {
err = s.c.Fetch(repoPath)
+1 -1
View File
@@ -17,7 +17,7 @@ func main() {
if err != nil {
log.Fatal(err)
}
c := cache.NewCache(cfg.MirrorRoot, cfg.MirrorURL)
c := cache.NewCache(cfg.CacheRoot, cfg.MirrorURL)
srv := &Server{cfg: cfg, c: c}
mux := http.NewServeMux()