moved server main to cmd/server
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
CacheRoot string `toml:"cache_root"`
|
||||
MirrorURLs []string `toml:"mirror_urls"`
|
||||
MirroredRepos []string `toml:"mirrored_repos"`
|
||||
Port string `toml:"port"`
|
||||
Auth AuthConfig `toml:"auth"`
|
||||
}
|
||||
|
||||
type AuthConfig struct {
|
||||
Token string `toml:"token"`
|
||||
}
|
||||
|
||||
/* Function kept for reference for future logic
|
||||
func NewConfig() *Config {
|
||||
return &Config{
|
||||
CacheRoot: "/home/ewpt3ch/dev/pacman-cache-server/tmprepo",
|
||||
MirrorURLs: "https://us.mirrors.cicku.me/archlinux/",
|
||||
Port: "8090",
|
||||
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.CacheRoot == "" {
|
||||
return fmt.Errorf("cache root is required")
|
||||
}
|
||||
if len(c.MirrorURLs) == 0 {
|
||||
return fmt.Errorf("at least one mirror is required")
|
||||
}
|
||||
if len(c.MirroredRepos) == 0 {
|
||||
return fmt.Errorf("at least one repo is required")
|
||||
}
|
||||
if c.Port == "" {
|
||||
return fmt.Errorf("port required")
|
||||
}
|
||||
if c.Auth.Token == "" {
|
||||
return fmt.Errorf("auth token is required")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user