commit 3a739dc3ae0cbc5f0c69de44def98adc110eab7e Author: tgiachi Date: Wed Nov 11 12:06:15 2026 +0100 feat: add sondad server with config, storage, auth and listening API - feat(config): TOML config with env overrides, strict parsing and validation - feat(storage): SQLite (modernc) and PostgreSQL (pgx) backends through bun, embedded per-driver SQL migrations with a transactional runner - feat(schema): catalog tables (artists, albums, tracks, audio_files with per-root paths), users and listening events with idempotency keys - feat(auth): Argon2id password hashing, HS256 JWT tokens, first-user-only registration and login - feat(api): gin REST API under /api/v1 (health, register, login, listens) with a consistent error envelope, body limit and request logging - feat(sondad): zap logger, graceful shutdown, composition root in main - chore: example config, .gitignore for secrets, DB files, IDE and agent files diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4af4b6b --- /dev/null +++ b/.gitignore @@ -0,0 +1,23 @@ +/bin/ +*.test +*.out +sonda.toml +*.db +*.db-wal +*.db-shm + +# AI agent instruction files: never published (Codeberg EULA) +agent.md +Agent.md +AGENT.md +agents.md +Agents.md +AGENTS.md +claude.md +Claude.md +CLAUDE.md +GEMINI.md +.claude/ + +# IDE +.idea/ diff --git a/cmd/sondad/logger.go b/cmd/sondad/logger.go new file mode 100644 index 0000000..b6bb222 --- /dev/null +++ b/cmd/sondad/logger.go @@ -0,0 +1,14 @@ +package main + +import ( + "go.uber.org/zap" +) + +// newLogger builds the application logger. +// env "dev" gives a human-readable console logger; anything else gives JSON. +func newLogger(env string) (*zap.Logger, error) { + if env == "dev" { + return zap.NewDevelopment() + } + return zap.NewProduction() +} diff --git a/cmd/sondad/main.go b/cmd/sondad/main.go new file mode 100644 index 0000000..10f2897 --- /dev/null +++ b/cmd/sondad/main.go @@ -0,0 +1,124 @@ +package main + +import ( + "context" + "errors" + "flag" + "fmt" + "net/http" + "os" + "os/signal" + "syscall" + "time" + + "github.com/gin-gonic/gin" + "go.uber.org/zap" + + httpapi "codeberg.org/squidd/sonda/internal/api/http" + "codeberg.org/squidd/sonda/internal/auth" + "codeberg.org/squidd/sonda/internal/config" + "codeberg.org/squidd/sonda/internal/listening" + "codeberg.org/squidd/sonda/internal/storage" + "codeberg.org/squidd/sonda/migrations" +) + +const defaultConfigFile = "sonda.toml" + +func main() { + if err := run(); err != nil { + fmt.Fprintln(os.Stderr, "sondad:", err) + os.Exit(1) + } +} + +func run() error { + configPath := flag.String("config", "", "path to the TOML config file (default ./"+defaultConfigFile+" if present)") + flag.Parse() + + path := *configPath + if path == "" { + if _, err := os.Stat(defaultConfigFile); err == nil { + path = defaultConfigFile + } + } + + cfg, err := config.Load(path) + if err != nil { + return err + } + if err := cfg.Validate(); err != nil { + return fmt.Errorf("invalid config: %w", err) + } + + logger, err := newLogger(cfg.Log.Env) + if err != nil { + return fmt.Errorf("create logger: %w", err) + } + defer func() { _ = logger.Sync() }() + + ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) + defer stop() + + target := cfg.Database.Path + if cfg.Database.Driver == storage.DriverPostgres { + target = cfg.Database.DSN + } + db, err := storage.Open(cfg.Database.Driver, target) + if err != nil { + return err + } + defer func() { _ = db.Close() }() + + dbMigrations, err := migrations.For(cfg.Database.Driver) + if err != nil { + return err + } + if err := storage.Migrate(ctx, db, dbMigrations); err != nil { + return err + } + + // Composition root: build services and hand them to the HTTP layer. + tokens := auth.NewTokenIssuer(cfg.Auth.JWTSecret, cfg.Auth.TokenTTL()) + if cfg.Log.Env == "production" { + gin.SetMode(gin.ReleaseMode) + } + router := httpapi.NewRouter(httpapi.Deps{ + Logger: logger, + Auth: auth.NewService(storage.NewUserRepo(db), tokens), + Tokens: tokens, + Listening: listening.NewService(storage.NewListeningRepo(db)), + }) + + // No WriteTimeout: audio streaming (later) needs long responses. + srv := &http.Server{ + Addr: cfg.Server.Listen, + Handler: router, + ReadHeaderTimeout: 10 * time.Second, + IdleTimeout: 2 * time.Minute, + } + + logger.Info("sondad starting", + zap.String("config", path), + zap.String("env", cfg.Log.Env), + zap.String("listen", cfg.Server.Listen), + zap.Strings("library_roots", cfg.Library.Roots), + zap.String("database_driver", cfg.Database.Driver), // never log the DSN: it holds the password + ) + + serveErr := make(chan error, 1) + go func() { serveErr <- srv.ListenAndServe() }() + + select { + case err := <-serveErr: + return fmt.Errorf("http server: %w", err) + case <-ctx.Done(): + logger.Info("shutting down") + } + + shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + if err := srv.Shutdown(shutdownCtx); err != nil && !errors.Is(err, http.ErrServerClosed) { + return fmt.Errorf("shutdown: %w", err) + } + return nil +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..0f3fc09 --- /dev/null +++ b/go.mod @@ -0,0 +1,63 @@ +module codeberg.org/squidd/sonda + +go 1.27.1 + +require ( + github.com/gin-gonic/gin v1.12.0 + github.com/golang-jwt/jwt/v5 v5.3.1 + github.com/jackc/pgx/v5 v5.12.0 + github.com/pelletier/go-toml/v2 v2.4.3 + github.com/uptrace/bun v1.3.0 + github.com/uptrace/bun/dialect/pgdialect v1.3.0 + github.com/uptrace/bun/dialect/sqlitedialect v1.3.0 + go.uber.org/zap v1.28.0 + golang.org/x/crypto v0.58.0 + modernc.org/sqlite v1.62.0 +) + +require ( + github.com/bytedance/gopkg v0.1.3 // indirect + github.com/bytedance/sonic v1.15.0 // indirect + github.com/bytedance/sonic/loader v0.5.0 // indirect + github.com/cloudwego/base64x v0.1.6 // indirect + github.com/dustin/go-humanize v1.1.0 // indirect + github.com/gabriel-vasile/mimetype v1.4.12 // indirect + github.com/gin-contrib/sse v1.1.0 // indirect + github.com/go-playground/locales v0.14.1 // indirect + github.com/go-playground/universal-translator v0.18.1 // indirect + github.com/go-playground/validator/v10 v10.30.1 // indirect + github.com/goccy/go-json v0.10.5 // indirect + github.com/goccy/go-yaml v1.19.2 // indirect + github.com/google/uuid v1.6.0 // indirect + github.com/jackc/pgpassfile v1.0.0 // indirect + github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect + github.com/jackc/puddle/v2 v2.2.3 // indirect + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/klauspost/cpuid/v2 v2.3.0 // indirect + github.com/leodido/go-urn v1.4.0 // indirect + github.com/mattn/go-isatty v0.0.24 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/ncruces/go-strftime v1.1.0 // indirect + github.com/puzpuzpuz/xsync/v3 v3.5.1 // indirect + github.com/quic-go/qpack v0.6.0 // indirect + github.com/quic-go/quic-go v0.59.0 // indirect + github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect + github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc // indirect + github.com/twitchyliquid64/golang-asm v0.15.1 // indirect + github.com/ugorji/go/codec v1.3.1 // indirect + github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect + github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect + go.mongodb.org/mongo-driver/v2 v2.5.0 // indirect + go.uber.org/multierr v1.11.0 // indirect + golang.org/x/arch v0.22.0 // indirect + golang.org/x/net v0.60.0 // indirect + golang.org/x/sync v0.24.0 // indirect + golang.org/x/sys v0.49.0 // indirect + golang.org/x/text v0.43.0 // indirect + google.golang.org/protobuf v1.36.10 // indirect + modernc.org/libc v1.80.1 // indirect + modernc.org/mathutil v1.7.1 // indirect + modernc.org/memory v1.12.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..387926c --- /dev/null +++ b/go.sum @@ -0,0 +1,169 @@ +github.com/bytedance/gopkg v0.1.3 h1:TPBSwH8RsouGCBcMBktLt1AymVo2TVsBVCY4b6TnZ/M= +github.com/bytedance/gopkg v0.1.3/go.mod h1:576VvJ+eJgyCzdjS+c4+77QF3p7ubbtiKARP3TxducM= +github.com/bytedance/sonic v1.15.0 h1:/PXeWFaR5ElNcVE84U0dOHjiMHQOwNIx3K4ymzh/uSE= +github.com/bytedance/sonic v1.15.0/go.mod h1:tFkWrPz0/CUCLEF4ri4UkHekCIcdnkqXw9VduqpJh0k= +github.com/bytedance/sonic/loader v0.5.0 h1:gXH3KVnatgY7loH5/TkeVyXPfESoqSBSBEiDd5VjlgE= +github.com/bytedance/sonic/loader v0.5.0/go.mod h1:AR4NYCk5DdzZizZ5djGqQ92eEhCCcdf5x77udYiSJRo= +github.com/cloudwego/base64x v0.1.6 h1:t11wG9AECkCDk5fMSoxmufanudBtJ+/HemLstXDLI2M= +github.com/cloudwego/base64x v0.1.6/go.mod h1:OFcloc187FXDaYHvrNIjxSe8ncn0OOM8gEHfghB2IPU= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dustin/go-humanize v1.1.0 h1:dbKTrvD0klcbBV/h4AWJdMuZogJACoMlvWIWZ5b2xWg= +github.com/dustin/go-humanize v1.1.0/go.mod h1:hc1CvRkJMsgxqjmjMQF3QNRAZBwY8AXBAzKYoSX9sFI= +github.com/gabriel-vasile/mimetype v1.4.12 h1:e9hWvmLYvtp846tLHam2o++qitpguFiYCKbn0w9jyqw= +github.com/gabriel-vasile/mimetype v1.4.12/go.mod h1:d+9Oxyo1wTzWdyVUPMmXFvp4F9tea18J8ufA774AB3s= +github.com/gin-contrib/sse v1.1.0 h1:n0w2GMuUpWDVp7qSpvze6fAu9iRxJY4Hmj6AmBOU05w= +github.com/gin-contrib/sse v1.1.0/go.mod h1:hxRZ5gVpWMT7Z0B0gSNYqqsSCNIJMjzvm6fqCz9vjwM= +github.com/gin-gonic/gin v1.12.0 h1:b3YAbrZtnf8N//yjKeU2+MQsh2mY5htkZidOM7O0wG8= +github.com/gin-gonic/gin v1.12.0/go.mod h1:VxccKfsSllpKshkBWgVgRniFFAzFb9csfngsqANjnLc= +github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= +github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= +github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= +github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= +github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= +github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= +github.com/go-playground/validator/v10 v10.30.1 h1:f3zDSN/zOma+w6+1Wswgd9fLkdwy06ntQJp0BBvFG0w= +github.com/go-playground/validator/v10 v10.30.1/go.mod h1:oSuBIQzuJxL//3MelwSLD5hc2Tu889bF0Idm9Dg26cM= +github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4= +github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M= +github.com/goccy/go-yaml v1.19.2 h1:PmFC1S6h8ljIz6gMRBopkjP1TVT7xuwrButHID66PoM= +github.com/goccy/go-yaml v1.19.2/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA= +github.com/golang-jwt/jwt/v5 v5.3.1 h1:kYf81DTWFe7t+1VvL7eS+jKFVWaUnK9cB1qbwn63YCY= +github.com/golang-jwt/jwt/v5 v5.3.1/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE= +github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= +github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/pprof v0.0.0-20261016150608-1facf8f91b50 h1:PlNWJRxu5APSV0pWqfIsZ0qt9OOGHSWfZQ4zvx+WgWs= +github.com/google/pprof v0.0.0-20261016150608-1facf8f91b50/go.mod h1:yUo1AMMpsPIDFGuwDK8nLcf8+vvAd5l2O6JDZ6hpwrM= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k= +github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM= +github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= +github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= +github.com/jackc/pgx/v5 v5.12.0 h1:hWwax8YiGT0YiN50U1oIDn5kgR7KC2tG3rmavwpPkxY= +github.com/jackc/pgx/v5 v5.12.0/go.mod h1:u2Fi6no03HCSBYE+ZVdLxbwdu3xyi2vU+K/YNJioSds= +github.com/jackc/puddle/v2 v2.2.3 h1:f8HoREISb3l5eO9coPgK7U3hKLhWGokOFVcThl5jrao= +github.com/jackc/puddle/v2 v2.2.3/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= +github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= +github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/klauspost/cpuid/v2 v2.3.0 h1:S4CRMLnYUhGeDFDqkGriYKdfoFlDnMtqTiI/sFzhA9Y= +github.com/klauspost/cpuid/v2 v2.3.0/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0= +github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= +github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= +github.com/mattn/go-isatty v0.0.24 h1:tGZZoVgT/KiqK1c8ocVLeDS8BSWMRd47J3Lbz7vsReI= +github.com/mattn/go-isatty v0.0.24/go.mod h1:nMCL3Zebbrt45jsMDgnfIwz6ydEQApk5oEI3HqDio6A= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/ncruces/go-strftime v1.1.0 h1:vtPmPWMkb539RGV9WpWT9EwJGvrOCKvMNI4obWL47Hk= +github.com/ncruces/go-strftime v1.1.0/go.mod h1:yVaq7iwCN09vHRrKBrrflXT0M9tTMG2KmZ9Fj7xeG/g= +github.com/pelletier/go-toml/v2 v2.4.3 h1:GTRvJQutkOSftxIFD5xw9aepkYNuPWmVJpffdDPYVpY= +github.com/pelletier/go-toml/v2 v2.4.3/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/puzpuzpuz/xsync/v3 v3.5.1 h1:GJYJZwO6IdxN/IKbneznS6yPkVC+c3zyY/j19c++5Fg= +github.com/puzpuzpuz/xsync/v3 v3.5.1/go.mod h1:VjzYrABPabuM4KyBh1Ftq6u8nhwY5tBPKP9jpmh0nnA= +github.com/quic-go/qpack v0.6.0 h1:g7W+BMYynC1LbYLSqRt8PBg5Tgwxn214ZZR34VIOjz8= +github.com/quic-go/qpack v0.6.0/go.mod h1:lUpLKChi8njB4ty2bFLX2x4gzDqXwUpaO1DP9qMDZII= +github.com/quic-go/quic-go v0.59.0 h1:OLJkp1Mlm/aS7dpKgTc6cnpynnD2Xg7C1pwL6vy/SAw= +github.com/quic-go/quic-go v0.59.0/go.mod h1:upnsH4Ju1YkqpLXC305eW3yDZ4NfnNbmQRCMWS58IKU= +github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE= +github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= +github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= +github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc h1:9lRDQMhESg+zvGYmW5DyG0UqvY96Bu5QYsTLvCHdrgo= +github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc/go.mod h1:bciPuU6GHm1iF1pBvUfxfsH0Wmnc2VbpgvbI9ZWuIRs= +github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= +github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= +github.com/ugorji/go/codec v1.3.1 h1:waO7eEiFDwidsBN6agj1vJQ4AG7lh2yqXyOXqhgQuyY= +github.com/ugorji/go/codec v1.3.1/go.mod h1:pRBVtBSKl77K30Bv8R2P+cLSGaTtex6fsA2Wjqmfxj4= +github.com/uptrace/bun v1.3.0 h1:hUcbfRpbQk4/upFSI+uZf6jSJw5A0lkIKt0XdpvbUZo= +github.com/uptrace/bun v1.3.0/go.mod h1:wNltaKJk4JtOt4SG5I5zmA7v0/Mzjh1+/S906Rayd3Y= +github.com/uptrace/bun/dialect/pgdialect v1.3.0 h1:0ZkGqGugxaPCxko4DV2f4d7i+FnM2Qj3rNpChfhiv54= +github.com/uptrace/bun/dialect/pgdialect v1.3.0/go.mod h1:kP5HGEpAX8iJCqj3WHm04XnoeBPacdCN1P2HiwMtHpg= +github.com/uptrace/bun/dialect/sqlitedialect v1.3.0 h1:XzxkORYksxkLsIQLJHFYUb3yDvqSQHzTungyKpJhHTw= +github.com/uptrace/bun/dialect/sqlitedialect v1.3.0/go.mod h1:x2D1sY50Itio0I/FVdNe30oMUT2ObiAS+oILcMR+Wys= +github.com/vmihailenco/msgpack/v5 v5.4.1 h1:cQriyiUvjTwOHg8QZaPihLWeRAAVoCpE00IUPn0Bjt8= +github.com/vmihailenco/msgpack/v5 v5.4.1/go.mod h1:GaZTsDaehaPpQVyxrf5mtQlH+pc21PIudVV/E3rRQok= +github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g= +github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds= +go.mongodb.org/mongo-driver/v2 v2.5.0 h1:yXUhImUjjAInNcpTcAlPHiT7bIXhshCTL3jVBkF3xaE= +go.mongodb.org/mongo-driver/v2 v2.5.0/go.mod h1:yOI9kBsufol30iFsl1slpdq1I0eHPzybRWdyYUs8K/0= +go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= +go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= +go.uber.org/mock v0.6.0 h1:hyF9dfmbgIX5EfOdasqLsWD6xqpNZlXblLB/Dbnwv3Y= +go.uber.org/mock v0.6.0/go.mod h1:KiVJ4BqZJaMj4svdfmHM0AUx4NJYO8ZNpPnZn1Z+BBU= +go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= +go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= +go.uber.org/zap v1.28.0 h1:IZzaP1Fv73/T/pBMLk4VutPl36uNC+OSUh3JLG3FIjo= +go.uber.org/zap v1.28.0/go.mod h1:rDLpOi171uODNm/mxFcuYWxDsqWSAVkFdX4XojSKg/Q= +go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc= +go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg= +golang.org/x/arch v0.22.0 h1:c/Zle32i5ttqRXjdLyyHZESLD/bB90DCU1g9l/0YBDI= +golang.org/x/arch v0.22.0/go.mod h1:dNHoOeKiyja7GTvF9NJS1l3Z2yntpQNzgrjh1cU103A= +golang.org/x/crypto v0.58.0 h1:COkYLr4k7nDI4r6QtMML2AfzeIswWHAJthLS6K/M/54= +golang.org/x/crypto v0.58.0/go.mod h1:Xh+kl5A+M0gMiWDytH2Hhr5Z7WbNCpv/eB/EFgfSk+w= +golang.org/x/mod v0.42.0 h1:ICeL3t2L30ljSPHHxDnR4fqVcBt/zR2zJAb9juavmQg= +golang.org/x/mod v0.42.0/go.mod h1:u5fzIrRInkL8MaO1cbRkUHT64v303YIT5KEqpNr7jRs= +golang.org/x/net v0.60.0 h1:79p50tfZlm0J9YfoDsSi639qSXNGVwEzOPLCxM2FsYU= +golang.org/x/net v0.60.0/go.mod h1:2DA/G1UfVbCpQPeWTmMPGY7Cs2PkBkwu743bVX5PIVg= +golang.org/x/sync v0.24.0 h1:iqiDrGoDgyuvAKrhLQthUEzU/R378N78gXbBjxed7oA= +golang.org/x/sync v0.24.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0= +golang.org/x/sys v0.49.0 h1:XbzkgYJHdqh/8m2Uu0W/dQv8nktxx4BFHp1M0gROTXA= +golang.org/x/sys v0.49.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og= +golang.org/x/text v0.43.0 h1:1QivrlhwAsnMOcqgOdl9my7s9OyZl8O5/UGUTJVXoWc= +golang.org/x/text v0.43.0/go.mod h1:hbSIYA/amXcRXTFSZqh8tWHEoqRvvbKPJ2uz/KU+a+s= +golang.org/x/tools v0.52.0 h1:ryxg4U63oaUYk7uOLTF37n43f4qgtGvqV4Ioit1BaEs= +golang.org/x/tools v0.52.0/go.mod h1:ItcP5UNaVGG+Yc4eIYmaFLNJ4soEopkx5Y2d4lcEurs= +google.golang.org/protobuf v1.36.10 h1:AYd7cD/uASjIL6Q9LiTjz8JLcrh/88q5UObnmY3aOOE= +google.golang.org/protobuf v1.36.10/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +modernc.org/cc/v4 v4.30.6-0.20261101114202-1b9d1a076f6b h1:WE5SLOURcNd8s6uPR/n4WV4weQxxYKi5OpEZkfNVvdE= +modernc.org/cc/v4 v4.30.6-0.20261101114202-1b9d1a076f6b/go.mod h1:OnovgIhbbMXMu1aISnJ0wvVD1KnW+cAUJkIrAWh+kVI= +modernc.org/ccgo/v4 v4.38.1 h1:wdxJVBuFT8JufZnnQyS6Bbz3NOWhViD0xJOGR96Bpd4= +modernc.org/ccgo/v4 v4.38.1/go.mod h1:kOpEkJRbFwBvjUWwn4j1j9B7iFHbl1+orhgTMCDkDBk= +modernc.org/fileutil v1.4.0 h1:j6ZzNTftVS054gi281TyLjHPp6CPHr2KCxEXjEbD6SM= +modernc.org/fileutil v1.4.0/go.mod h1:EqdKFDxiByqxLk8ozOxObDSfcVOv/54xDs/DUHdvCUU= +modernc.org/gc/v2 v2.6.5 h1:nyqdV8q46KvTpZlsw66kWqwXRHdjIlJOhG6kxiV/9xI= +modernc.org/gc/v2 v2.6.5/go.mod h1:YgIahr1ypgfe7chRuJi2gD7DBQiKSLMPgBQe9oIiito= +modernc.org/gc/v3 v3.2.0 h1:/Y3LGDMQaoYf55PebP1qsZ5FDGgAZyG1RYKEHeQzh1o= +modernc.org/gc/v3 v3.2.0/go.mod h1:HFK/6AGESC7Ex+EZJhJ2Gni6cTaYpSMmU/cT9RmlfYY= +modernc.org/goabi0 v0.2.0 h1:HvEowk7LxcPd0eq6mVOAEMai46V+i7Jrj13t4AzuNks= +modernc.org/goabi0 v0.2.0/go.mod h1:CEFRnnJhKvWT1c1JTI3Avm+tgOWbkOu5oPA8eH8LnMI= +modernc.org/libc v1.80.1 h1:ejn7HtTV3GknNKbklqWQ1nvUrfX8mTxs1D+xp/JQwy4= +modernc.org/libc v1.80.1/go.mod h1:pvo25Rs3OJEbDqhUV9jdEGevxeLJrJeDmNid/AcQV1g= +modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU= +modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg= +modernc.org/memory v1.12.1 h1:nFMiWrpStgZczNl6XI9GnIk/rWhYIyHGUaR04pGbp9g= +modernc.org/memory v1.12.1/go.mod h1:/JP4VbVC+K5sU2wZi9bHoq2MAkCnrt2r98UGeSK7Mjw= +modernc.org/opt v0.2.0 h1:tGyef5ApycA7FSEOMraay9SaTk5zmbx7Tu+cJs4QKZg= +modernc.org/opt v0.2.0/go.mod h1:03fq9lsNfvkYSfxrfUhZCWPk1lm4cq4N+Bh//bEtgns= +modernc.org/sortutil v1.2.1 h1:+xyoGf15mM3NMlPDnFqrteY07klSFxLElE2PVuWIJ7w= +modernc.org/sortutil v1.2.1/go.mod h1:7ZI3a3REbai7gzCLcotuw9AC4VZVpYMjDzETGsSMqJE= +modernc.org/sqlite v1.62.0 h1:gMaCjSM1qpl+iOjiWhR1LYP3zqrYDI+5GYLGt6veP2g= +modernc.org/sqlite v1.62.0/go.mod h1:Jzjh1mt4s0HuQ0KvtB1LIuHiGRmU2D5pgj8dUhW7/SE= +modernc.org/strutil v1.2.1 h1:UneZBkQA+DX2Rp35KcM69cSsNES9ly8mQWD71HKlOA0= +modernc.org/strutil v1.2.1/go.mod h1:EHkiggD70koQxjVdSBM3JKM7k6L0FbGE5eymy9i3B9A= +modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y= +modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM= diff --git a/internal/api/http/api_test.go b/internal/api/http/api_test.go new file mode 100644 index 0000000..8c3ed37 --- /dev/null +++ b/internal/api/http/api_test.go @@ -0,0 +1,213 @@ +package httpapi + +import ( + "bytes" + "context" + "encoding/json" + "net/http" + "net/http/httptest" + "path/filepath" + "strings" + "testing" + "time" + + "github.com/gin-gonic/gin" + "go.uber.org/zap" + + "codeberg.org/squidd/sonda/internal/auth" + "codeberg.org/squidd/sonda/internal/listening" + "codeberg.org/squidd/sonda/internal/storage" + "codeberg.org/squidd/sonda/migrations" +) + +const secret = "0123456789abcdef0123456789abcdef" + +func init() { gin.SetMode(gin.TestMode) } + +// newTestAPI wires real services on a temporary SQLite database. +func newTestAPI(t *testing.T) http.Handler { + t.Helper() + db, err := storage.Open(storage.DriverSQLite, filepath.Join(t.TempDir(), "api.db")) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = db.Close() }) + fsys, err := migrations.For(storage.DriverSQLite) + if err != nil { + t.Fatal(err) + } + if err := storage.Migrate(context.Background(), db, fsys); err != nil { + t.Fatal(err) + } + if _, err := db.DB.Exec("INSERT INTO tracks (title) VALUES ('song')"); err != nil { + t.Fatal(err) + } + + tokens := auth.NewTokenIssuer(secret, time.Hour) + return NewRouter(Deps{ + Logger: zap.NewNop(), + Auth: auth.NewService(storage.NewUserRepo(db), tokens), + Tokens: tokens, + Listening: listening.NewService(storage.NewListeningRepo(db)), + }) +} + +type resp struct { + Code int + Body map[string]any +} + +func do(t *testing.T, h http.Handler, method, path, token string, body any) resp { + t.Helper() + var buf bytes.Buffer + if body != nil { + if s, ok := body.(string); ok { + buf.WriteString(s) + } else if err := json.NewEncoder(&buf).Encode(body); err != nil { + t.Fatal(err) + } + } + req := httptest.NewRequest(method, path, &buf) + req.Header.Set("Content-Type", "application/json") + if token != "" { + req.Header.Set("Authorization", "Bearer "+token) + } + rec := httptest.NewRecorder() + h.ServeHTTP(rec, req) + + out := resp{Code: rec.Code, Body: map[string]any{}} + _ = json.Unmarshal(rec.Body.Bytes(), &out.Body) + return out +} + +func errCode(r resp) string { + e, _ := r.Body["error"].(map[string]any) + c, _ := e["code"].(string) + return c +} + +// registerAndLogin creates the first user and returns a valid token. +func registerAndLogin(t *testing.T, h http.Handler) string { + t.Helper() + creds := map[string]string{"username": "alice", "password": "longenough"} + if r := do(t, h, "POST", "/api/v1/auth/register", "", creds); r.Code != http.StatusCreated { + t.Fatalf("register: %d %v", r.Code, r.Body) + } + r := do(t, h, "POST", "/api/v1/auth/login", "", creds) + if r.Code != http.StatusOK { + t.Fatalf("login: %d %v", r.Code, r.Body) + } + tok, _ := r.Body["token"].(string) + if tok == "" { + t.Fatal("no token in login response") + } + return tok +} + +func TestHealth(t *testing.T) { + r := do(t, newTestAPI(t), "GET", "/api/v1/health", "", nil) + if r.Code != http.StatusOK || r.Body["status"] != "ok" { + t.Fatalf("health: %d %v", r.Code, r.Body) + } +} + +func TestRegisterThenClosed(t *testing.T) { + h := newTestAPI(t) + registerAndLogin(t, h) + + r := do(t, h, "POST", "/api/v1/auth/register", "", map[string]string{"username": "bob", "password": "longenough"}) + if r.Code != http.StatusForbidden || errCode(r) != "registration_closed" { + t.Fatalf("second register: %d %v", r.Code, r.Body) + } +} + +func TestRegisterInvalid(t *testing.T) { + h := newTestAPI(t) + for name, body := range map[string]any{ + "short password": map[string]string{"username": "alice", "password": "x"}, + "bad username": map[string]string{"username": "a", "password": "longenough"}, + "malformed json": "{not json", + } { + r := do(t, h, "POST", "/api/v1/auth/register", "", body) + if r.Code != http.StatusBadRequest || errCode(r) != "invalid_input" { + t.Errorf("%s: %d %v", name, r.Code, r.Body) + } + } +} + +func TestLoginWrongPassword(t *testing.T) { + h := newTestAPI(t) + registerAndLogin(t, h) + r := do(t, h, "POST", "/api/v1/auth/login", "", map[string]string{"username": "alice", "password": "nopenopenope"}) + if r.Code != http.StatusUnauthorized || errCode(r) != "invalid_credentials" { + t.Fatalf("login: %d %v", r.Code, r.Body) + } +} + +func TestListenRequiresAuth(t *testing.T) { + h := newTestAPI(t) + body := map[string]any{"track_id": 1, "event_type": "play", "idempotency_key": "k"} + + if r := do(t, h, "POST", "/api/v1/listens", "", body); r.Code != http.StatusUnauthorized { + t.Errorf("no token: %d", r.Code) + } + if r := do(t, h, "POST", "/api/v1/listens", "garbage", body); r.Code != http.StatusUnauthorized { + t.Errorf("bad token: %d", r.Code) + } + + // A token signed with another secret is rejected. + other, _, _ := auth.NewTokenIssuer("ffffffffffffffffffffffffffffffff", time.Hour).Issue(auth.User{ID: 1, Username: "alice"}) + if r := do(t, h, "POST", "/api/v1/listens", other, body); r.Code != http.StatusUnauthorized { + t.Errorf("foreign token: %d", r.Code) + } +} + +func TestListenFlow(t *testing.T) { + h := newTestAPI(t) + tok := registerAndLogin(t, h) + body := map[string]any{ + "track_id": 1, "event_type": "complete", "duration_played_ms": 1000, + "idempotency_key": "abc", "occurred_at": "2026-01-02T03:04:05Z", + } + + r := do(t, h, "POST", "/api/v1/listens", tok, body) + if r.Code != http.StatusCreated || r.Body["status"] != "recorded" { + t.Fatalf("first: %d %v", r.Code, r.Body) + } + + // Same idempotency key: accepted, not duplicated. + r = do(t, h, "POST", "/api/v1/listens", tok, body) + if r.Code != http.StatusOK || r.Body["status"] != "duplicate" { + t.Fatalf("retry: %d %v", r.Code, r.Body) + } + + body["idempotency_key"] = "def" + body["track_id"] = 999 + r = do(t, h, "POST", "/api/v1/listens", tok, body) + if r.Code != http.StatusNotFound || errCode(r) != "track_not_found" { + t.Fatalf("unknown track: %d %v", r.Code, r.Body) + } + + body["track_id"] = 1 + body["event_type"] = "dance" + r = do(t, h, "POST", "/api/v1/listens", tok, body) + if r.Code != http.StatusBadRequest || errCode(r) != "invalid_input" { + t.Fatalf("bad type: %d %v", r.Code, r.Body) + } + + body["event_type"] = "play" + body["occurred_at"] = "yesterday" + r = do(t, h, "POST", "/api/v1/listens", tok, body) + if r.Code != http.StatusBadRequest || errCode(r) != "invalid_input" { + t.Fatalf("bad time: %d %v", r.Code, r.Body) + } +} + +func TestBodyTooLarge(t *testing.T) { + h := newTestAPI(t) + huge := `{"username":"` + strings.Repeat("a", 200_000) + `","password":"longenough"}` + r := do(t, h, "POST", "/api/v1/auth/register", "", huge) + if r.Code != http.StatusBadRequest && r.Code != http.StatusRequestEntityTooLarge { + t.Fatalf("huge body: %d", r.Code) + } +} diff --git a/internal/api/http/errors.go b/internal/api/http/errors.go new file mode 100644 index 0000000..38c30d8 --- /dev/null +++ b/internal/api/http/errors.go @@ -0,0 +1,18 @@ +package httpapi + +import "github.com/gin-gonic/gin" + +// errorBody is the consistent error envelope: {"error":{"code":"...","message":"..."}}. +type errorBody struct { + Error errorDetail `json:"error"` +} + +type errorDetail struct { + Code string `json:"code"` + Message string `json:"message"` +} + +// writeError aborts the request with the standard error envelope. +func writeError(c *gin.Context, status int, code, message string) { + c.AbortWithStatusJSON(status, errorBody{Error: errorDetail{Code: code, Message: message}}) +} diff --git a/internal/api/http/handlers_auth.go b/internal/api/http/handlers_auth.go new file mode 100644 index 0000000..831a387 --- /dev/null +++ b/internal/api/http/handlers_auth.go @@ -0,0 +1,68 @@ +package httpapi + +import ( + "errors" + "net/http" + "time" + + "github.com/gin-gonic/gin" + + "codeberg.org/squidd/sonda/internal/auth" +) + +type authHandlers struct{ svc authService } + +type credentials struct { + Username string `json:"username"` + Password string `json:"password"` +} + +type userJSON struct { + ID int64 `json:"id"` + Username string `json:"username"` +} + +// register creates the first user. It is closed once any user exists. +func (h *authHandlers) register(c *gin.Context) { + var req credentials + if err := c.ShouldBindJSON(&req); err != nil { + writeError(c, http.StatusBadRequest, "invalid_input", "malformed JSON body") + return + } + user, err := h.svc.Register(c.Request.Context(), req.Username, req.Password) + switch { + case errors.Is(err, auth.ErrInvalidInput): + writeError(c, http.StatusBadRequest, "invalid_input", err.Error()) + case errors.Is(err, auth.ErrRegistrationClosed): + writeError(c, http.StatusForbidden, "registration_closed", err.Error()) + case errors.Is(err, auth.ErrUsernameTaken): + writeError(c, http.StatusConflict, "username_taken", err.Error()) + case err != nil: + _ = c.Error(err) + writeError(c, http.StatusInternalServerError, "internal", "internal error") + default: + c.JSON(http.StatusCreated, userJSON{ID: user.ID, Username: user.Username}) + } +} + +func (h *authHandlers) login(c *gin.Context) { + var req credentials + if err := c.ShouldBindJSON(&req); err != nil { + writeError(c, http.StatusBadRequest, "invalid_input", "malformed JSON body") + return + } + res, err := h.svc.Login(c.Request.Context(), req.Username, req.Password) + switch { + case errors.Is(err, auth.ErrInvalidCredentials): + writeError(c, http.StatusUnauthorized, "invalid_credentials", err.Error()) + case err != nil: + _ = c.Error(err) + writeError(c, http.StatusInternalServerError, "internal", "internal error") + default: + c.JSON(http.StatusOK, gin.H{ + "token": res.Token, + "expires_at": res.ExpiresAt.UTC().Format(time.RFC3339), + "user": userJSON{ID: res.User.ID, Username: res.User.Username}, + }) + } +} diff --git a/internal/api/http/handlers_listen.go b/internal/api/http/handlers_listen.go new file mode 100644 index 0000000..40073a4 --- /dev/null +++ b/internal/api/http/handlers_listen.go @@ -0,0 +1,65 @@ +package httpapi + +import ( + "errors" + "net/http" + "time" + + "github.com/gin-gonic/gin" + + "codeberg.org/squidd/sonda/internal/listening" +) + +type listenHandlers struct{ svc listenService } + +type listenRequest struct { + TrackID int64 `json:"track_id"` + EventType string `json:"event_type"` + OccurredAt string `json:"occurred_at"` // RFC 3339, optional + DurationPlayedMs int64 `json:"duration_played_ms"` + IdempotencyKey string `json:"idempotency_key"` +} + +// record ingests one explicit listen event for the authenticated user. +func (h *listenHandlers) record(c *gin.Context) { + var req listenRequest + if err := c.ShouldBindJSON(&req); err != nil { + writeError(c, http.StatusBadRequest, "invalid_input", "malformed JSON body") + return + } + + var occurred time.Time + if req.OccurredAt != "" { + t, err := time.Parse(time.RFC3339, req.OccurredAt) + if err != nil { + writeError(c, http.StatusBadRequest, "invalid_input", "occurred_at must be RFC 3339") + return + } + occurred = t + } + + userID, _ := c.Get(ctxUserID) + uid, _ := userID.(int64) + + created, err := h.svc.Record(c.Request.Context(), listening.Event{ + UserID: uid, + TrackID: req.TrackID, + Type: listening.EventType(req.EventType), + OccurredAt: occurred, + DurationPlayedMs: req.DurationPlayedMs, + IdempotencyKey: req.IdempotencyKey, + }) + switch { + case errors.Is(err, listening.ErrInvalidEvent): + writeError(c, http.StatusBadRequest, "invalid_input", err.Error()) + case errors.Is(err, listening.ErrTrackNotFound): + writeError(c, http.StatusNotFound, "track_not_found", err.Error()) + case err != nil: + _ = c.Error(err) + writeError(c, http.StatusInternalServerError, "internal", "internal error") + case created: + c.JSON(http.StatusCreated, gin.H{"status": "recorded"}) + default: + c.JSON(http.StatusOK, gin.H{"status": "duplicate"}) + } +} diff --git a/internal/api/http/middleware.go b/internal/api/http/middleware.go new file mode 100644 index 0000000..0fb4711 --- /dev/null +++ b/internal/api/http/middleware.go @@ -0,0 +1,58 @@ +package httpapi + +import ( + "net/http" + "strings" + "time" + + "github.com/gin-gonic/gin" + "go.uber.org/zap" +) + +const ( + ctxUserID = "user_id" + ctxUsername = "username" +) + +// limitBody caps the request body size. +func limitBody(max int64) gin.HandlerFunc { + return func(c *gin.Context) { + c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, max) + c.Next() + } +} + +// requestLog logs one line per request. It never logs headers, query strings +// or bodies, so tokens and passwords cannot leak into the log. +func requestLog(logger *zap.Logger) gin.HandlerFunc { + return func(c *gin.Context) { + start := time.Now() + c.Next() + logger.Info("http request", + zap.String("method", c.Request.Method), + zap.String("path", c.Request.URL.Path), + zap.Int("status", c.Writer.Status()), + zap.Duration("latency", time.Since(start)), + ) + } +} + +// requireAuth accepts only "Authorization: Bearer ". +func requireAuth(tokens tokenParser) gin.HandlerFunc { + return func(c *gin.Context) { + header := c.GetHeader("Authorization") + token, ok := strings.CutPrefix(header, "Bearer ") + if !ok || token == "" { + writeError(c, http.StatusUnauthorized, "unauthorized", "missing bearer token") + return + } + claims, err := tokens.Parse(token) + if err != nil { + writeError(c, http.StatusUnauthorized, "unauthorized", "invalid or expired token") + return + } + c.Set(ctxUserID, claims.UserID) + c.Set(ctxUsername, claims.Username) + c.Next() + } +} diff --git a/internal/api/http/router.go b/internal/api/http/router.go new file mode 100644 index 0000000..7c4b190 --- /dev/null +++ b/internal/api/http/router.go @@ -0,0 +1,64 @@ +// Package httpapi is the first-party REST API (/api/v1). +// +// Handlers stay thin: they validate and serialize, then call services. +package httpapi + +import ( + "context" + "net/http" + + "github.com/gin-gonic/gin" + "go.uber.org/zap" + + "codeberg.org/squidd/sonda/internal/auth" + "codeberg.org/squidd/sonda/internal/listening" +) + +// maxBodyBytes bounds every request body. +const maxBodyBytes = 64 << 10 + +type authService interface { + Register(ctx context.Context, username, password string) (auth.User, error) + Login(ctx context.Context, username, password string) (auth.LoginResult, error) +} + +type tokenParser interface { + Parse(token string) (auth.Claims, error) +} + +type listenService interface { + Record(ctx context.Context, e listening.Event) (bool, error) +} + +// Deps are the services the API needs. +type Deps struct { + Logger *zap.Logger + Auth authService + Tokens tokenParser + Listening listenService +} + +// NewRouter builds the gin engine with all routes. +func NewRouter(d Deps) *gin.Engine { + r := gin.New() + _ = r.SetTrustedProxies(nil) // use the socket address; no forwarded-header trust yet + r.Use(gin.Recovery(), limitBody(maxBodyBytes), requestLog(d.Logger)) + r.NoRoute(func(c *gin.Context) { writeError(c, http.StatusNotFound, "not_found", "no such route") }) + + v1 := r.Group("/api/v1") + v1.GET("/health", health) + + a := &authHandlers{svc: d.Auth} + v1.POST("/auth/register", a.register) + v1.POST("/auth/login", a.login) + + protected := v1.Group("", requireAuth(d.Tokens)) + l := &listenHandlers{svc: d.Listening} + protected.POST("/listens", l.record) + + return r +} + +func health(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{"status": "ok"}) +} diff --git a/internal/auth/auth_test.go b/internal/auth/auth_test.go new file mode 100644 index 0000000..90303e4 --- /dev/null +++ b/internal/auth/auth_test.go @@ -0,0 +1,202 @@ +package auth + +import ( + "context" + "errors" + "strings" + "testing" + "time" + + "github.com/golang-jwt/jwt/v5" +) + +// ---- password hashing ---- + +func TestHashAndVerify(t *testing.T) { + h, err := HashPassword("correct horse") + if err != nil { + t.Fatal(err) + } + if !strings.HasPrefix(h, "$argon2id$") { + t.Fatalf("hash = %q, want argon2id PHC string", h) + } + if !VerifyPassword(h, "correct horse") { + t.Error("correct password rejected") + } + if VerifyPassword(h, "wrong") { + t.Error("wrong password accepted") + } +} + +func TestHashIsSalted(t *testing.T) { + a, _ := HashPassword("same") + b, _ := HashPassword("same") + if a == b { + t.Error("two hashes of the same password are identical") + } +} + +func TestVerifyRejectsGarbage(t *testing.T) { + for _, h := range []string{"", "plaintext", "$argon2id$v=19$m=1,t=1,p=1$bad", "$bcrypt$x$y"} { + if VerifyPassword(h, "x") { + t.Errorf("garbage hash %q accepted", h) + } + } +} + +// ---- tokens ---- + +const testSecret = "0123456789abcdef0123456789abcdef" + +func TestTokenRoundTrip(t *testing.T) { + iss := NewTokenIssuer(testSecret, time.Hour) + tok, exp, err := iss.Issue(User{ID: 7, Username: "bob"}) + if err != nil { + t.Fatal(err) + } + if time.Until(exp) <= 0 { + t.Error("expiry is not in the future") + } + claims, err := iss.Parse(tok) + if err != nil { + t.Fatal(err) + } + if claims.UserID != 7 || claims.Username != "bob" { + t.Errorf("claims = %+v", claims) + } +} + +func TestTokenExpired(t *testing.T) { + iss := NewTokenIssuer(testSecret, time.Hour) + iss.now = func() time.Time { return time.Now().Add(-2 * time.Hour) } + tok, _, err := iss.Issue(User{ID: 1, Username: "a"}) + if err != nil { + t.Fatal(err) + } + iss.now = time.Now + if _, err := iss.Parse(tok); err == nil { + t.Fatal("expired token accepted") + } +} + +func TestTokenWrongSecret(t *testing.T) { + tok, _, _ := NewTokenIssuer(testSecret, time.Hour).Issue(User{ID: 1, Username: "a"}) + other := NewTokenIssuer("ffffffffffffffffffffffffffffffff", time.Hour) + if _, err := other.Parse(tok); err == nil { + t.Fatal("token signed with another secret accepted") + } +} + +func TestTokenRejectsAlgNone(t *testing.T) { + claims := jwt.RegisteredClaims{ + Subject: "1", + Issuer: tokenIssuerName, + ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Hour)), + } + tok, err := jwt.NewWithClaims(jwt.SigningMethodNone, claims).SignedString(jwt.UnsafeAllowNoneSignatureType) + if err != nil { + t.Fatal(err) + } + if _, err := NewTokenIssuer(testSecret, time.Hour).Parse(tok); err == nil { + t.Fatal("alg=none token accepted") + } +} + +func TestTokenGarbage(t *testing.T) { + if _, err := NewTokenIssuer(testSecret, time.Hour).Parse("not.a.jwt"); err == nil { + t.Fatal("garbage token accepted") + } +} + +// ---- service ---- + +type fakeStore struct { + users map[string]fakeUser + next int64 +} + +type fakeUser struct { + u User + hash string +} + +func newFakeStore() *fakeStore { return &fakeStore{users: map[string]fakeUser{}} } + +func (f *fakeStore) CountUsers(context.Context) (int, error) { return len(f.users), nil } + +func (f *fakeStore) CreateUser(_ context.Context, username, hash string) (User, error) { + key := strings.ToLower(username) + if _, ok := f.users[key]; ok { + return User{}, ErrUsernameTaken + } + f.next++ + u := User{ID: f.next, Username: username, CreatedAt: time.Now()} + f.users[key] = fakeUser{u: u, hash: hash} + return u, nil +} + +func (f *fakeStore) UserByUsername(_ context.Context, username string) (User, string, error) { + fu, ok := f.users[strings.ToLower(username)] + if !ok { + return User{}, "", ErrUserNotFound + } + return fu.u, fu.hash, nil +} + +func newTestService() *Service { + return NewService(newFakeStore(), NewTokenIssuer(testSecret, time.Hour)) +} + +func TestRegisterFirstUserThenClosed(t *testing.T) { + s := newTestService() + ctx := context.Background() + if _, err := s.Register(ctx, "alice", "longenough"); err != nil { + t.Fatal(err) + } + if _, err := s.Register(ctx, "bob", "longenough"); !errors.Is(err, ErrRegistrationClosed) { + t.Fatalf("err = %v, want ErrRegistrationClosed", err) + } +} + +func TestRegisterValidation(t *testing.T) { + tests := []struct { + name, user, pass string + }{ + {"short username", "ab", "longenough"}, + {"long username", strings.Repeat("a", 65), "longenough"}, + {"bad chars", "bad name!", "longenough"}, + {"short password", "alice", "short"}, + {"huge password", "alice", strings.Repeat("x", 300)}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + _, err := newTestService().Register(context.Background(), tt.user, tt.pass) + if !errors.Is(err, ErrInvalidInput) { + t.Fatalf("err = %v, want ErrInvalidInput", err) + } + }) + } +} + +func TestLogin(t *testing.T) { + s := newTestService() + ctx := context.Background() + if _, err := s.Register(ctx, "alice", "longenough"); err != nil { + t.Fatal(err) + } + + res, err := s.Login(ctx, "ALICE", "longenough") // usernames are case-insensitive + if err != nil { + t.Fatal(err) + } + if res.Token == "" || res.User.Username != "alice" { + t.Errorf("result = %+v", res) + } + + if _, err := s.Login(ctx, "alice", "wrongpass1"); !errors.Is(err, ErrInvalidCredentials) { + t.Errorf("wrong password: err = %v", err) + } + if _, err := s.Login(ctx, "nobody", "longenough"); !errors.Is(err, ErrInvalidCredentials) { + t.Errorf("unknown user: err = %v", err) + } +} diff --git a/internal/auth/password.go b/internal/auth/password.go new file mode 100644 index 0000000..1e5187f --- /dev/null +++ b/internal/auth/password.go @@ -0,0 +1,68 @@ +package auth + +import ( + "crypto/rand" + "crypto/subtle" + "encoding/base64" + "fmt" + "strings" + + "golang.org/x/crypto/argon2" +) + +// Argon2id parameters (64 MiB, 2 passes, 2 lanes). +const ( + argonTime = 2 + argonMemory = 64 * 1024 // KiB + argonThreads = 2 + argonKeyLen = 32 + argonSaltLen = 16 +) + +// HashPassword returns an Argon2id hash as a PHC-format string. +func HashPassword(password string) (string, error) { + salt := make([]byte, argonSaltLen) + if _, err := rand.Read(salt); err != nil { + return "", fmt.Errorf("generate salt: %w", err) + } + key := argon2.IDKey([]byte(password), salt, argonTime, argonMemory, argonThreads, argonKeyLen) + return fmt.Sprintf("$argon2id$v=%d$m=%d,t=%d,p=%d$%s$%s", + argon2.Version, argonMemory, argonTime, argonThreads, + base64.RawStdEncoding.EncodeToString(salt), + base64.RawStdEncoding.EncodeToString(key)), nil +} + +// VerifyPassword reports whether password matches a hash made by HashPassword. +// Any malformed hash simply returns false. +func VerifyPassword(encoded, password string) bool { + parts := strings.Split(encoded, "$") + if len(parts) != 6 || parts[1] != "argon2id" { + return false + } + + var version int + if _, err := fmt.Sscanf(parts[2], "v=%d", &version); err != nil || version != argon2.Version { + return false + } + + var memory, time uint32 + var threads uint8 + if _, err := fmt.Sscanf(parts[3], "m=%d,t=%d,p=%d", &memory, &time, &threads); err != nil { + return false + } + if memory == 0 || time == 0 || threads == 0 { + return false + } + + salt, err := base64.RawStdEncoding.DecodeString(parts[4]) + if err != nil { + return false + } + want, err := base64.RawStdEncoding.DecodeString(parts[5]) + if err != nil || len(want) == 0 { + return false + } + + got := argon2.IDKey([]byte(password), salt, time, memory, threads, uint32(len(want))) + return subtle.ConstantTimeCompare(got, want) == 1 +} diff --git a/internal/auth/service.go b/internal/auth/service.go new file mode 100644 index 0000000..c377104 --- /dev/null +++ b/internal/auth/service.go @@ -0,0 +1,121 @@ +// Package auth implements local users, password hashing and JWT access tokens. +package auth + +import ( + "context" + "errors" + "fmt" + "regexp" + "sync" + "time" +) + +// Errors returned by the service. Handlers map them to HTTP statuses. +var ( + ErrInvalidInput = errors.New("invalid input") + ErrUsernameTaken = errors.New("username already taken") + ErrUserNotFound = errors.New("user not found") + ErrInvalidCredentials = errors.New("invalid username or password") + ErrRegistrationClosed = errors.New("registration is closed") +) + +var usernameRe = regexp.MustCompile(`^[A-Za-z0-9._-]{3,64}$`) + +const ( + minPasswordLen = 8 + maxPasswordLen = 256 // caps the work an attacker can force on the KDF +) + +// User is a local account. The password hash never leaves the store layer. +type User struct { + ID int64 + Username string + CreatedAt time.Time +} + +// UserStore is what the service needs from persistence. +type UserStore interface { + CountUsers(ctx context.Context) (int, error) + // CreateUser returns ErrUsernameTaken when the name exists (case-insensitive). + CreateUser(ctx context.Context, username, passwordHash string) (User, error) + // UserByUsername returns ErrUserNotFound when there is no such user. + UserByUsername(ctx context.Context, username string) (User, string, error) +} + +// LoginResult is returned by a successful login. +type LoginResult struct { + Token string + ExpiresAt time.Time + User User +} + +// Service registers users and logs them in. +type Service struct { + store UserStore + tokens *TokenIssuer + + mu sync.Mutex // serialises Register so only one first user can win + dummyOnce sync.Once + dummyHash string +} + +// NewService builds a Service. +func NewService(store UserStore, tokens *TokenIssuer) *Service { + return &Service{store: store, tokens: tokens} +} + +// Register creates the first user. Once any user exists, registration is +// closed: there is no admin flow yet. +func (s *Service) Register(ctx context.Context, username, password string) (User, error) { + if !usernameRe.MatchString(username) { + return User{}, fmt.Errorf("%w: username must be 3-64 characters: letters, digits, '.', '_' or '-'", ErrInvalidInput) + } + if len(password) < minPasswordLen || len(password) > maxPasswordLen { + return User{}, fmt.Errorf("%w: password must be %d-%d bytes", ErrInvalidInput, minPasswordLen, maxPasswordLen) + } + + s.mu.Lock() + defer s.mu.Unlock() + + n, err := s.store.CountUsers(ctx) + if err != nil { + return User{}, fmt.Errorf("count users: %w", err) + } + if n > 0 { + return User{}, ErrRegistrationClosed + } + + hash, err := HashPassword(password) + if err != nil { + return User{}, err + } + return s.store.CreateUser(ctx, username, hash) +} + +// Login checks the credentials and returns a signed token. +// Unknown user and wrong password are indistinguishable to the caller. +func (s *Service) Login(ctx context.Context, username, password string) (LoginResult, error) { + user, hash, err := s.store.UserByUsername(ctx, username) + if errors.Is(err, ErrUserNotFound) { + // Spend the same time as a real check to avoid revealing which names exist. + VerifyPassword(s.fakeHash(), password) + return LoginResult{}, ErrInvalidCredentials + } + if err != nil { + return LoginResult{}, fmt.Errorf("find user: %w", err) + } + if !VerifyPassword(hash, password) { + return LoginResult{}, ErrInvalidCredentials + } + + token, exp, err := s.tokens.Issue(user) + if err != nil { + return LoginResult{}, err + } + return LoginResult{Token: token, ExpiresAt: exp, User: user}, nil +} + +func (s *Service) fakeHash() string { + s.dummyOnce.Do(func() { s.dummyHash, _ = HashPassword("sonda-dummy-password") }) + return s.dummyHash +} diff --git a/internal/auth/token.go b/internal/auth/token.go new file mode 100644 index 0000000..913b274 --- /dev/null +++ b/internal/auth/token.go @@ -0,0 +1,71 @@ +package auth + +import ( + "fmt" + "strconv" + "time" + + "github.com/golang-jwt/jwt/v5" +) + +const tokenIssuerName = "sonda" + +// Claims is what a valid access token tells us about the caller. +type Claims struct { + UserID int64 + Username string +} + +type tokenClaims struct { + Username string `json:"username"` + jwt.RegisteredClaims +} + +// TokenIssuer creates and verifies HS256 JWT access tokens. +type TokenIssuer struct { + secret []byte + ttl time.Duration + now func() time.Time +} + +// NewTokenIssuer builds an issuer. The secret must be long and random. +func NewTokenIssuer(secret string, ttl time.Duration) *TokenIssuer { + return &TokenIssuer{secret: []byte(secret), ttl: ttl, now: time.Now} +} + +// Issue signs a token for user and returns it with its expiry time. +func (i *TokenIssuer) Issue(user User) (string, time.Time, error) { + now := i.now() + exp := now.Add(i.ttl) + claims := tokenClaims{Username: user.Username, + Issuer: tokenIssuerName, + Subject: strconv.FormatInt(user.ID, 10), + IssuedAt: jwt.NewNumericDate(now), + ExpiresAt: jwt.NewNumericDate(exp)} + + signed, err := jwt.NewWithClaims(jwt.SigningMethodHS256, claims).SignedString(i.secret) + if err != nil { + return "", time.Time{}, fmt.Errorf("sign token: %w", err) + } + return signed, exp, nil +} + +// Parse verifies the signature, algorithm, issuer and expiry of token. +func (i *TokenIssuer) Parse(token string) (Claims, error) { + var tc tokenClaims + _, err := jwt.ParseWithClaims(token, &tc, + func(*jwt.Token) (any, error) { return i.secret, nil }, + jwt.WithValidMethods([]string{jwt.SigningMethodHS256.Alg()}), + jwt.WithIssuer(tokenIssuerName), + jwt.WithExpirationRequired(), + jwt.WithTimeFunc(i.now), + ) + if err != nil { + return Claims{}, fmt.Errorf("invalid token: %w", err) + } + id, err := strconv.ParseInt(tc.Subject, 10, 64) + if err != nil || id <= 0 { + return Claims{}, fmt.Errorf("invalid token: bad subject") + } + return Claims{UserID: id, Username: tc.Username}, nil +} diff --git a/internal/config/config.go b/internal/config/config.go new file mode 100644 index 0000000..17b31a6 --- /dev/null +++ b/internal/config/config.go @@ -0,0 +1,171 @@ +// 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" + "os" + "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"` + Providers Providers `toml:"providers"` +} + +// Server holds HTTP listener settings. +type Server struct { + Listen string `toml:"listen"` +} + +// Library holds the music directories. They are treated as read-only. +type Library struct { + Roots []string `toml:"roots"` +} + +// 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"` + TokenTTLHours int `toml:"token_ttl_hours"` +} + +// TokenTTL returns the access token lifetime. +func (a Auth) TokenTTL() time.Duration { + return time.Duration(a.TokenTTLHours) * time.Hour +} + +// Providers holds external provider settings. +type Providers struct { + LastFM LastFM `toml:"lastfm"` +} + +// LastFM holds the Last.fm API settings. +type LastFM struct { + APIKey string `toml:"api_key"` +} + +// Default returns the configuration used when nothing else is set. +func Default() Config { + return Config{ + Server: Server{Listen: "127.0.0.1:8080"}, + Database: Database{Driver: "sqlite", Path: "sonda.db"}, + Log: Log{Env: "production"}, + Auth: Auth{TokenTTLHours: 168}, // one week + } +} + +// 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) { + 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{}, fmt.Errorf("parse config %s: %w", path, err) + } + } + + applyEnv(&cfg) + return cfg, nil +} + +func applyEnv(cfg *Config) { + 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 + } + if v := os.Getenv("SONDA_LASTFM_API_KEY"); v != "" { + cfg.Providers.LastFM.APIKey = 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.TokenTTLHours <= 0 { + errs = append(errs, errors.New("auth.token_ttl_hours must be greater than 0")) + } + if len(c.Library.Roots) == 0 { + errs = append(errs, errors.New("library.roots needs at least one directory")) + } + for _, root := range c.Library.Roots { + info, err := os.Stat(root) + switch { + case err != nil: + errs = append(errs, fmt.Errorf("library.roots: %w", err)) + case !info.IsDir(): + errs = append(errs, fmt.Errorf("library.roots: %s is not a directory", root)) + } + } + + return errors.Join(errs...) +} diff --git a/internal/config/config_test.go b/internal/config/config_test.go new file mode 100644 index 0000000..d2e40bb --- /dev/null +++ b/internal/config/config_test.go @@ -0,0 +1,160 @@ +package config + +import ( + "os" + "path/filepath" + "strings" + "testing" + "time" +) + +func writeFile(t *testing.T, dir, content string) string { + t.Helper() + p := filepath.Join(dir, "sonda.toml") + if err := os.WriteFile(p, []byte(content), 0o600); err != nil { + t.Fatal(err) + } + return p +} + +func TestLoadEmptyPathReturnsDefaults(t *testing.T) { + cfg, err := Load("") + if err != nil { + t.Fatal(err) + } + if cfg.Server.Listen != "127.0.0.1:8080" { + t.Errorf("listen = %q", cfg.Server.Listen) + } + if cfg.Database.Path != "sonda.db" { + t.Errorf("db path = %q", cfg.Database.Path) + } + if cfg.Database.Driver != "sqlite" { + t.Errorf("db driver = %q", cfg.Database.Driver) + } + if cfg.Log.Env != "production" { + t.Errorf("env = %q", cfg.Log.Env) + } + if cfg.Auth.TokenTTL() != 168*time.Hour { + t.Errorf("token ttl = %v", cfg.Auth.TokenTTL()) + } +} + +func TestLoadFileOverridesDefaults(t *testing.T) { + dir := t.TempDir() + p := writeFile(t, dir, ` +[server] +listen = "0.0.0.0:9000" + +[library] +roots = ["/a", "/b"] + +[providers.lastfm] +api_key = "k" +`) + cfg, err := Load(p) + if err != nil { + t.Fatal(err) + } + if cfg.Server.Listen != "0.0.0.0:9000" { + t.Errorf("listen = %q", cfg.Server.Listen) + } + if len(cfg.Library.Roots) != 2 { + t.Errorf("roots = %v", cfg.Library.Roots) + } + if cfg.Providers.LastFM.APIKey != "k" { + t.Errorf("api key = %q", cfg.Providers.LastFM.APIKey) + } + // untouched keys keep their defaults + if cfg.Database.Path != "sonda.db" { + t.Errorf("db path = %q", cfg.Database.Path) + } +} + +func TestLoadEnvOverridesFile(t *testing.T) { + dir := t.TempDir() + p := writeFile(t, dir, "[log]\nenv = \"production\"\n") + t.Setenv("SONDA_ENV", "dev") + t.Setenv("SONDA_LISTEN", "127.0.0.1:1") + t.Setenv("SONDA_DB_PATH", "x.db") + t.Setenv("SONDA_DB_DRIVER", "postgres") + t.Setenv("SONDA_DB_DSN", "postgres://u:p@h/db") + t.Setenv("SONDA_LASTFM_API_KEY", "envkey") + t.Setenv("SONDA_JWT_SECRET", "envsecret") + cfg, err := Load(p) + if err != nil { + t.Fatal(err) + } + if cfg.Log.Env != "dev" || cfg.Server.Listen != "127.0.0.1:1" || + cfg.Database.Path != "x.db" || cfg.Providers.LastFM.APIKey != "envkey" || + cfg.Auth.JWTSecret != "envsecret" || + cfg.Database.Driver != "postgres" || cfg.Database.DSN != "postgres://u:p@h/db" { + t.Errorf("env not applied: %+v", cfg) + } +} + +func TestLoadRejectsUnknownField(t *testing.T) { + dir := t.TempDir() + p := writeFile(t, dir, "[server]\nlisten_addr = \"x\"\n") + if _, err := Load(p); err == nil { + t.Fatal("expected error for unknown field") + } +} + +func TestLoadMissingFile(t *testing.T) { + if _, err := Load(filepath.Join(t.TempDir(), "nope.toml")); err == nil { + t.Fatal("expected error for missing file") + } +} + +func TestValidate(t *testing.T) { + good := t.TempDir() + file := filepath.Join(good, "f") + if err := os.WriteFile(file, nil, 0o600); err != nil { + t.Fatal(err) + } + + tests := []struct { + name string + mutate func(*Config) + wantErr string + }{ + {"ok", func(c *Config) {}, ""}, + {"no roots", func(c *Config) { c.Library.Roots = nil }, "library.roots"}, + {"root missing", func(c *Config) { c.Library.Roots = []string{filepath.Join(good, "nope")} }, "library.roots"}, + {"root is file", func(c *Config) { c.Library.Roots = []string{file} }, "not a directory"}, + {"bad env", func(c *Config) { c.Log.Env = "staging" }, "log.env"}, + {"empty listen", func(c *Config) { c.Server.Listen = "" }, "server.listen"}, + {"empty db", func(c *Config) { c.Database.Path = "" }, "database.path"}, + {"bad driver", func(c *Config) { c.Database.Driver = "mysql" }, "database.driver"}, + {"postgres without dsn", func(c *Config) { c.Database.Driver = "postgres" }, "database.dsn"}, + {"postgres ok", func(c *Config) { + c.Database.Driver = "postgres" + c.Database.Path = "" + c.Database.DSN = "postgres://u:p@localhost/db" + }, ""}, + {"no secret", func(c *Config) { c.Auth.JWTSecret = "" }, "auth.jwt_secret"}, + {"short secret", func(c *Config) { c.Auth.JWTSecret = "short" }, "auth.jwt_secret"}, + {"bad ttl", func(c *Config) { c.Auth.TokenTTLHours = 0 }, "auth.token_ttl_hours"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + cfg, err := Load("") + if err != nil { + t.Fatal(err) + } + cfg.Library.Roots = []string{good} + cfg.Auth.JWTSecret = strings.Repeat("s", 32) + tt.mutate(&cfg) + err = cfg.Validate() + if tt.wantErr == "" { + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + return + } + if err == nil || !strings.Contains(err.Error(), tt.wantErr) { + t.Fatalf("err = %v, want containing %q", err, tt.wantErr) + } + }) + } +} diff --git a/internal/listening/listening.go b/internal/listening/listening.go new file mode 100644 index 0000000..5e10416 --- /dev/null +++ b/internal/listening/listening.go @@ -0,0 +1,81 @@ +// Package listening records explicit listen events (play, complete, skip). +// +// These events feed the recommendation engine. Merely opening or prefetching +// an audio URL must never create one. +package listening + +import ( + "context" + "errors" + "fmt" + "time" +) + +// Errors returned by the service. +var ( + ErrInvalidEvent = errors.New("invalid listening event") + ErrTrackNotFound = errors.New("track not found") +) + +// EventType says what the listener did. +type EventType string + +// Supported event types. +const ( + Play EventType = "play" + Complete EventType = "complete" + Skip EventType = "skip" +) + +const maxKeyLen = 128 + +// Event is one listen reported by a client. +type Event struct { + UserID int64 + TrackID int64 + Type EventType + OccurredAt time.Time // zero means "now" + DurationPlayedMs int64 + IdempotencyKey string // client generated; retries with the same key are no-ops +} + +// Store persists events. +type Store interface { + // Record saves e. It returns created=false when an event with the same + // (user, idempotency key) already exists, and ErrTrackNotFound for an + // unknown track. + Record(ctx context.Context, e Event) (created bool, err error) +} + +// Service validates and records events. +type Service struct { + store Store + now func() time.Time +} + +// NewService builds a Service. +func NewService(store Store) *Service { + return &Service{store: store, now: time.Now} +} + +// Record validates e and stores it. +func (s *Service) Record(ctx context.Context, e Event) (bool, error) { + switch e.Type { + case Play, Complete, Skip: + default: + return false, fmt.Errorf("%w: event_type must be play, complete or skip", ErrInvalidEvent) + } + if e.UserID <= 0 || e.TrackID <= 0 { + return false, fmt.Errorf("%w: user and track are required", ErrInvalidEvent) + } + if e.IdempotencyKey == "" || len(e.IdempotencyKey) > maxKeyLen { + return false, fmt.Errorf("%w: idempotency_key must be 1-%d characters", ErrInvalidEvent, maxKeyLen) + } + if e.DurationPlayedMs < 0 { + return false, fmt.Errorf("%w: duration_played_ms cannot be negative", ErrInvalidEvent) + } + if e.OccurredAt.IsZero() { + e.OccurredAt = s.now() + } + return s.store.Record(ctx, e) +} diff --git a/internal/listening/listening_test.go b/internal/listening/listening_test.go new file mode 100644 index 0000000..cf16eb5 --- /dev/null +++ b/internal/listening/listening_test.go @@ -0,0 +1,86 @@ +package listening + +import ( + "context" + "errors" + "strings" + "testing" + "time" +) + +type fakeStore struct { + got []Event + created bool + err error +} + +func (f *fakeStore) Record(_ context.Context, e Event) (bool, error) { + f.got = append(f.got, e) + return f.created, f.err +} + +func TestRecordValidation(t *testing.T) { + valid := Event{UserID: 1, TrackID: 2, Type: Play, IdempotencyKey: "k1"} + tests := []struct { + name string + mutate func(*Event) + }{ + {"bad type", func(e *Event) { e.Type = "like" }}, + {"empty type", func(e *Event) { e.Type = "" }}, + {"no key", func(e *Event) { e.IdempotencyKey = "" }}, + {"long key", func(e *Event) { e.IdempotencyKey = strings.Repeat("k", 129) }}, + {"no track", func(e *Event) { e.TrackID = 0 }}, + {"no user", func(e *Event) { e.UserID = 0 }}, + {"negative duration", func(e *Event) { e.DurationPlayedMs = -1 }}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + store := &fakeStore{} + e := valid + tt.mutate(&e) + if _, err := NewService(store).Record(context.Background(), e); !errors.Is(err, ErrInvalidEvent) { + t.Fatalf("err = %v, want ErrInvalidEvent", err) + } + if len(store.got) != 0 { + t.Error("invalid event reached the store") + } + }) + } +} + +func TestRecordDefaultsTimestamp(t *testing.T) { + store := &fakeStore{created: true} + now := time.Unix(1_700_000_000, 0) + s := NewService(store) + s.now = func() time.Time { return now } + + created, err := s.Record(context.Background(), Event{UserID: 1, TrackID: 2, Type: Complete, IdempotencyKey: "k"}) + if err != nil || !created { + t.Fatalf("created=%v err=%v", created, err) + } + if !store.got[0].OccurredAt.Equal(now) { + t.Errorf("OccurredAt = %v, want %v", store.got[0].OccurredAt, now) + } +} + +func TestRecordKeepsClientTimestamp(t *testing.T) { + store := &fakeStore{created: true} + ts := time.Unix(1_600_000_000, 0) + _, err := NewService(store).Record(context.Background(), + Event{UserID: 1, TrackID: 2, Type: Skip, IdempotencyKey: "k", OccurredAt: ts}) + if err != nil { + t.Fatal(err) + } + if !store.got[0].OccurredAt.Equal(ts) { + t.Errorf("OccurredAt = %v, want %v", store.got[0].OccurredAt, ts) + } +} + +func TestRecordPropagatesStoreError(t *testing.T) { + store := &fakeStore{err: ErrTrackNotFound} + _, err := NewService(store).Record(context.Background(), + Event{UserID: 1, TrackID: 2, Type: Play, IdempotencyKey: "k"}) + if !errors.Is(err, ErrTrackNotFound) { + t.Fatalf("err = %v, want ErrTrackNotFound", err) + } +} diff --git a/internal/storage/listening.go b/internal/storage/listening.go new file mode 100644 index 0000000..d8d7dd8 --- /dev/null +++ b/internal/storage/listening.go @@ -0,0 +1,66 @@ +package storage + +import ( + "context" + "fmt" + + "github.com/uptrace/bun" + + "codeberg.org/squidd/sonda/internal/listening" +) + +type listeningRow struct { + bun.BaseModel `bun:"table:listening_events"` + + ID int64 `bun:"id,pk,autoincrement"` + UserID int64 `bun:"user_id,notnull"` + TrackID int64 `bun:"track_id,notnull"` + EventType string `bun:"event_type,notnull"` + OccurredAt int64 `bun:"occurred_at,notnull"` + DurationPlayedMs int64 `bun:"duration_played_ms,notnull"` + IdempotencyKey string `bun:"idempotency_key,notnull"` +} + +type trackRow struct { + bun.BaseModel `bun:"table:tracks"` + + ID int64 `bun:"id,pk"` +} + +// ListeningRepo stores listen events. It implements listening.Store. +type ListeningRepo struct{ db *bun.DB } + +// NewListeningRepo builds a ListeningRepo. +func NewListeningRepo(db *bun.DB) *ListeningRepo { return &ListeningRepo{db: db} } + +// Record inserts e. A repeated (user, idempotency key) is ignored and reported +// as created=false. +func (r *ListeningRepo) Record(ctx context.Context, e listening.Event) (bool, error) { + exists, err := r.db.NewSelect().Model((*trackRow)(nil)).Where("id = ?", e.TrackID).Exists(ctx) + if err != nil { + return false, fmt.Errorf("check track: %w", err) + } + if !exists { + return false, listening.ErrTrackNotFound + } + + row := &listeningRow{ + UserID: e.UserID, + TrackID: e.TrackID, + EventType: string(e.Type), + OccurredAt: e.OccurredAt.Unix(), + DurationPlayedMs: e.DurationPlayedMs, + IdempotencyKey: e.IdempotencyKey, + } + res, err := r.db.NewInsert().Model(row). + On("CONFLICT (user_id, idempotency_key) DO NOTHING"). + Exec(ctx) + if err != nil { + return false, fmt.Errorf("insert listening event: %w", err) + } + n, err := res.RowsAffected() + if err != nil { + return false, fmt.Errorf("rows affected: %w", err) + } + return n > 0, nil +} diff --git a/internal/storage/repos_test.go b/internal/storage/repos_test.go new file mode 100644 index 0000000..b2ebac9 --- /dev/null +++ b/internal/storage/repos_test.go @@ -0,0 +1,127 @@ +package storage + +import ( + "context" + "errors" + "testing" + "time" + + "github.com/uptrace/bun" + + "codeberg.org/squidd/sonda/internal/auth" + "codeberg.org/squidd/sonda/internal/listening" +) + +func TestUserRepo(t *testing.T) { + forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { + ctx := context.Background() + repo := NewUserRepo(db) + + n, err := repo.CountUsers(ctx) + if err != nil || n != 0 { + t.Fatalf("CountUsers = %d, %v", n, err) + } + + u, err := repo.CreateUser(ctx, "Alice", "hash1") + if err != nil { + t.Fatal(err) + } + if u.ID <= 0 || u.Username != "Alice" || u.CreatedAt.IsZero() { + t.Errorf("user = %+v", u) + } + + // Lookup is case-insensitive and returns the stored hash. + got, hash, err := repo.UserByUsername(ctx, "alice") + if err != nil { + t.Fatal(err) + } + if got.ID != u.ID || hash != "hash1" { + t.Errorf("got %+v hash %q", got, hash) + } + + // Same name in another case is a duplicate. + if _, err := repo.CreateUser(ctx, "ALICE", "hash2"); !errors.Is(err, auth.ErrUsernameTaken) { + t.Errorf("duplicate: err = %v, want ErrUsernameTaken", err) + } + + if _, _, err := repo.UserByUsername(ctx, "nobody"); !errors.Is(err, auth.ErrUserNotFound) { + t.Errorf("missing: err = %v, want ErrUserNotFound", err) + } + + if n, _ := repo.CountUsers(ctx); n != 1 { + t.Errorf("CountUsers = %d, want 1", n) + } + }) +} + +func TestListeningRepo(t *testing.T) { + forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { + ctx := context.Background() + users := NewUserRepo(db) + repo := NewListeningRepo(db) + + u1, err := users.CreateUser(ctx, "alice", "h") + if err != nil { + t.Fatal(err) + } + u2, err := users.CreateUser(ctx, "bob", "h") + if err != nil { + t.Fatal(err) + } + var trackID int64 + if err := db.QueryRow("INSERT INTO tracks (title) VALUES ('song') RETURNING id").Scan(&trackID); err != nil { + t.Fatal(err) + } + + ev := listening.Event{ + UserID: u1.ID, TrackID: trackID, Type: listening.Complete, + OccurredAt: time.Unix(1_700_000_000, 0), DurationPlayedMs: 180000, IdempotencyKey: "k1", + } + + created, err := repo.Record(ctx, ev) + if err != nil || !created { + t.Fatalf("first Record: created=%v err=%v", created, err) + } + + // Retry with the same key is a no-op. + created, err = repo.Record(ctx, ev) + if err != nil || created { + t.Fatalf("retry: created=%v err=%v, want false/nil", created, err) + } + + // Same key from another user is a different event. + ev2 := ev + ev2.UserID = u2.ID + if created, err = repo.Record(ctx, ev2); err != nil || !created { + t.Fatalf("other user: created=%v err=%v", created, err) + } + + var count int + if err := db.QueryRow("SELECT COUNT(*) FROM listening_events").Scan(&count); err != nil { + t.Fatal(err) + } + if count != 2 { + t.Errorf("rows = %d, want 2", count) + } + + var typ string + var dur, at int64 + err = db.QueryRow( + "SELECT event_type, duration_played_ms, occurred_at FROM listening_events WHERE user_id = ?", u1.ID). + Scan(&typ, &dur, &at) + if err != nil { + t.Fatal(err) + } + if typ != "complete" || dur != 180000 || at != 1_700_000_000 { + t.Errorf("stored = %q %d %d", typ, dur, at) + } + + // Unknown track. + bad := ev + bad.TrackID = trackID + 1000 + bad.IdempotencyKey = "k2" + if _, err := repo.Record(ctx, bad); !errors.Is(err, listening.ErrTrackNotFound) { + t.Errorf("unknown track: err = %v, want ErrTrackNotFound", err) + } + }) +} diff --git a/internal/storage/storage.go b/internal/storage/storage.go new file mode 100644 index 0000000..9686f87 --- /dev/null +++ b/internal/storage/storage.go @@ -0,0 +1,145 @@ +// Package storage opens the database (SQLite or PostgreSQL) and applies schema +// migrations. Repositories live here too. +// +// This is the only place that should import the ORM (bun) and the database +// drivers. Services depend on small interfaces, never on this package directly. +package storage + +import ( + "context" + "database/sql" + "fmt" + "io/fs" + "strings" + "time" + + _ "github.com/jackc/pgx/v5/stdlib" // PostgreSQL driver ("pgx") + "github.com/uptrace/bun" + "github.com/uptrace/bun/dialect/pgdialect" + "github.com/uptrace/bun/dialect/sqlitedialect" + _ "modernc.org/sqlite" // pure-Go SQLite driver ("sqlite") +) + +// Supported database drivers. +const ( + DriverSQLite = "sqlite" + DriverPostgres = "postgres" +) + +// pathEscaper protects characters that are special in a file: URI. +var pathEscaper = strings.NewReplacer("%", "%25", "?", "%3F", "#", "%23") + +// Open connects to the database. For "sqlite" target is a file path (created if +// needed); for "postgres" it is a connection string (DSN or URL). +func Open(driver, target string) (*bun.DB, error) { + switch driver { + case DriverSQLite: + return openSQLite(target) + case DriverPostgres: + return openPostgres(target) + default: + return nil, fmt.Errorf("unknown database driver %q", driver) + } +} + +// openSQLite enables WAL, foreign keys and a busy timeout on every connection. +func openSQLite(path string) (*bun.DB, error) { + dsn := "file:" + pathEscaper.Replace(path) + + "?_pragma=foreign_keys(1)&_pragma=journal_mode(WAL)&_pragma=busy_timeout(5000)" + + sqldb, err := sql.Open("sqlite", dsn) + if err != nil { + return nil, fmt.Errorf("open sqlite: %w", err) + } + if err := sqldb.Ping(); err != nil { + _ = sqldb.Close() + return nil, fmt.Errorf("ping sqlite: %w", err) + } + return bun.NewDB(sqldb, sqlitedialect.New()), nil +} + +func openPostgres(dsn string) (*bun.DB, error) { + sqldb, err := sql.Open("pgx", dsn) + if err != nil { + return nil, fmt.Errorf("open postgres: %w", err) + } + if err := sqldb.Ping(); err != nil { + _ = sqldb.Close() + return nil, fmt.Errorf("ping postgres: %w", err) + } + return bun.NewDB(sqldb, pgdialect.New()), nil +} + +// Migrate applies, in lexical order, every *.sql file in fsys that has not +// been applied yet. Each file runs in its own transaction. +func Migrate(ctx context.Context, db *bun.DB, fsys fs.FS) error { + _, err := db.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS schema_migrations ( + version TEXT PRIMARY KEY, + applied_at BIGINT NOT NULL + )`) + if err != nil { + return fmt.Errorf("create schema_migrations: %w", err) + } + + applied, err := appliedVersions(ctx, db) + if err != nil { + return err + } + + entries, err := fs.ReadDir(fsys, ".") + if err != nil { + return fmt.Errorf("read migrations: %w", err) + } + for _, e := range entries { + name := e.Name() + if e.IsDir() || !strings.HasSuffix(name, ".sql") || applied[name] { + continue + } + if err := applyOne(ctx, db, fsys, name); err != nil { + return fmt.Errorf("migration %s: %w", name, err) + } + } + return nil +} + +func appliedVersions(ctx context.Context, db *bun.DB) (map[string]bool, error) { + rows, err := db.QueryContext(ctx, "SELECT version FROM schema_migrations") + if err != nil { + return nil, fmt.Errorf("list applied migrations: %w", err) + } + defer rows.Close() + + applied := map[string]bool{} + for rows.Next() { + var v string + if err := rows.Scan(&v); err != nil { + return nil, err + } + applied[v] = true + } + return applied, rows.Err() +} + +func applyOne(ctx context.Context, db *bun.DB, fsys fs.FS, name string) error { + body, err := fs.ReadFile(fsys, name) + if err != nil { + return err + } + tx, err := db.BeginTx(ctx, nil) + if err != nil { + return err + } + defer func() { _ = tx.Rollback() }() // no-op after a successful Commit + + // The file body goes straight to the driver: it may contain several + // statements and must not go through placeholder formatting. + if _, err := tx.Tx.ExecContext(ctx, string(body)); err != nil { + return err + } + _, err = tx.ExecContext(ctx, + "INSERT INTO schema_migrations (version, applied_at) VALUES (?, ?)", name, time.Now().Unix()) + if err != nil { + return err + } + return tx.Commit() +} diff --git a/internal/storage/storage_test.go b/internal/storage/storage_test.go new file mode 100644 index 0000000..af3d006 --- /dev/null +++ b/internal/storage/storage_test.go @@ -0,0 +1,269 @@ +package storage + +import ( + "context" + "fmt" + "io/fs" + "net/url" + "os" + "path/filepath" + "strings" + "sync/atomic" + "testing" + "testing/fstest" + + "github.com/uptrace/bun" + + "codeberg.org/squidd/sonda/migrations" +) + +// postgresDSNEnv points the tests at a throwaway PostgreSQL server, e.g. +// postgres://postgres:sonda@127.0.0.1:55432/sonda?sslmode=disable +// Without it only the SQLite tests run, so the default suite needs no network. +const postgresDSNEnv = "SONDA_TEST_POSTGRES_DSN" + +var schemaCounter atomic.Int64 + +// openBackend opens a fresh, empty database for driver. Postgres tests get +// their own schema, dropped when the test ends. +func openBackend(t *testing.T, driver string) *bun.DB { + t.Helper() + switch driver { + case DriverSQLite: + db, err := Open(DriverSQLite, filepath.Join(t.TempDir(), "test.db")) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = db.Close() }) + return db + + case DriverPostgres: + admin, err := Open(DriverPostgres, os.Getenv(postgresDSNEnv)) + if err != nil { + t.Fatal(err) + } + schema := fmt.Sprintf("t_%d_%d", os.Getpid(), schemaCounter.Add(1)) + if _, err := admin.Exec("CREATE SCHEMA " + schema); err != nil { + t.Fatal(err) + } + u, err := url.Parse(os.Getenv(postgresDSNEnv)) + if err != nil { + t.Fatal(err) + } + q := u.Query() + q.Set("search_path", schema) + u.RawQuery = q.Encode() + db, err := Open(DriverPostgres, u.String()) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { + _ = db.Close() + _, _ = admin.Exec("DROP SCHEMA " + schema + " CASCADE") + _ = admin.Close() + }) + return db + } + t.Fatalf("unknown driver %q", driver) + return nil +} + +// forEachBackend runs fn once per available backend with a migrated database. +func forEachBackend(t *testing.T, fn func(t *testing.T, db *bun.DB, driver string)) { + t.Helper() + drivers := []string{DriverSQLite} + if os.Getenv(postgresDSNEnv) != "" { + drivers = append(drivers, DriverPostgres) + } + for _, driver := range drivers { + t.Run(driver, func(t *testing.T) { + db := openBackend(t, driver) + fsys, err := migrations.For(driver) + if err != nil { + t.Fatal(err) + } + if err := Migrate(context.Background(), db, fsys); err != nil { + t.Fatal(err) + } + fn(t, db, driver) + }) + } +} + +func tableExists(t *testing.T, db *bun.DB, driver, table string) bool { + t.Helper() + var n int + var err error + if driver == DriverPostgres { + err = db.QueryRow( + "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = current_schema() AND table_name = ?", + table).Scan(&n) + } else { + err = db.QueryRow("SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name = ?", table).Scan(&n) + } + if err != nil { + t.Fatal(err) + } + return n == 1 +} + +func TestOpenRejectsUnknownDriver(t *testing.T) { + if _, err := Open("mysql", "x"); err == nil { + t.Fatal("expected error for unknown driver") + } +} + +func TestSQLiteOpenSetsPragmas(t *testing.T) { + db := openBackend(t, DriverSQLite) + + var fk int + if err := db.QueryRow("PRAGMA foreign_keys").Scan(&fk); err != nil { + t.Fatal(err) + } + if fk != 1 { + t.Errorf("foreign_keys = %d, want 1", fk) + } + + var mode string + if err := db.QueryRow("PRAGMA journal_mode").Scan(&mode); err != nil { + t.Fatal(err) + } + if mode != "wal" { + t.Errorf("journal_mode = %q, want wal", mode) + } + + var busy int + if err := db.QueryRow("PRAGMA busy_timeout").Scan(&busy); err != nil { + t.Fatal(err) + } + if busy <= 0 { + t.Errorf("busy_timeout = %d, want > 0", busy) + } +} + +func TestMigrateCreatesSchemaAndIsIdempotent(t *testing.T) { + forEachBackend(t, func(t *testing.T, db *bun.DB, driver string) { + fsys, _ := migrations.For(driver) + if err := Migrate(context.Background(), db, fsys); err != nil { + t.Fatalf("second Migrate: %v", err) + } + for _, table := range []string{"artists", "albums", "tracks", "track_artists", "audio_files", "users", "listening_events"} { + if !tableExists(t, db, driver, table) { + t.Errorf("table %s missing", table) + } + } + var applied int + if err := db.QueryRow("SELECT COUNT(*) FROM schema_migrations").Scan(&applied); err != nil { + t.Fatal(err) + } + if applied != 2 { + t.Errorf("applied migrations = %d, want 2", applied) + } + }) +} + +func TestSchemaEnforcesForeignKeys(t *testing.T) { + forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { + if _, err := db.Exec("INSERT INTO tracks (title, album_id) VALUES ('x', 999)"); err == nil { + t.Fatal("expected foreign key violation") + } + }) +} + +func TestSchemaAllowsUnknownMBIDsButRejectsDuplicates(t *testing.T) { + forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { + // Several artists without an MBID must be allowed. + for _, name := range []string{"A", "B"} { + if _, err := db.Exec("INSERT INTO artists (name) VALUES (?)", name); err != nil { + t.Fatalf("insert %s: %v", name, err) + } + } + // The same MBID twice must not. + if _, err := db.Exec("INSERT INTO artists (name, mbid) VALUES ('C', 'm1')"); err != nil { + t.Fatal(err) + } + if _, err := db.Exec("INSERT INTO artists (name, mbid) VALUES ('D', 'm1')"); err == nil { + t.Fatal("expected unique violation on mbid") + } + }) +} + +func TestAudioFilePathIsUniquePerRoot(t *testing.T) { + forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { + if _, err := db.Exec("INSERT INTO tracks (title) VALUES ('t')"); err != nil { + t.Fatal(err) + } + insert := func(root, rel string) error { + _, err := db.Exec( + `INSERT INTO audio_files (track_id, root, rel_path, format, size_bytes, mtime_unix) + VALUES (1, ?, ?, 'mp3', 1, 1)`, root, rel) + return err + } + + if err := insert("/music/a", "x/y.mp3"); err != nil { + t.Fatal(err) + } + // Same relative path under another root is a different file. + if err := insert("/music/b", "x/y.mp3"); err != nil { + t.Fatalf("same rel_path in another root should be allowed: %v", err) + } + // Same root and same relative path is a duplicate. + if err := insert("/music/a", "x/y.mp3"); err == nil { + t.Fatal("expected unique violation on (root, rel_path)") + } + }) +} + +func TestMigrateRollsBackFailedMigration(t *testing.T) { + for _, driver := range availableDrivers() { + t.Run(driver, func(t *testing.T) { + db := openBackend(t, driver) + fsys := fstest.MapFS{ + "0001_ok.sql": {Data: []byte("CREATE TABLE a (id INTEGER);")}, + "0002_bad.sql": {Data: []byte("CREATE TABLE b (id INTEGER); THIS IS NOT SQL;")}, + } + if err := Migrate(context.Background(), db, fsys); err == nil { + t.Fatal("expected error from bad migration") + } + if tableExists(t, db, driver, "b") { + t.Error("table b exists: failed migration was not rolled back") + } + var n int + if err := db.QueryRow("SELECT COUNT(*) FROM schema_migrations").Scan(&n); err != nil { + t.Fatal(err) + } + if n != 1 { + t.Errorf("applied = %d, want 1 (only 0001)", n) + } + }) + } +} + +func availableDrivers() []string { + drivers := []string{DriverSQLite} + if os.Getenv(postgresDSNEnv) != "" { + drivers = append(drivers, DriverPostgres) + } + return drivers +} + +func TestMigrationFoldersMatch(t *testing.T) { + names := func(driver string) string { + fsys, err := migrations.For(driver) + if err != nil { + t.Fatal(err) + } + entries, err := fs.ReadDir(fsys, ".") + if err != nil { + t.Fatal(err) + } + var out []string + for _, e := range entries { + out = append(out, e.Name()) + } + return strings.Join(out, ",") + } + if a, b := names(DriverSQLite), names(DriverPostgres); a != b { + t.Errorf("migration files differ: sqlite=%s postgres=%s", a, b) + } +} diff --git a/internal/storage/users.go b/internal/storage/users.go new file mode 100644 index 0000000..0fb2491 --- /dev/null +++ b/internal/storage/users.go @@ -0,0 +1,80 @@ +package storage + +import ( + "context" + "database/sql" + "errors" + "fmt" + "time" + + "github.com/jackc/pgx/v5/pgconn" + "github.com/uptrace/bun" + msqlite "modernc.org/sqlite" + + "codeberg.org/squidd/sonda/internal/auth" +) + +// sqliteConstraintUnique is the extended result code SQLITE_CONSTRAINT_UNIQUE. +const sqliteConstraintUnique = 2067 + +type userRow struct { + bun.BaseModel `bun:"table:users"` + + ID int64 `bun:"id,pk,autoincrement"` + Username string `bun:"username,notnull"` + PasswordHash string `bun:"password_hash,notnull"` + CreatedAt int64 `bun:"created_at,notnull"` +} + +func (r userRow) toUser() auth.User { + return auth.User{ID: r.ID, Username: r.Username, CreatedAt: time.Unix(r.CreatedAt, 0)} +} + +// UserRepo stores users. It implements auth.UserStore. +type UserRepo struct{ db *bun.DB } + +// NewUserRepo builds a UserRepo. +func NewUserRepo(db *bun.DB) *UserRepo { return &UserRepo{db: db} } + +// CountUsers returns how many users exist. +func (r *UserRepo) CountUsers(ctx context.Context) (int, error) { + n, err := r.db.NewSelect().Model((*userRow)(nil)).Count(ctx) + return int(n), err +} + +// CreateUser inserts a user. Usernames are unique case-insensitively. +func (r *UserRepo) CreateUser(ctx context.Context, username, passwordHash string) (auth.User, error) { + row := &userRow{Username: username, PasswordHash: passwordHash, CreatedAt: time.Now().Unix()} + if _, err := r.db.NewInsert().Model(row).Exec(ctx); err != nil { + if isUniqueViolation(err) { + return auth.User{}, auth.ErrUsernameTaken + } + return auth.User{}, fmt.Errorf("insert user: %w", err) + } + return row.toUser(), nil +} + +// UserByUsername finds a user (case-insensitive) and returns their password hash. +func (r *UserRepo) UserByUsername(ctx context.Context, username string) (auth.User, string, error) { + var row userRow + err := r.db.NewSelect().Model(&row).Where("lower(username) = lower(?)", username).Limit(1).Scan(ctx) + if errors.Is(err, sql.ErrNoRows) { + return auth.User{}, "", auth.ErrUserNotFound + } + if err != nil { + return auth.User{}, "", fmt.Errorf("select user: %w", err) + } + return row.toUser(), row.PasswordHash, nil +} + +// postgresUniqueViolation is the SQLSTATE for unique_violation. +const postgresUniqueViolation = "23505" + +func isUniqueViolation(err error) bool { + var se *msqlite.Error + if errors.As(err, &se) { + return se.Code() == sqliteConstraintUnique + } + var pe *pgconn.PgError + return errors.As(err, &pe) && pe.Code == postgresUniqueViolation +} diff --git a/migrations/embed.go b/migrations/embed.go new file mode 100644 index 0000000..5090cdf --- /dev/null +++ b/migrations/embed.go @@ -0,0 +1,29 @@ +// Package migrations embeds the versioned SQL migrations, one folder per +// database driver. Both folders must describe the same schema. +// +// Files are named NNNN_description.sql and applied in lexical order. +package migrations + +import ( + "embed" + "fmt" + "io/fs" +) + +//go:embed sqlite/*.sql +var sqliteFS embed.FS + +//go:embed postgres/*.sql +var postgresFS embed.FS + +// For returns the migrations for driver ("sqlite" or "postgres"). +func For(driver string) (fs.FS, error) { + switch driver { + case "sqlite": + return fs.Sub(sqliteFS, "sqlite") + case "postgres": + return fs.Sub(postgresFS, "postgres") + default: + return nil, fmt.Errorf("unknown database driver %q", driver) + } +} diff --git a/migrations/postgres/0001_init.sql b/migrations/postgres/0001_init.sql new file mode 100644 index 0000000..44cea24 --- /dev/null +++ b/migrations/postgres/0001_init.sql @@ -0,0 +1,55 @@ +-- Core catalog for local music (M1). PostgreSQL flavour of sqlite/0001_init.sql. +-- Keep both files in step: same tables, same columns, same meaning. + +CREATE TABLE artists ( + id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, + mbid TEXT UNIQUE, -- MusicBrainz artist id, NULL when unknown + name TEXT NOT NULL, + tags TEXT NOT NULL DEFAULT '[]' -- JSON array +); + +CREATE TABLE albums ( + id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, + mbid TEXT UNIQUE, -- MusicBrainz release-group id, NULL when unknown + title TEXT NOT NULL, + artist_id BIGINT REFERENCES artists(id) +); + +CREATE TABLE tracks ( + id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, + title TEXT NOT NULL, + duration_ms BIGINT, + track_no INTEGER, + disc_no INTEGER, + album_id BIGINT REFERENCES albums(id), + recording_mbid TEXT UNIQUE -- MusicBrainz recording id, NULL when unknown +); + +CREATE TABLE track_artists ( + track_id BIGINT NOT NULL REFERENCES tracks(id) ON DELETE CASCADE, + artist_id BIGINT NOT NULL REFERENCES artists(id) ON DELETE CASCADE, + role TEXT NOT NULL DEFAULT 'main', + PRIMARY KEY (track_id, artist_id, role) +); + +-- Physical files, kept separate from tracks so several encodings can exist. +-- Missing files are marked, not deleted, so history survives a rescan. +CREATE TABLE audio_files ( + id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, + track_id BIGINT NOT NULL REFERENCES tracks(id) ON DELETE CASCADE, + root TEXT NOT NULL, -- configured library root the file belongs to + rel_path TEXT NOT NULL, -- relative to that root + format TEXT NOT NULL, + size_bytes BIGINT NOT NULL, + mtime_unix BIGINT NOT NULL, + sample_rate INTEGER, + bitrate INTEGER, + content_hash TEXT, + missing_at BIGINT, -- unix time the file was last seen missing + UNIQUE (root, rel_path) +); + +CREATE INDEX idx_albums_artist ON albums(artist_id); +CREATE INDEX idx_tracks_album ON tracks(album_id); +CREATE INDEX idx_track_artists_artist ON track_artists(artist_id); +CREATE INDEX idx_audio_files_track ON audio_files(track_id); diff --git a/migrations/postgres/0002_users_listening.sql b/migrations/postgres/0002_users_listening.sql new file mode 100644 index 0000000..7546a15 --- /dev/null +++ b/migrations/postgres/0002_users_listening.sql @@ -0,0 +1,26 @@ +-- Local users and the listening history. PostgreSQL flavour of sqlite/0002_users_listening.sql. + +CREATE TABLE users ( + id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, + username TEXT NOT NULL, + password_hash TEXT NOT NULL, -- Argon2id PHC string + created_at BIGINT NOT NULL -- unix seconds +); + +-- Case-insensitive uniqueness (SQLite uses COLLATE NOCASE for the same effect). +CREATE UNIQUE INDEX users_username_lower ON users (lower(username)); + +-- Explicit listen events only. Opening or prefetching an audio URL is never recorded. +CREATE TABLE listening_events ( + id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, + user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE, + track_id BIGINT NOT NULL REFERENCES tracks(id) ON DELETE CASCADE, + event_type TEXT NOT NULL CHECK (event_type IN ('play', 'complete', 'skip')), + occurred_at BIGINT NOT NULL, -- unix seconds, client or server time + duration_played_ms BIGINT NOT NULL DEFAULT 0 CHECK (duration_played_ms >= 0), + idempotency_key TEXT NOT NULL, -- client generated, makes retries safe + UNIQUE (user_id, idempotency_key) +); + +CREATE INDEX idx_listening_user_time ON listening_events(user_id, occurred_at); +CREATE INDEX idx_listening_track ON listening_events(track_id); diff --git a/migrations/sqlite/0001_init.sql b/migrations/sqlite/0001_init.sql new file mode 100644 index 0000000..5cb0bfe --- /dev/null +++ b/migrations/sqlite/0001_init.sql @@ -0,0 +1,55 @@ +-- Core catalog for local music (M1). +-- Internal ids are authoritative; MusicBrainz ids are optional and never invented. + +CREATE TABLE artists ( + id INTEGER PRIMARY KEY, + mbid TEXT UNIQUE, -- MusicBrainz artist id, NULL when unknown + name TEXT NOT NULL, + tags TEXT NOT NULL DEFAULT '[]' -- JSON array +); + +CREATE TABLE albums ( + id INTEGER PRIMARY KEY, + mbid TEXT UNIQUE, -- MusicBrainz release-group id, NULL when unknown + title TEXT NOT NULL, + artist_id INTEGER REFERENCES artists(id) +); + +CREATE TABLE tracks ( + id INTEGER PRIMARY KEY, + title TEXT NOT NULL, + duration_ms INTEGER, + track_no INTEGER, + disc_no INTEGER, + album_id INTEGER REFERENCES albums(id), + recording_mbid TEXT UNIQUE -- MusicBrainz recording id, NULL when unknown +); + +CREATE TABLE track_artists ( + track_id INTEGER NOT NULL REFERENCES tracks(id) ON DELETE CASCADE, + artist_id INTEGER NOT NULL REFERENCES artists(id) ON DELETE CASCADE, + role TEXT NOT NULL DEFAULT 'main', + PRIMARY KEY (track_id, artist_id, role) +); + +-- Physical files, kept separate from tracks so several encodings can exist. +-- Missing files are marked, not deleted, so history survives a rescan. +CREATE TABLE audio_files ( + id INTEGER PRIMARY KEY, + track_id INTEGER NOT NULL REFERENCES tracks(id) ON DELETE CASCADE, + root TEXT NOT NULL, -- configured library root the file belongs to + rel_path TEXT NOT NULL, -- relative to that root + format TEXT NOT NULL, + size_bytes INTEGER NOT NULL, + mtime_unix INTEGER NOT NULL, + sample_rate INTEGER, + bitrate INTEGER, + content_hash TEXT, + missing_at INTEGER, -- unix time the file was last seen missing + UNIQUE (root, rel_path) +); + +CREATE INDEX idx_albums_artist ON albums(artist_id); +CREATE INDEX idx_tracks_album ON tracks(album_id); +CREATE INDEX idx_track_artists_artist ON track_artists(artist_id); +CREATE INDEX idx_audio_files_track ON audio_files(track_id); diff --git a/migrations/sqlite/0002_users_listening.sql b/migrations/sqlite/0002_users_listening.sql new file mode 100644 index 0000000..fac020b --- /dev/null +++ b/migrations/sqlite/0002_users_listening.sql @@ -0,0 +1,23 @@ +-- Local users and the listening history used for recommendations. + +CREATE TABLE users ( + id INTEGER PRIMARY KEY, + username TEXT NOT NULL UNIQUE COLLATE NOCASE, + password_hash TEXT NOT NULL, -- Argon2id PHC string + created_at INTEGER NOT NULL -- unix seconds +); + +-- Explicit listen events only. Opening or prefetching an audio URL is never recorded. +CREATE TABLE listening_events ( + id INTEGER PRIMARY KEY, + user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE, + track_id INTEGER NOT NULL REFERENCES tracks(id) ON DELETE CASCADE, + event_type TEXT NOT NULL CHECK (event_type IN ('play', 'complete', 'skip')), + occurred_at INTEGER NOT NULL, -- unix seconds, client or server time + duration_played_ms INTEGER NOT NULL DEFAULT 0 CHECK (duration_played_ms >= 0), + idempotency_key TEXT NOT NULL, -- client generated, makes retries safe + UNIQUE (user_id, idempotency_key) +); + +CREATE INDEX idx_listening_user_time ON listening_events(user_id, occurred_at); +CREATE INDEX idx_listening_track ON listening_events(track_id); diff --git a/sonda.example.toml b/sonda.example.toml new file mode 100644 index 0000000..56375dd --- /dev/null +++ b/sonda.example.toml @@ -0,0 +1,25 @@ +# Copy to sonda.toml and adjust. sonda.toml is git-ignored. +# Precedence: defaults < this file < environment variables +# (SONDA_ENV, SONDA_LISTEN, SONDA_DB_DRIVER, SONDA_DB_PATH, SONDA_DB_DSN, +# SONDA_JWT_SECRET, SONDA_LASTFM_API_KEY). + +[server] +listen = "127.0.0.1:8080" # localhost by default + +[library] +roots = ["/path/to/music"] # read-only, at least one required + +[database] +driver = "sqlite" # "sqlite" or "postgres" +path = "sonda.db" # sqlite only +dsn = "" # postgres only, e.g. "postgres://user:pass@localhost:5432/sonda?sslmode=disable" + +[log] +env = "production" # "dev" or "production" + +[auth] +jwt_secret = "" # required, 32+ chars: openssl rand -hex 32 +token_ttl_hours = 168 # one week + +[providers.lastfm] +api_key = ""