100 lines
2.0 KiB
Go
100 lines
2.0 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
DefaultPort = ":8080"
|
|
DefaultJWTSecret = "local-poc-secret-change-me"
|
|
DefaultResetInterval = 10 * time.Minute
|
|
DefaultDatasetSize = 546
|
|
)
|
|
|
|
// ApiKey mirrors the original API's JWT utility style: utils.GenerateToken and
|
|
// utils.VerifyToken read the signing secret from config.
|
|
var ApiKey = DefaultJWTSecret
|
|
|
|
type Config struct {
|
|
Environment string
|
|
API APIConfig
|
|
Auth AuthConfig
|
|
Dataset DatasetConfig
|
|
}
|
|
|
|
type APIConfig struct {
|
|
Name string
|
|
Port string
|
|
BaseURL string
|
|
}
|
|
|
|
type AuthConfig struct {
|
|
JWTSecret string
|
|
}
|
|
|
|
type DatasetConfig struct {
|
|
Size int
|
|
ResetInterval time.Duration
|
|
}
|
|
|
|
func LoadConfig() Config {
|
|
return Config{
|
|
Environment: envString("APP_ENV", "dev"),
|
|
API: APIConfig{
|
|
Name: envString("APP_NAME", "abuse-registration-poc"),
|
|
Port: envString("PORT", DefaultPort),
|
|
BaseURL: envString("BASE_URL", "http://localhost:8080"),
|
|
},
|
|
Auth: AuthConfig{
|
|
JWTSecret: envString("JWT_SECRET", DefaultJWTSecret),
|
|
},
|
|
Dataset: DatasetConfig{
|
|
Size: envInt("DATASET_SIZE", DefaultDatasetSize),
|
|
ResetInterval: envDuration("RESET_INTERVAL", DefaultResetInterval),
|
|
},
|
|
}
|
|
}
|
|
|
|
func SetAPIKey(value string) {
|
|
if strings.TrimSpace(value) == "" {
|
|
ApiKey = DefaultJWTSecret
|
|
return
|
|
}
|
|
ApiKey = value
|
|
}
|
|
|
|
func envString(key, fallback string) string {
|
|
value := strings.TrimSpace(os.Getenv(key))
|
|
if value == "" {
|
|
return fallback
|
|
}
|
|
return value
|
|
}
|
|
|
|
func envInt(key string, fallback int) int {
|
|
value := strings.TrimSpace(os.Getenv(key))
|
|
if value == "" {
|
|
return fallback
|
|
}
|
|
parsed, err := strconv.Atoi(value)
|
|
if err != nil || parsed < 1 {
|
|
return fallback
|
|
}
|
|
return parsed
|
|
}
|
|
|
|
func envDuration(key string, fallback time.Duration) time.Duration {
|
|
value := strings.TrimSpace(os.Getenv(key))
|
|
if value == "" {
|
|
return fallback
|
|
}
|
|
parsed, err := time.ParseDuration(value)
|
|
if err != nil {
|
|
return fallback
|
|
}
|
|
return parsed
|
|
}
|