Files
ez-api/internal/config/config.go
zenfun 64d71953a6 feat(server): implement management endpoints and redis sync
Enable full resource management via API and support data plane
synchronization.

- Add CRUD handlers for Providers, Models, and Keys using DTOs
- Implement LogWriter service for asynchronous, batched audit logging
- Update SyncService to snapshot full configuration state to Redis
- Register new API routes and initialize background services
- Add configuration options for logging performance tuning
2025-12-02 14:26:16 +08:00

81 lines
1.6 KiB
Go

package config
import (
"os"
"strconv"
"time"
)
type Config struct {
Server ServerConfig
Postgres PostgresConfig
Redis RedisConfig
Log LogConfig
}
type ServerConfig struct {
Port 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),
},
}, 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
}