73 lines
1.5 KiB
Go
73 lines
1.5 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"strconv"
|
|
)
|
|
|
|
type Config struct {
|
|
Server ServerConfig
|
|
Database DatabaseConfig
|
|
Auth AuthConfig
|
|
}
|
|
|
|
type ServerConfig struct {
|
|
Port string
|
|
Host string
|
|
Env string
|
|
}
|
|
|
|
type DatabaseConfig struct {
|
|
Type string // "sqlite" or "mysql"
|
|
Host string
|
|
Port string
|
|
User string
|
|
Password string
|
|
DBName string
|
|
SQLitePath string
|
|
}
|
|
|
|
type AuthConfig struct {
|
|
JWTSecret string
|
|
TokenDuration int // in hours
|
|
}
|
|
|
|
func Load() *Config {
|
|
return &Config{
|
|
Server: ServerConfig{
|
|
Port: getEnv("SERVER_PORT", "8080"),
|
|
Host: getEnv("SERVER_HOST", "0.0.0.0"),
|
|
Env: getEnv("ENV", "development"),
|
|
},
|
|
Database: DatabaseConfig{
|
|
Type: getEnv("DB_TYPE", "sqlite"),
|
|
Host: getEnv("DB_HOST", "localhost"),
|
|
Port: getEnv("DB_PORT", "3306"),
|
|
User: getEnv("DB_USER", "root"),
|
|
Password: getEnv("DB_PASSWORD", ""),
|
|
DBName: getEnv("DB_NAME", "admintemplate"),
|
|
SQLitePath: getEnv("SQLITE_PATH", "./data/admintemplate.db"),
|
|
},
|
|
Auth: AuthConfig{
|
|
JWTSecret: getEnv("JWT_SECRET", "your-secret-key-change-in-production"),
|
|
TokenDuration: getEnvAsInt("TOKEN_DURATION", 24),
|
|
},
|
|
}
|
|
}
|
|
|
|
func getEnv(key, defaultValue string) string {
|
|
if value := os.Getenv(key); value != "" {
|
|
return value
|
|
}
|
|
return defaultValue
|
|
}
|
|
|
|
func getEnvAsInt(key string, defaultValue int) int {
|
|
if value := os.Getenv(key); value != "" {
|
|
if intVal, err := strconv.Atoi(value); err == nil {
|
|
return intVal
|
|
}
|
|
}
|
|
return defaultValue
|
|
}
|