diff --git a/config.go b/config.go index 8781281..e7b1a63 100644 --- a/config.go +++ b/config.go @@ -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 } diff --git a/handlerPkgs.go b/handlerPkgs.go index bf2c467..82d8b0f 100644 --- a/handlerPkgs.go +++ b/handlerPkgs.go @@ -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) diff --git a/main.go b/main.go index f9f109b..bbe9cf8 100644 --- a/main.go +++ b/main.go @@ -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()