-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
111 lines (99 loc) · 2.54 KB
/
Copy pathconfig.go
File metadata and controls
111 lines (99 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"fmt"
"log/slog"
"net/url"
"os"
"strconv"
"strings"
"time"
)
type Config struct {
Environment string
SiteID string
Address string
DatabaseURL string
APIKey string
ElasticAddresses []string
ElasticAPIKey string
ElasticIndex string
IngestMaxBytes int64
ShutdownTimeout time.Duration
SeedOnStart bool
}
func LoadConfig() (Config, error) {
environment := env("APP_ENV", "demo")
production := isProduction(environment)
cfg := Config{
Environment: environment,
SiteID: env("SITE_ID", "lab-01"),
Address: env("API_ADDR", ":8080"),
DatabaseURL: env("DATABASE_URL", "postgres://postgres:postgres@localhost:5432/telemetry?sslmode=disable"),
APIKey: env("API_KEY", defaultDemoKey(production)),
ElasticAPIKey: strings.TrimSpace(os.Getenv("ELASTICSEARCH_API_KEY")),
ElasticIndex: env("ELASTICSEARCH_INDEX", "hardware-telemetry-events"),
IngestMaxBytes: envInt64("INGEST_MAX_BYTES", 16<<20),
ShutdownTimeout: time.Duration(envInt64("SHUTDOWN_TIMEOUT_SECONDS", 20)) * time.Second,
SeedOnStart: envBool("SEED_ON_START", !production),
}
if _, err := url.Parse(cfg.DatabaseURL); err != nil {
return Config{}, fmt.Errorf("parse DATABASE_URL: %w", err)
}
if raw := strings.TrimSpace(os.Getenv("ELASTICSEARCH_URL")); raw != "" {
for _, part := range strings.Split(raw, ",") {
if address := strings.TrimSpace(part); address != "" {
cfg.ElasticAddresses = append(cfg.ElasticAddresses, address)
}
}
}
return cfg, nil
}
func defaultDemoKey(production bool) string {
if production {
return ""
}
return "demo-key"
}
func isProduction(environment string) bool {
switch strings.ToLower(strings.TrimSpace(environment)) {
case "prod", "production":
return true
default:
return false
}
}
func env(key, fallback string) string {
value := strings.TrimSpace(os.Getenv(key))
if value == "" {
return fallback
}
return value
}
func envBool(key string, fallback bool) bool {
value := strings.TrimSpace(os.Getenv(key))
if value == "" {
return fallback
}
parsed, err := strconv.ParseBool(value)
if err != nil {
return fallback
}
return parsed
}
func envInt64(key string, fallback int64) int64 {
value := strings.TrimSpace(os.Getenv(key))
if value == "" {
return fallback
}
parsed, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return fallback
}
return parsed
}
func logLevel(environment string) slog.Level {
if isProduction(environment) {
return slog.LevelInfo
}
return slog.LevelDebug
}