2025-01-21 14:53:18 +00:00
|
|
|
package controller
|
2024-11-10 05:34:04 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
|
2025-01-21 14:53:18 +00:00
|
|
|
"arimelody-web/model"
|
2024-11-10 05:34:04 +00:00
|
|
|
|
2025-01-21 14:53:18 +00:00
|
|
|
"github.com/pelletier/go-toml/v2"
|
2024-11-10 05:34:04 +00:00
|
|
|
)
|
|
|
|
|
2025-01-21 14:53:18 +00:00
|
|
|
func GetConfig() model.Config {
|
2024-11-10 05:34:04 +00:00
|
|
|
configFile := os.Getenv("ARIMELODY_CONFIG")
|
|
|
|
if configFile == "" {
|
|
|
|
configFile = "config.toml"
|
|
|
|
}
|
|
|
|
|
2025-01-21 14:53:18 +00:00
|
|
|
config := model.Config{
|
2024-11-10 05:34:04 +00:00
|
|
|
BaseUrl: "https://arimelody.me",
|
2025-01-21 17:13:06 +00:00
|
|
|
Host: "0.0.0.0",
|
2024-11-10 05:34:04 +00:00
|
|
|
Port: 8080,
|
2025-01-21 14:53:18 +00:00
|
|
|
DB: model.DBConfig{
|
2025-01-21 14:12:21 +00:00
|
|
|
Host: "127.0.0.1",
|
|
|
|
Port: 5432,
|
|
|
|
User: "arimelody",
|
|
|
|
Name: "arimelody",
|
|
|
|
},
|
2024-11-10 05:34:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
data, err := os.ReadFile(configFile)
|
|
|
|
if err != nil {
|
|
|
|
configOut, _ := toml.Marshal(&config)
|
|
|
|
os.WriteFile(configFile, configOut, os.ModePerm)
|
|
|
|
fmt.Printf(
|
|
|
|
"A default config.toml has been created. " +
|
|
|
|
"Please configure before running again!\n")
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = toml.Unmarshal([]byte(data), &config)
|
|
|
|
if err != nil {
|
2025-01-21 14:53:18 +00:00
|
|
|
panic(fmt.Sprintf("FATAL: Failed to parse configuration file: %v\n", err))
|
2024-11-10 05:34:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = handleConfigOverrides(&config)
|
|
|
|
if err != nil {
|
2025-01-21 14:53:18 +00:00
|
|
|
panic(fmt.Sprintf("FATAL: Failed to parse environment variable %v\n", err))
|
2024-11-10 05:34:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return config
|
2025-01-21 14:53:18 +00:00
|
|
|
}
|
2024-11-10 05:34:04 +00:00
|
|
|
|
2025-01-21 14:53:18 +00:00
|
|
|
func handleConfigOverrides(config *model.Config) error {
|
2024-11-10 05:34:04 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
if env, has := os.LookupEnv("ARIMELODY_BASE_URL"); has { config.BaseUrl = env }
|
2025-01-21 17:13:06 +00:00
|
|
|
if env, has := os.LookupEnv("ARIMELODY_HOST"); has { config.Host = env }
|
2024-11-10 05:34:04 +00:00
|
|
|
if env, has := os.LookupEnv("ARIMELODY_PORT"); has {
|
|
|
|
config.Port, err = strconv.ParseInt(env, 10, 0)
|
|
|
|
if err != nil { return errors.New("ARIMELODY_PORT: " + err.Error()) }
|
|
|
|
}
|
|
|
|
if env, has := os.LookupEnv("ARIMELODY_DATA_DIR"); has { config.DataDirectory = env }
|
|
|
|
|
|
|
|
if env, has := os.LookupEnv("ARIMELODY_DB_HOST"); has { config.DB.Host = env }
|
2025-01-21 14:12:21 +00:00
|
|
|
if env, has := os.LookupEnv("ARIMELODY_DB_PORT"); has {
|
|
|
|
config.DB.Port, err = strconv.ParseInt(env, 10, 0)
|
|
|
|
if err != nil { return errors.New("ARIMELODY_DB_PORT: " + err.Error()) }
|
|
|
|
}
|
2024-11-10 05:34:04 +00:00
|
|
|
if env, has := os.LookupEnv("ARIMELODY_DB_NAME"); has { config.DB.Name = env }
|
|
|
|
if env, has := os.LookupEnv("ARIMELODY_DB_USER"); has { config.DB.User = env }
|
|
|
|
if env, has := os.LookupEnv("ARIMELODY_DB_PASS"); has { config.DB.Pass = env }
|
|
|
|
|
|
|
|
if env, has := os.LookupEnv("ARIMELODY_DISCORD_ADMIN_ID"); has { config.Discord.AdminID = env }
|
|
|
|
if env, has := os.LookupEnv("ARIMELODY_DISCORD_CLIENT_ID"); has { config.Discord.ClientID = env }
|
|
|
|
if env, has := os.LookupEnv("ARIMELODY_DISCORD_SECRET"); has { config.Discord.Secret = env }
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|