Files
ez-api/internal/config/config.go
zenfun 58dfe5e9ac feat(server): initialize project skeleton with db and api setup
Establish the foundational structure for the ez-api server.

Key changes include:
- Set up main entry point with graceful shutdown and Gin router
- Configure database connections for PostgreSQL (GORM) and Redis
- Define core data models (User, Provider, Key, Model)
- Implement configuration loading and basic key creation handler
- Add Dockerfile for multi-stage builds and .gitignore
2025-12-02 13:35:17 +08:00

59 lines
1.0 KiB
Go

package config
import (
"os"
"strconv"
)
type Config struct {
Server ServerConfig
Postgres PostgresConfig
Redis RedisConfig
}
type ServerConfig struct {
Port string
}
type PostgresConfig struct {
DSN string
}
type RedisConfig struct {
Addr string
Password string
DB 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),
},
}, 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
}