2023-09-18 23:57:18 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2023-09-19 23:29:55 +00:00
|
|
|
"strings"
|
2023-11-25 23:38:18 +00:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
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-10-24 22:41:25 +00:00
|
|
|
DBType string
|
|
|
|
DBName 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-10-07 01:25:56 +00:00
|
|
|
SearchEnabled bool
|
2023-10-31 10:28:22 +00:00
|
|
|
DemoMode bool
|
2023-11-25 23:38:18 +00:00
|
|
|
LogLevel string
|
2023-10-24 22:41:25 +00:00
|
|
|
|
|
|
|
// Cookie Settings
|
|
|
|
CookieSessionKey string
|
|
|
|
CookieSecure bool
|
|
|
|
CookieHTTPOnly bool
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Load() *Config {
|
2023-11-25 23:38:18 +00:00
|
|
|
c := &Config{
|
2023-11-18 15:14:57 +00:00
|
|
|
Version: "0.0.1",
|
2023-09-19 23:29:55 +00:00
|
|
|
DBType: trimLowerString(getEnv("DATABASE_TYPE", "SQLite")),
|
2023-11-04 23:40:32 +00:00
|
|
|
DBName: trimLowerString(getEnv("DATABASE_NAME", "antholume")),
|
2023-09-19 23:29:55 +00:00
|
|
|
ConfigPath: getEnv("CONFIG_PATH", "/config"),
|
|
|
|
DataPath: getEnv("DATA_PATH", "/data"),
|
|
|
|
ListenPort: getEnv("LISTEN_PORT", "8585"),
|
|
|
|
RegistrationEnabled: trimLowerString(getEnv("REGISTRATION_ENABLED", "false")) == "true",
|
2023-10-31 10:28:22 +00:00
|
|
|
DemoMode: trimLowerString(getEnv("DEMO_MODE", "false")) == "true",
|
2023-10-07 01:25:56 +00:00
|
|
|
SearchEnabled: trimLowerString(getEnv("SEARCH_ENABLED", "false")) == "true",
|
2023-10-24 22:41:25 +00:00
|
|
|
CookieSessionKey: trimLowerString(getEnv("COOKIE_SESSION_KEY", "")),
|
2023-11-25 23:38:18 +00:00
|
|
|
LogLevel: trimLowerString(getEnv("LOG_LEVEL", "info")),
|
2023-10-24 22:41:25 +00:00
|
|
|
CookieSecure: trimLowerString(getEnv("COOKIE_SECURE", "true")) == "true",
|
|
|
|
CookieHTTPOnly: trimLowerString(getEnv("COOKIE_HTTP_ONLY", "true")) == "true",
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
2023-11-25 23:38:18 +00:00
|
|
|
|
|
|
|
// Log Level
|
|
|
|
ll, err := log.ParseLevel(c.LogLevel)
|
|
|
|
if err != nil {
|
|
|
|
ll = log.InfoLevel
|
|
|
|
}
|
|
|
|
log.SetLevel(ll)
|
|
|
|
|
|
|
|
return c
|
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))
|
|
|
|
}
|