read config from file
This commit is contained in:
@@ -7,19 +7,19 @@ 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",
|
||||
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
|
||||
}
|
||||
|
||||
+1
-1
@@ -16,7 +16,7 @@ func (s *Server) handlePackage(w http.ResponseWriter, req *http.Request) {
|
||||
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
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user