Replace in-file bootstrap logic with package-level constructors in `run`: - use `config.Load()` instead of local env parsing/AppConfig helpers - use `store.Open()` and `web.New()` for persistence and app wiring - rename local store variable to `benchmarkStore` for clarity This centralizes startup concerns in dedicated modules, reducing `main.go` boilerplate and improving maintainability.refactor(main): delegate setup to config, store, and web packages Replace in-file bootstrap logic with package-level constructors in `run`: - use `config.Load()` instead of local env parsing/AppConfig helpers - use `store.Open()` and `web.New()` for persistence and app wiring - rename local store variable to `benchmarkStore` for clarity This centralizes startup concerns in dedicated modules, reducing `main.go` boilerplate and improving maintainability.
60 lines
1.1 KiB
Go
60 lines
1.1 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
type Config struct {
|
|
Addr string
|
|
BadgerDir string
|
|
PageSize int
|
|
ShutdownTimeout time.Duration
|
|
}
|
|
|
|
func Load() Config {
|
|
return Config{
|
|
Addr: envOrDefault("APP_ADDR", ":8080"),
|
|
BadgerDir: envOrDefault("BADGER_DIR", "data/badger"),
|
|
PageSize: envIntOrDefault("PAGE_SIZE", 50),
|
|
ShutdownTimeout: envDurationOrDefault("SHUTDOWN_TIMEOUT", 10*time.Second),
|
|
}
|
|
}
|
|
|
|
func envOrDefault(key, fallback string) string {
|
|
if value := os.Getenv(key); value != "" {
|
|
return value
|
|
}
|
|
|
|
return fallback
|
|
}
|
|
|
|
func envIntOrDefault(key string, fallback int) int {
|
|
value := os.Getenv(key)
|
|
if value == "" {
|
|
return fallback
|
|
}
|
|
|
|
parsed, err := strconv.Atoi(value)
|
|
if err != nil || parsed <= 0 {
|
|
return fallback
|
|
}
|
|
|
|
return parsed
|
|
}
|
|
|
|
func envDurationOrDefault(key string, fallback time.Duration) time.Duration {
|
|
value := os.Getenv(key)
|
|
if value == "" {
|
|
return fallback
|
|
}
|
|
|
|
parsed, err := time.ParseDuration(value)
|
|
if err != nil || parsed <= 0 {
|
|
return fallback
|
|
}
|
|
|
|
return parsed
|
|
}
|