ipapi/internal/config/config.go
sneak 2a1710cca8 Implement IP API daemon with GeoIP database support
- Create modular architecture with separate packages for config, database, HTTP, logging, and state management
- Implement Cobra CLI with daemon command
- Set up Uber FX dependency injection
- Add Chi router with health check and IP lookup endpoints
- Implement GeoIP database downloader with automatic updates
- Add state persistence for tracking database download times
- Include comprehensive test coverage for all components
- Configure structured logging with slog
- Add Makefile with test, lint, and build targets
- Support both IPv4 and IPv6 lookups
- Return country, city, ASN, and location data in JSON format
2025-07-27 18:15:38 +02:00

78 lines
1.4 KiB
Go

// Package config handles application configuration.
package config
import (
"os"
"strconv"
"git.eeqj.de/sneak/smartconfig"
)
// Config holds the application configuration.
type Config struct {
Port int
StateDir string
LogLevel string
}
// New creates a new configuration instance.
func New(configFile string) (*Config, error) {
// Check if config file exists first
if _, err := os.Stat(configFile); os.IsNotExist(err) {
return newDefaultConfig(), nil
}
// Load smartconfig
sc, err := smartconfig.NewFromConfigPath(configFile)
if err != nil {
return nil, err
}
cfg := &Config{}
// Get port from smartconfig or environment or default
if port, err := sc.GetInt("port"); err == nil {
cfg.Port = port
} else {
cfg.Port = getPortFromEnv()
}
// Get state directory
if stateDir, err := sc.GetString("state_dir"); err == nil {
cfg.StateDir = stateDir
} else {
cfg.StateDir = "/var/lib/ipapi"
}
// Get log level
if logLevel, err := sc.GetString("log_level"); err == nil {
cfg.LogLevel = logLevel
} else {
cfg.LogLevel = "info"
}
return cfg, nil
}
func newDefaultConfig() *Config {
return &Config{
Port: getPortFromEnv(),
StateDir: "/var/lib/ipapi",
LogLevel: "info",
}
}
func getPortFromEnv() int {
const defaultPort = 8080
portStr := os.Getenv("PORT")
if portStr == "" {
return defaultPort
}
port, err := strconv.Atoi(portStr)
if err != nil {
return defaultPort
}
return port
}