mirror of
https://github.com/EZ-Api/ez-api.git
synced 2026-01-13 17:47:51 +00:00
Add admin and master authentication layers with JWT support. Replace direct key creation with hierarchical master/child key system. Update database schema to support master accounts with configurable limits and epoch-based key revocation. Add health check endpoint with system status monitoring. BREAKING CHANGE: Removed direct POST /keys endpoint in favor of master-based key issuance through /v1/tokens. Database migration requires dropping old User table and creating Master table with new relationships.
89 lines
1.7 KiB
Go
89 lines
1.7 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
type Config struct {
|
|
Server ServerConfig
|
|
Postgres PostgresConfig
|
|
Redis RedisConfig
|
|
Log LogConfig
|
|
Auth AuthConfig
|
|
}
|
|
|
|
type ServerConfig struct {
|
|
Port string
|
|
}
|
|
|
|
type AuthConfig struct {
|
|
JWTSecret string
|
|
}
|
|
|
|
type PostgresConfig struct {
|
|
DSN string
|
|
}
|
|
|
|
type RedisConfig struct {
|
|
Addr string
|
|
Password string
|
|
DB int
|
|
}
|
|
|
|
type LogConfig struct {
|
|
BatchSize int
|
|
FlushInterval time.Duration
|
|
QueueCapacity int
|
|
}
|
|
|
|
func Load() (*Config, error) {
|
|
return &Config{
|
|
Server: ServerConfig{
|
|
Port: getEnv("EZ_API_PORT", "8080"),
|
|
},
|
|
Postgres: PostgresConfig{
|
|
DSN: getEnv("EZ_PG_DSN", "host=localhost user=postgres password=postgres dbname=ezapi port=5432 sslmode=disable"),
|
|
},
|
|
Redis: RedisConfig{
|
|
Addr: getEnv("EZ_REDIS_ADDR", "localhost:6379"),
|
|
Password: getEnv("EZ_REDIS_PASSWORD", ""),
|
|
DB: getEnvInt("EZ_REDIS_DB", 0),
|
|
},
|
|
Log: LogConfig{
|
|
BatchSize: getEnvInt("EZ_LOG_BATCH_SIZE", 10),
|
|
FlushInterval: getEnvDuration("EZ_LOG_FLUSH_MS", 1000),
|
|
QueueCapacity: getEnvInt("EZ_LOG_QUEUE", 10000),
|
|
},
|
|
Auth: AuthConfig{
|
|
JWTSecret: getEnv("EZ_JWT_SECRET", "change_me_in_production"),
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
func getEnv(key, fallback string) string {
|
|
if value, ok := os.LookupEnv(key); ok {
|
|
return value
|
|
}
|
|
return fallback
|
|
}
|
|
|
|
func getEnvInt(key string, fallback int) int {
|
|
if value, ok := os.LookupEnv(key); ok {
|
|
if i, err := strconv.Atoi(value); err == nil {
|
|
return i
|
|
}
|
|
}
|
|
return fallback
|
|
}
|
|
|
|
func getEnvDuration(key string, fallbackMs int) time.Duration {
|
|
if value, ok := os.LookupEnv(key); ok {
|
|
if i, err := strconv.Atoi(value); err == nil {
|
|
return time.Duration(i) * time.Millisecond
|
|
}
|
|
}
|
|
return time.Duration(fallbackMs) * time.Millisecond
|
|
}
|