mirror of
https://git.evilfox.cc/test2/wg.git
synced 2026-07-31 20:03:22 +00:00
76 lines
1.7 KiB
Go
76 lines
1.7 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type Config struct {
|
|
AppPort string
|
|
AppURL string
|
|
SiteName string
|
|
DatabaseURL string
|
|
SessionSecret string
|
|
AdminUsername string
|
|
AdminEmail string
|
|
AdminPassword string
|
|
MySQLDumpPath string
|
|
AutoImportDump bool
|
|
SecureCookies bool
|
|
RememberDays int
|
|
}
|
|
|
|
func Load() Config {
|
|
return Config{
|
|
AppPort: getenv("APP_PORT", "4000"),
|
|
AppURL: strings.TrimRight(getenv("APP_URL", "http://localhost:4000"), "/"),
|
|
SiteName: getenv("SITE_NAME", "Evilfox.cc"),
|
|
DatabaseURL: getenv("DATABASE_URL", "postgres://wg:wg@localhost:5432/wg?sslmode=disable"),
|
|
SessionSecret: getenv("SESSION_SECRET", "change-me-session-secret-32chars!!"),
|
|
AdminUsername: getenv("ADMIN_USERNAME", "admin"),
|
|
AdminEmail: getenv("ADMIN_EMAIL", "admin@evilfox.cc"),
|
|
AdminPassword: getenv("ADMIN_PASSWORD", ""),
|
|
MySQLDumpPath: getenv("MYSQL_DUMP_PATH", "/data/wg.sql"),
|
|
AutoImportDump: getenvBool("AUTO_IMPORT_MYSQL", true),
|
|
SecureCookies: getenvBool("SECURE_COOKIES", false),
|
|
RememberDays: getenvInt("REMEMBER_DAYS", 30),
|
|
}
|
|
}
|
|
|
|
func (c Config) RememberDuration() time.Duration {
|
|
return time.Duration(c.RememberDays) * 24 * time.Hour
|
|
}
|
|
|
|
func getenv(key, def string) string {
|
|
if v := strings.TrimSpace(os.Getenv(key)); v != "" {
|
|
return v
|
|
}
|
|
return def
|
|
}
|
|
|
|
func getenvBool(key string, def bool) bool {
|
|
v := strings.TrimSpace(os.Getenv(key))
|
|
if v == "" {
|
|
return def
|
|
}
|
|
b, err := strconv.ParseBool(v)
|
|
if err != nil {
|
|
return def
|
|
}
|
|
return b
|
|
}
|
|
|
|
func getenvInt(key string, def int) int {
|
|
v := strings.TrimSpace(os.Getenv(key))
|
|
if v == "" {
|
|
return def
|
|
}
|
|
n, err := strconv.Atoi(v)
|
|
if err != nil {
|
|
return def
|
|
}
|
|
return n
|
|
}
|