sonda/internal/config/config.go
tgiachi 89f1b5275a feat: scan the libraries by themselves when their music changed
Every 15 minutes (library.auto_scan_minutes, SONDA_AUTO_SCAN_MINUTES, 0 turns it off)
Sonda takes the signature of each library, a hash of the names of everything in it folder
by folder, which costs only the names of the files and so suits a NAS that sleeps. A
library whose signature changed (a song or a folder came, went or was renamed) is
scanned the usual way; every library is also scanned at least once a day, to find a song
tagged again where it stands.

It does not scan while another scan runs, tries again at the next look when a scan failed,
leaves alone a folder that cannot be read (a NAS that is not mounted), and ignores the
housekeeping folders of a NAS.
2026-11-16 13:05:09 +01:00

326 lines
11 KiB
Go

// Package config loads and validates the sondad configuration.
//
// Precedence: built-in defaults, then the TOML file, then environment variables.
package config
import (
"bytes"
"errors"
"fmt"
"math"
"os"
"path/filepath"
"regexp"
"strconv"
"time"
"github.com/pelletier/go-toml/v2"
)
const minJWTSecretLen = 32
// Config is the full sondad configuration.
type Config struct {
Server Server `toml:"server"`
Library Library `toml:"library"`
Database Database `toml:"database"`
Log Log `toml:"log"`
Auth Auth `toml:"auth"`
Listening Listening `toml:"listening"`
Taste Taste `toml:"taste"`
Scoring Scoring `toml:"scoring"`
Storage Storage `toml:"storage"`
Wikipedia Wikipedia `toml:"wikipedia"`
}
// Server holds HTTP listener settings.
type Server struct {
Listen string `toml:"listen"`
}
// Library holds scanner settings. The libraries themselves (name and path) live
// in the database and are managed through the API.
type Library struct {
ScanWorkers int `toml:"scan_workers"` // parallel tag readers per scan
// AutoScanMinutes is how often Sonda looks at the folders of the libraries and scans the ones whose music changed;
// 0 turns it off. Looking reads the names of the files and nothing else.
AutoScanMinutes int `toml:"auto_scan_minutes"`
// AllowDelete lets an admin delete the extra copies of a song from the music folders. It is the
// one thing that makes Sonda write there, so it is off until the operator turns it on (and it
// needs the folders mounted writable).
AllowDelete bool `toml:"allow_delete"`
}
// Database selects and configures the storage backend.
//
// Driver is "sqlite" (uses Path) or "postgres" (uses DSN).
type Database struct {
Driver string `toml:"driver"`
Path string `toml:"path"`
DSN string `toml:"dsn"`
}
// Log holds logging settings. Env is "dev" or "production".
type Log struct {
Env string `toml:"env"`
}
// Auth holds token settings. JWTSecret must be long and random,
// for example: openssl rand -hex 32.
type Auth struct {
JWTSecret string `toml:"jwt_secret"`
AccessTTLMinutes int `toml:"access_ttl_minutes"` // lifetime of an access token
RefreshTTLHours int `toml:"refresh_ttl_hours"` // lifetime of a refresh token
}
// AccessTTL returns the access token lifetime.
func (a Auth) AccessTTL() time.Duration {
return time.Duration(a.AccessTTLMinutes) * time.Minute
}
// RefreshTTL returns the refresh token lifetime.
func (a Auth) RefreshTTL() time.Duration {
return time.Duration(a.RefreshTTLHours) * time.Hour
}
// Listening holds the rule for what counts as a listen.
type Listening struct {
// MinListenSeconds is how long a play must last to count as a listen when it
// is not reported as complete.
MinListenSeconds int `toml:"min_listen_seconds"`
}
// MinListen returns the minimum play time of a counted listen.
func (l Listening) MinListen() time.Duration {
return time.Duration(l.MinListenSeconds) * time.Second
}
// Taste holds the knobs of the model that learns from listening.
type Taste struct {
// HalfLifeDays is how long it takes for an old listen to weigh half as much.
HalfLifeDays int `toml:"half_life_days"`
}
// HalfLife returns the half-life of a listening event.
func (t Taste) HalfLife() time.Duration {
return time.Duration(t.HalfLifeDays) * 24 * time.Hour
}
// Scoring holds the weights of the parts of a Discover score. Only their
// proportions matter: 0.4, 0.25, 0.25 and 0.1 are the same as 4, 2.5, 2.5 and 1.
type Scoring struct {
WeightAffinity float64 `toml:"weight_affinity"` // similar to the artists I like
WeightGenre float64 `toml:"weight_genre"` // tags close to the tags I like
WeightPreference float64 `toml:"weight_preference"` // what my listening says about the artist
WeightDiscovery float64 `toml:"weight_discovery"` // bonus for what I have not played
}
// Wikipedia says in which languages to look for the description of an artist, in
// order: the first language that has a page for it wins.
type Wikipedia struct {
Languages []string `toml:"languages"` // two or three lower case letters each, at most five
}
var wikipediaLanguageRe = regexp.MustCompile(`^[a-z]{2,3}$`)
// defaultDataDir puts the data folder next to a SQLite database, so that a service
// started from another working directory finds the same place; with PostgreSQL it is
// "data" in the working directory.
func defaultDataDir(db Database) string {
if db.Driver == "sqlite" && db.Path != "" {
return filepath.Join(filepath.Dir(db.Path), "data")
}
return "data"
}
// Storage says where Sonda keeps the files it makes (covers and, later, other
// pictures). They can be built again from the music, so this folder needs no backup,
// but losing it makes every cover be built again.
type Storage struct {
// DataDir is the folder; empty means "data" next to the SQLite database.
DataDir string `toml:"data_dir"`
}
// Default returns the configuration used when nothing else is set.
func Default() Config {
return Config{
Server: Server{Listen: "127.0.0.1:8080"},
Library: Library{ScanWorkers: 4, AutoScanMinutes: 15},
Database: Database{Driver: "sqlite", Path: "sonda.db"},
Log: Log{Env: "production"},
Auth: Auth{AccessTTLMinutes: 15, RefreshTTLHours: 720}, // 15 minutes, 30 days
Listening: Listening{MinListenSeconds: 20},
Taste: Taste{HalfLifeDays: 90},
Wikipedia: Wikipedia{Languages: []string{"en"}},
Scoring: Scoring{WeightAffinity: 0.40, WeightGenre: 0.25, WeightPreference: 0.25, WeightDiscovery: 0.10},
}
}
// Load builds a Config from defaults, the TOML file at path, and environment
// variables. An empty path skips the file. Unknown keys in the file are errors.
func Load(path string) (Config, error) {
// Provider settings live in the database now. A variable that used to carry
// the Last.fm key must not be ignored in silence.
if os.Getenv("SONDA_LASTFM_API_KEY") != "" {
return Config{}, errors.New("SONDA_LASTFM_API_KEY is no longer read: set the key with " +
"PUT /api/v1/providers/lastfm and unset the variable")
}
cfg := Default()
if path != "" {
data, err := os.ReadFile(path)
if err != nil {
return Config{}, fmt.Errorf("read config: %w", err)
}
dec := toml.NewDecoder(bytes.NewReader(data))
dec.DisallowUnknownFields()
if err := dec.Decode(&cfg); err != nil {
return Config{}, explainDecodeError(path, err)
}
}
applyEnv(&cfg)
// A typo here must not be ignored in silence: it decides whether Sonda may delete files.
if v := os.Getenv("SONDA_ALLOW_DELETE"); v != "" {
allow, err := strconv.ParseBool(v)
if err != nil {
return Config{}, fmt.Errorf("SONDA_ALLOW_DELETE must be true or false, got %q", v)
}
cfg.Library.AllowDelete = allow
}
if v := os.Getenv("SONDA_AUTO_SCAN_MINUTES"); v != "" {
n, err := strconv.Atoi(v)
if err != nil {
return Config{}, fmt.Errorf("SONDA_AUTO_SCAN_MINUTES must be a whole number of minutes, got %q", v)
}
cfg.Library.AutoScanMinutes = n
}
if cfg.Storage.DataDir == "" {
cfg.Storage.DataDir = defaultDataDir(cfg.Database)
}
return cfg, nil
}
func applyEnv(cfg *Config) {
if v := os.Getenv("SONDA_DATA_DIR"); v != "" {
cfg.Storage.DataDir = v
}
if v := os.Getenv("SONDA_ENV"); v != "" {
cfg.Log.Env = v
}
if v := os.Getenv("SONDA_LISTEN"); v != "" {
cfg.Server.Listen = v
}
if v := os.Getenv("SONDA_DB_DRIVER"); v != "" {
cfg.Database.Driver = v
}
if v := os.Getenv("SONDA_DB_PATH"); v != "" {
cfg.Database.Path = v
}
if v := os.Getenv("SONDA_DB_DSN"); v != "" {
cfg.Database.DSN = v
}
if v := os.Getenv("SONDA_JWT_SECRET"); v != "" {
cfg.Auth.JWTSecret = v
}
}
// Validate checks that the configuration is usable.
func (c Config) Validate() error {
var errs []error
if c.Server.Listen == "" {
errs = append(errs, errors.New("server.listen is required"))
}
switch c.Database.Driver {
case "sqlite":
if c.Database.Path == "" {
errs = append(errs, errors.New("database.path is required for the sqlite driver"))
}
case "postgres":
if c.Database.DSN == "" {
errs = append(errs, errors.New("database.dsn is required for the postgres driver"))
}
default:
errs = append(errs, fmt.Errorf("database.driver must be \"sqlite\" or \"postgres\", got %q", c.Database.Driver))
}
if c.Log.Env != "dev" && c.Log.Env != "production" {
errs = append(errs, fmt.Errorf("log.env must be \"dev\" or \"production\", got %q", c.Log.Env))
}
if len(c.Auth.JWTSecret) < minJWTSecretLen {
errs = append(errs, fmt.Errorf("auth.jwt_secret must be at least %d characters (try: openssl rand -hex 32)", minJWTSecretLen))
}
if c.Auth.AccessTTLMinutes <= 0 {
errs = append(errs, errors.New("auth.access_ttl_minutes must be greater than 0"))
}
if c.Auth.RefreshTTLHours <= 0 {
errs = append(errs, errors.New("auth.refresh_ttl_hours must be greater than 0"))
}
if c.Listening.MinListenSeconds < 1 || c.Listening.MinListenSeconds > 3600 {
errs = append(errs, errors.New("listening.min_listen_seconds must be between 1 and 3600"))
}
if c.Taste.HalfLifeDays < 1 || c.Taste.HalfLifeDays > 3650 {
errs = append(errs, errors.New("taste.half_life_days must be between 1 and 3650"))
}
sum := 0.0
for _, w := range []struct {
key string
v float64
}{
{"scoring.weight_affinity", c.Scoring.WeightAffinity},
{"scoring.weight_genre", c.Scoring.WeightGenre},
{"scoring.weight_preference", c.Scoring.WeightPreference},
{"scoring.weight_discovery", c.Scoring.WeightDiscovery},
} {
if math.IsNaN(w.v) || w.v < 0 || w.v > 10 {
errs = append(errs, fmt.Errorf("%s must be a number from 0 to 10", w.key))
}
sum += w.v
}
if sum == 0 {
errs = append(errs, errors.New("scoring.weight_*: at least one weight must be above 0"))
}
if n := len(c.Wikipedia.Languages); n < 1 || n > 5 {
errs = append(errs, errors.New("wikipedia.languages must hold one to five languages"))
} else {
seen := map[string]bool{}
for _, l := range c.Wikipedia.Languages {
if !wikipediaLanguageRe.MatchString(l) || seen[l] {
errs = append(errs, fmt.Errorf("wikipedia.languages: %q is not a language code of two or three lower case letters, or is repeated", l))
break
}
seen[l] = true
}
}
if c.Storage.DataDir == "" {
errs = append(errs, errors.New("storage.data_dir must not be empty"))
}
if c.Library.AutoScanMinutes < 0 || c.Library.AutoScanMinutes > 10080 {
errs = append(errs, errors.New("library.auto_scan_minutes must be between 0 (off) and 10080 (a week)"))
}
if c.Library.ScanWorkers < 1 {
errs = append(errs, errors.New("library.scan_workers must be at least 1"))
}
return errors.Join(errs...)
}
// explainDecodeError turns the strict decoder's "unknown key" into something
// useful for the one key that used to exist.
func explainDecodeError(path string, err error) error {
var missing *toml.StrictMissingError
if errors.As(err, &missing) {
for _, e := range missing.Errors {
key := e.Key()
if len(key) > 0 && key[0] == "providers" {
return fmt.Errorf("parse config %s: the providers section was removed; "+
"remove it and set the settings with PUT /api/v1/providers/{name}", path)
}
if len(key) == 2 && key[0] == "library" && key[1] == "roots" {
return fmt.Errorf("parse config %s: library.roots was removed; "+
"remove it and add each directory as a library with POST /api/v1/libraries", path)
}
}
}
return fmt.Errorf("parse config %s: %w", path, err)
}