2023-09-18 23:57:18 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2023-09-19 23:29:55 +00:00
|
|
|
"strings"
|
2023-09-18 23:57:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2023-09-19 23:29:55 +00:00
|
|
|
// Server Config
|
|
|
|
Version string
|
|
|
|
ListenPort string
|
|
|
|
|
|
|
|
// DB Configuration
|
2023-09-18 23:57:18 +00:00
|
|
|
DBType string
|
|
|
|
DBName string
|
|
|
|
DBPassword string
|
2023-09-19 23:29:55 +00:00
|
|
|
|
|
|
|
// Data Paths
|
2023-09-18 23:57:18 +00:00
|
|
|
ConfigPath string
|
|
|
|
DataPath string
|
2023-09-19 23:29:55 +00:00
|
|
|
|
|
|
|
// Miscellaneous Settings
|
|
|
|
RegistrationEnabled bool
|
2023-09-23 02:12:36 +00:00
|
|
|
CookieSessionKey string
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Load() *Config {
|
|
|
|
return &Config{
|
2023-09-19 23:29:55 +00:00
|
|
|
Version: "0.0.1",
|
|
|
|
DBType: trimLowerString(getEnv("DATABASE_TYPE", "SQLite")),
|
|
|
|
DBName: trimLowerString(getEnv("DATABASE_NAME", "book_manager")),
|
|
|
|
DBPassword: getEnv("DATABASE_PASSWORD", ""),
|
|
|
|
ConfigPath: getEnv("CONFIG_PATH", "/config"),
|
|
|
|
DataPath: getEnv("DATA_PATH", "/data"),
|
|
|
|
ListenPort: getEnv("LISTEN_PORT", "8585"),
|
2023-09-23 02:12:36 +00:00
|
|
|
CookieSessionKey: trimLowerString(getEnv("COOKIE_SESSION_KEY", "")),
|
2023-09-19 23:29:55 +00:00
|
|
|
RegistrationEnabled: trimLowerString(getEnv("REGISTRATION_ENABLED", "false")) == "true",
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getEnv(key, fallback string) string {
|
|
|
|
if value, ok := os.LookupEnv(key); ok {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
return fallback
|
|
|
|
}
|
2023-09-19 23:29:55 +00:00
|
|
|
|
|
|
|
func trimLowerString(val string) string {
|
|
|
|
return strings.ToLower(strings.TrimSpace(val))
|
|
|
|
}
|