72 lines
1.5 KiB
Go
72 lines
1.5 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type Config struct {
|
|
Port string
|
|
BaseURL string
|
|
HTTPBudgetSec int
|
|
SessionSecret string
|
|
DatabaseURL string
|
|
PanelURL string
|
|
PanelToken string
|
|
ServerLabelsJSON string
|
|
MigrationsDir string
|
|
WebDir string
|
|
}
|
|
|
|
func Load() Config {
|
|
budget := envInt("APP_HTTP_BUDGET_SEC", 52)
|
|
return Config{
|
|
Port: env("APP_PORT", "30000"),
|
|
BaseURL: strings.Trim(env("APP_BASE_URL", ""), "/"),
|
|
HTTPBudgetSec: budget,
|
|
SessionSecret: env("APP_SESSION_SECRET", "dev-insecure-change-me-please-32chars"),
|
|
DatabaseURL: env("DATABASE_URL", "postgres://vpn_admin:vpn_admin@127.0.0.1:5432/vpn_admin?sslmode=disable"),
|
|
PanelURL: env("AMNEZIA_PANEL_URL", ""),
|
|
PanelToken: env("AMNEZIA_API_TOKEN", ""),
|
|
ServerLabelsJSON: env("AMNEZIA_SERVER_LABELS_JSON", ""),
|
|
MigrationsDir: env("MIGRATIONS_DIR", "migrations"),
|
|
WebDir: env("WEB_DIR", "web"),
|
|
}
|
|
}
|
|
|
|
func (c Config) HTTPBudget() time.Duration {
|
|
if c.HTTPBudgetSec <= 0 {
|
|
return 0
|
|
}
|
|
return time.Duration(c.HTTPBudgetSec) * time.Second
|
|
}
|
|
|
|
func (c Config) AppURL(path string) string {
|
|
path = strings.TrimLeft(path, "/")
|
|
if c.BaseURL == "" {
|
|
return "/" + path
|
|
}
|
|
return "/" + c.BaseURL + "/" + path
|
|
}
|
|
|
|
func env(k, def string) string {
|
|
if v := os.Getenv(k); v != "" {
|
|
return v
|
|
}
|
|
return def
|
|
}
|
|
|
|
func envInt(k string, def int) int {
|
|
v := os.Getenv(k)
|
|
if v == "" {
|
|
return def
|
|
}
|
|
n, err := strconv.Atoi(v)
|
|
if err != nil {
|
|
return def
|
|
}
|
|
return n
|
|
}
|