mirror of
https://github.com/riwiwa/muzi.git
synced 2026-03-04 00:51:59 -08:00
fix now-playing status on listenbrainz endpoint and add config support
This commit is contained in:
9
config.toml
Normal file
9
config.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[server]
|
||||
address = "0.0.0.0:1234"
|
||||
|
||||
[database]
|
||||
host = "localhost"
|
||||
port = "5432"
|
||||
user = "postgres"
|
||||
password = "postgres"
|
||||
name = "muzi"
|
||||
71
config/config.go
Normal file
71
config/config.go
Normal file
@@ -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)
|
||||
}
|
||||
14
db/db.go
14
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 {
|
||||
|
||||
1
go.mod
1
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
|
||||
|
||||
2
go.sum
2
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=
|
||||
|
||||
8
main.go
8
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()
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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"))))
|
||||
|
||||
Reference in New Issue
Block a user