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:
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)
|
||||
}
|
||||
Reference in New Issue
Block a user