Skeleton contracts / contracts (pull_request) Blocked by required conditions
Реализован контракт lab1 (X-User-Id), non-root образ, SIGTERM ≤10s, DEFENSE/REPORT.
38 lines
642 B
Go
38 lines
642 B
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
)
|
|
|
|
type Config struct {
|
|
Host string
|
|
Port int
|
|
Env string
|
|
}
|
|
|
|
func Load() (Config, error) {
|
|
cfg := Config{
|
|
Host: getenv("APP_HOST", "0.0.0.0"),
|
|
Env: getenv("APP_ENV", "lab"),
|
|
}
|
|
portStr := getenv("APP_PORT", "8080")
|
|
port, err := strconv.Atoi(portStr)
|
|
if err != nil || port < 1 || port > 65535 {
|
|
return cfg, fmt.Errorf("invalid APP_PORT: %q", portStr)
|
|
}
|
|
cfg.Port = port
|
|
return cfg, nil
|
|
}
|
|
|
|
func (c Config) Addr() string {
|
|
return fmt.Sprintf("%s:%d", c.Host, c.Port)
|
|
}
|
|
|
|
func getenv(key, fallback string) string {
|
|
if v := os.Getenv(key); v != "" {
|
|
return v
|
|
}
|
|
return fallback
|
|
} |