61 lines
1.3 KiB
Go
61 lines
1.3 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
type Config struct {
|
|
Port string
|
|
BasePath string
|
|
AppOrigin string
|
|
SessionSecret string
|
|
TurnstileSecret string
|
|
TurnstileSiteKey string
|
|
DBPath string
|
|
AppDir string
|
|
}
|
|
|
|
func Load() *Config {
|
|
appDir := os.Getenv("APP_DIR")
|
|
if appDir == "" {
|
|
appDir = "."
|
|
}
|
|
|
|
// BasePath: no Mini PaaS, as requisições chegam com prefixo já removido, portanto "" é o ideal.
|
|
// Se o usuário passar BASE_PATH explicitamente, respeita a variável.
|
|
basePath := os.Getenv("BASE_PATH")
|
|
|
|
dbPath := os.Getenv("DB_PATH")
|
|
if dbPath == "" {
|
|
dbPath = filepath.Join(appDir, "data", "initpage.db")
|
|
}
|
|
|
|
appOrigin := os.Getenv("APP_ORIGIN")
|
|
if appOrigin == "" {
|
|
domain := os.Getenv("PAAS_DOMAIN")
|
|
if domain == "" {
|
|
domain = "paas.weltonmoura.com.br"
|
|
}
|
|
appOrigin = "https://" + domain
|
|
}
|
|
|
|
return &Config{
|
|
Port: getEnv("PORT", "8080"),
|
|
BasePath: basePath,
|
|
AppOrigin: appOrigin,
|
|
SessionSecret: getEnv("SESSION_SECRET", "dev-secret-change-me"),
|
|
TurnstileSecret: getEnv("TURNSTILE_SECRET", ""),
|
|
TurnstileSiteKey: getEnv("TURNSTILE_SITEKEY", ""),
|
|
DBPath: dbPath,
|
|
AppDir: appDir,
|
|
}
|
|
}
|
|
|
|
func getEnv(key, def string) string {
|
|
if v := os.Getenv(key); v != "" {
|
|
return v
|
|
}
|
|
return def
|
|
}
|