115 lines
2.8 KiB
Go
115 lines
2.8 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
type Config struct {
|
|
Server ServerConfig `yaml:"server"`
|
|
Database DatabaseConfig `yaml:"database"`
|
|
Log LogConfig `yaml:"log"`
|
|
Auth AuthConfig `yaml:"auth"`
|
|
Library LibraryConfig `yaml:"library"`
|
|
}
|
|
|
|
type ServerConfig struct {
|
|
Address string `yaml:"address"`
|
|
}
|
|
|
|
type DatabaseConfig struct {
|
|
URL string `yaml:"url"`
|
|
}
|
|
|
|
type LogConfig struct {
|
|
Level string `yaml:"level"`
|
|
Format string `yaml:"format"`
|
|
}
|
|
|
|
type AuthConfig struct {
|
|
AdminBootstrap AdminBootstrapConfig `yaml:"admin_bootstrap"`
|
|
}
|
|
|
|
// AdminBootstrapConfig drives the one-shot admin creation on an empty users
|
|
// table. Username is required to enable the flow; leaving Password blank asks
|
|
// the server to generate a random one and log it to stderr.
|
|
type AdminBootstrapConfig struct {
|
|
Username string `yaml:"username"`
|
|
Password string `yaml:"password"`
|
|
}
|
|
|
|
type LibraryConfig struct {
|
|
ScanPaths []string `yaml:"scan_paths"`
|
|
ScanOnStartup bool `yaml:"scan_on_startup"`
|
|
}
|
|
|
|
func Default() Config {
|
|
return Config{
|
|
Server: ServerConfig{Address: ":4533"},
|
|
Database: DatabaseConfig{URL: ""},
|
|
Log: LogConfig{Level: "info", Format: "text"},
|
|
}
|
|
}
|
|
|
|
func Load(path string) (Config, error) {
|
|
cfg := Default()
|
|
if path != "" {
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
if !os.IsNotExist(err) {
|
|
return cfg, fmt.Errorf("read config %q: %w", path, err)
|
|
}
|
|
} else if err := yaml.Unmarshal(data, &cfg); err != nil {
|
|
return cfg, fmt.Errorf("parse config %q: %w", path, err)
|
|
}
|
|
}
|
|
applyEnv(&cfg)
|
|
return cfg, nil
|
|
}
|
|
|
|
func applyEnv(cfg *Config) {
|
|
if v, ok := os.LookupEnv("SMARTMUSIC_SERVER_ADDRESS"); ok {
|
|
cfg.Server.Address = v
|
|
}
|
|
if v, ok := os.LookupEnv("SMARTMUSIC_DATABASE_URL"); ok {
|
|
cfg.Database.URL = v
|
|
}
|
|
if v, ok := os.LookupEnv("SMARTMUSIC_LOG_LEVEL"); ok {
|
|
cfg.Log.Level = strings.ToLower(v)
|
|
}
|
|
if v, ok := os.LookupEnv("SMARTMUSIC_LOG_FORMAT"); ok {
|
|
cfg.Log.Format = strings.ToLower(v)
|
|
}
|
|
if v, ok := os.LookupEnv("SMARTMUSIC_AUTH_ADMIN_USERNAME"); ok {
|
|
cfg.Auth.AdminBootstrap.Username = v
|
|
}
|
|
if v, ok := os.LookupEnv("SMARTMUSIC_AUTH_ADMIN_PASSWORD"); ok {
|
|
cfg.Auth.AdminBootstrap.Password = v
|
|
}
|
|
if v, ok := os.LookupEnv("SMARTMUSIC_LIBRARY_SCAN_PATHS"); ok {
|
|
cfg.Library.ScanPaths = splitPaths(v)
|
|
}
|
|
if v, ok := os.LookupEnv("SMARTMUSIC_LIBRARY_SCAN_ON_STARTUP"); ok {
|
|
if b, err := strconv.ParseBool(v); err == nil {
|
|
cfg.Library.ScanOnStartup = b
|
|
}
|
|
}
|
|
}
|
|
|
|
// splitPaths splits a colon-separated path list, dropping empty entries.
|
|
// Colon matches PATH-style Unix conventions; Minstrel ships Linux-only.
|
|
func splitPaths(s string) []string {
|
|
parts := strings.Split(s, ":")
|
|
out := parts[:0]
|
|
for _, p := range parts {
|
|
if p != "" {
|
|
out = append(out, p)
|
|
}
|
|
}
|
|
return out
|
|
}
|