From 7542eaf32115f5feb4d9c983dc9b1bc9af687731 Mon Sep 17 00:00:00 2001 From: riwiwa Date: Sat, 28 Feb 2026 16:08:46 -0800 Subject: [PATCH] fix now-playing status on listenbrainz endpoint and add config support --- config.toml | 9 ++++++ config/config.go | 71 ++++++++++++++++++++++++++++++++++++++++++++ db/db.go | 14 ++------- go.mod | 1 + go.sum | 2 ++ main.go | 8 ++++- scrobble/lastfm.go | 3 +- scrobble/scrobble.go | 4 +++ scrobble/spotify.go | 6 +--- web/web.go | 7 ++--- 10 files changed, 103 insertions(+), 22 deletions(-) create mode 100644 config.toml create mode 100644 config/config.go diff --git a/config.toml b/config.toml new file mode 100644 index 0000000..3e98b57 --- /dev/null +++ b/config.toml @@ -0,0 +1,9 @@ +[server] +address = "0.0.0.0:1234" + +[database] +host = "localhost" +port = "5432" +user = "postgres" +password = "postgres" +name = "muzi" diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..bb97fa9 --- /dev/null +++ b/config/config.go @@ -0,0 +1,71 @@ +package config + +import ( + "fmt" + "os" + + "github.com/BurntSushi/toml" +) + +type Config struct { + Server ServerConfig + Database DatabaseConfig +} + +type ServerConfig struct { + Address string +} + +type DatabaseConfig struct { + Host string + Port string + User string + Password string + Name string +} + +var cfg *Config + +func LoadConfig() (*Config, error) { + cfg = &Config{ + Server: ServerConfig{ + Address: "0.0.0.0:1234", + }, + Database: DatabaseConfig{ + Host: "localhost", + Port: "5432", + User: "postgres", + Password: "postgres", + Name: "muzi", + }, + } + + if _, err := os.Stat("config.toml"); err == nil { + _, err := toml.DecodeFile("config.toml", cfg) + if err != nil { + return nil, fmt.Errorf("error parsing config.toml: %w", err) + } + } + + return cfg, nil +} + +func Get() *Config { + if cfg == nil { + var err error + cfg, err = LoadConfig() + if err != nil { + panic(fmt.Sprintf("failed to load config: %v", err)) + } + } + return cfg +} + +func (d *DatabaseConfig) GetDbUrl(withDb bool) string { + if withDb { + return fmt.Sprintf("postgres://%s:%s@%s:%s/%s", + d.User, d.Password, d.Host, d.Port, d.Name) + } + return fmt.Sprintf("postgres://%s:%s@%s:%s", + d.User, d.Password, d.Host, d.Port) +} diff --git a/db/db.go b/db/db.go index cdb3e61..65023a8 100644 --- a/db/db.go +++ b/db/db.go @@ -5,6 +5,8 @@ import ( "fmt" "os" + "muzi/config" + "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgxpool" ) @@ -25,17 +27,7 @@ func CreateAllTables() error { } func GetDbUrl(dbName bool) string { - host := os.Getenv("PGHOST") - port := os.Getenv("PGPORT") - user := os.Getenv("PGUSER") - pass := os.Getenv("PGPASSWORD") - - if dbName { - return fmt.Sprintf("postgres://%s:%s@%s:%s/%s", - user, pass, host, port, "muzi") - } else { - return fmt.Sprintf("postgres://%s:%s@%s:%s", user, pass, host, port) - } + return config.Get().Database.GetDbUrl(dbName) } func CreateDB() error { diff --git a/go.mod b/go.mod index 44650d8..c451994 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,7 @@ require ( ) require ( + github.com/BurntSushi/toml v1.6.0 // indirect github.com/jackc/pgio v1.0.0 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect diff --git a/go.sum b/go.sum index 69d57a3..53d9e5b 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,6 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/BurntSushi/toml v1.6.0 h1:dRaEfpa2VI55EwlIW72hMRHdWouJeRF7TPYhI+AUQjk= +github.com/BurntSushi/toml v1.6.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs= github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ= github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= diff --git a/main.go b/main.go index b914dd0..7004446 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "fmt" "os" + "muzi/config" "muzi/db" "muzi/scrobble" "muzi/web" @@ -20,9 +21,14 @@ func check(msg string, err error) { } func main() { + _, err := config.LoadConfig() + if err != nil { + fmt.Fprintf(os.Stderr, "Error loading config: %v\n", err) + os.Exit(1) + } + check("ensuring muzi DB exists", db.CreateDB()) - var err error db.Pool, err = pgxpool.New(context.Background(), db.GetDbUrl(true)) check("connecting to muzi database", err) defer db.Pool.Close() diff --git a/scrobble/lastfm.go b/scrobble/lastfm.go index 2b2f79a..90fee54 100644 --- a/scrobble/lastfm.go +++ b/scrobble/lastfm.go @@ -119,7 +119,8 @@ func (h *LastFMHandler) handleHandshake(w http.ResponseWriter, r *http.Request) return } - w.Write([]byte(fmt.Sprintf("OK\n%s\nhttp://127.0.0.1:1234/2.0/\nhttp://127.0.0.1:1234/2.0/\n", sessionKey))) + baseURL := getBaseURL(r) + w.Write([]byte(fmt.Sprintf("OK\n%s\n%s/2.0/\n%s/2.0/\n", sessionKey, baseURL, baseURL))) } func (h *LastFMHandler) handleGetToken(w http.ResponseWriter, apiKey string) { diff --git a/scrobble/scrobble.go b/scrobble/scrobble.go index 366235d..7b45e8c 100644 --- a/scrobble/scrobble.go +++ b/scrobble/scrobble.go @@ -200,6 +200,10 @@ func GetNowPlaying(userId int) (NowPlaying, bool) { if ok && np.SongName != "" { return np, true } + np, ok = platforms["listenbrainz"] + if ok && np.SongName != "" { + return np, true + } return NowPlaying{}, false } diff --git a/scrobble/spotify.go b/scrobble/spotify.go index 363f1b5..7825c91 100644 --- a/scrobble/spotify.go +++ b/scrobble/spotify.go @@ -401,11 +401,7 @@ func getBaseURL(r *http.Request) string { if r.TLS != nil { scheme = "https" } - host := r.Host - if host == "localhost:1234" || host == "localhost" { - host = "127.0.0.1:1234" - } - return scheme + "://" + host + return scheme + "://" + r.Host } func GetSpotifyAuthURL(userId int, baseURL string) (string, error) { diff --git a/web/web.go b/web/web.go index 09a1414..eb4f3a8 100644 --- a/web/web.go +++ b/web/web.go @@ -9,6 +9,7 @@ import ( "net/http" "os" + "muzi/config" "muzi/db" "muzi/scrobble" @@ -16,13 +17,11 @@ import ( "github.com/go-chi/chi/v5/middleware" ) -const serverAddr = "127.0.0.1:1234" - // 50 MiB const maxHeaderSize int64 = 50 * 1024 * 1024 func serverAddrStr() string { - return serverAddr + return config.Get().Server.Address } // Holds all the parsed HTML templates @@ -74,7 +73,7 @@ func rootHandler() http.HandlerFunc { // Serves all pages at the specified address. func Start() { - addr := serverAddr + addr := config.Get().Server.Address r := chi.NewRouter() r.Use(middleware.Logger) r.Handle("/files/*", http.StripPrefix("/files", http.FileServer(http.Dir("./static"))))