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
+11 -8
View File
@@ -7,19 +7,19 @@ import (
) )
type Config struct { type Config struct {
MirrorRoot string CacheRoot string `toml:"cache_root"`
MirrorURL string MirrorURL string `toml:"mirror_url"`
Port string Port string `toml:"port"`
Auth AuthConfig Auth AuthConfig `toml:"auth"`
} }
type AuthConfig struct { type AuthConfig struct {
Token string Token string `toml:"token"`
} }
func NewConfig() *Config { func NewConfig() *Config {
return &Config{ return &Config{
MirrorRoot: "/home/ewpt3ch/dev/pacman-cache-server/tmprepo", CacheRoot: "/home/ewpt3ch/dev/pacman-cache-server/tmprepo",
MirrorURL: "https://us.mirrors.cicku.me/archlinux/", MirrorURL: "https://us.mirrors.cicku.me/archlinux/",
Port: "8090", Port: "8090",
Auth: AuthConfig{Token: "FakeToken"}, Auth: AuthConfig{Token: "FakeToken"},
@@ -31,7 +31,7 @@ func ReadConfig(path string) (*Config, error) {
var cfg Config var cfg Config
_, err := toml.DecodeFile(path, &cfg) _, err := toml.DecodeFile(path, &cfg)
if err != nil { 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 { if err = cfg.validate(); err != nil {
@@ -42,7 +42,7 @@ func ReadConfig(path string) (*Config, error) {
} }
func (c *Config) validate() error { func (c *Config) validate() error {
if c.MirrorRoot == "" { if c.CacheRoot == "" {
return fmt.Errorf("cache root is required") return fmt.Errorf("cache root is required")
} }
if c.MirrorURL == "" { if c.MirrorURL == "" {
@@ -51,5 +51,8 @@ func (c *Config) validate() error {
if c.Port == "" { if c.Port == "" {
c.Port = "8090" c.Port = "8090"
} }
if c.Auth.Token == "" {
return fmt.Errorf("auth token is required")
}
return nil return nil
} }
+1 -1
View File
@@ -16,7 +16,7 @@ func (s *Server) handlePackage(w http.ResponseWriter, req *http.Request) {
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 pkg or db file
pkgPath := filepath.Join(s.cfg.MirrorRoot, repoPath) //absolute path for local read of the file pkgPath := filepath.Join(s.cfg.CacheRoot, repoPath) //absolute path for local read of the file
if _, err := os.Stat(pkgPath); err != nil { if _, err := os.Stat(pkgPath); err != nil {
err = s.c.Fetch(repoPath) err = s.c.Fetch(repoPath)
+1 -1
View File
@@ -17,7 +17,7 @@ func main() {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
c := cache.NewCache(cfg.MirrorRoot, cfg.MirrorURL) c := cache.NewCache(cfg.CacheRoot, cfg.MirrorURL)
srv := &Server{cfg: cfg, c: c} srv := &Server{cfg: cfg, c: c}
mux := http.NewServeMux() mux := http.NewServeMux()