-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
52 lines (44 loc) · 992 Bytes
/
config.go
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
package main
import (
"log"
"os"
"strings"
)
type Config struct {
Port string
ConnectionString string
CacheConfig ShortenerCacheConfig
}
func ReadConnectionString() (string, bool) {
// First try env
connectionString, ok := os.LookupEnv("DB_CONNECTION_STRING")
if ok {
return connectionString, true
}
// Fall back to config file
content, err := os.ReadFile("/etc/r3dr/db_config")
if err != nil {
return "", false
}
trimmed := strings.TrimSpace(string(content))
if len(trimmed) == 0 {
return "", false
}
return trimmed, true
}
func ReadConfig() Config {
connectionString, ok := ReadConnectionString()
if !ok {
log.Fatalf("DB_CONNECTION_STRING env var not set, and no config at /etc/r3dr/db_config")
}
port, ok := os.LookupEnv("PORT")
if !ok {
log.Printf("PORT env var not set. Defaulting to 8080.")
port = "8080"
}
return Config{
Port: port,
ConnectionString: connectionString,
CacheConfig: CacheConfig,
}
}