-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
89 lines (81 loc) · 2.24 KB
/
Copy pathconfig.go
File metadata and controls
89 lines (81 loc) · 2.24 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
package main
import (
"crypto/rand"
"encoding/hex"
"log"
"log/slog"
"os"
"strconv"
"strings"
"time"
)
type Config struct {
Addr string
BaseURL string
DBPath string
ChallengeTTL time.Duration
TokenTTL time.Duration
TokenSecret []byte
MaxBodyBytes int64
}
func loadConfig() Config {
secret := envBytesHex("KSYNC_TOKEN_SECRET_HEX", nil)
if len(secret) < 32 {
if !envBool("KSYNC_ALLOW_EPHEMERAL_TOKEN_SECRET", false) {
log.Fatal("KSYNC_TOKEN_SECRET_HEX must be at least 32 bytes; set KSYNC_ALLOW_EPHEMERAL_TOKEN_SECRET=1 only for local development")
}
slog.Warn("KSYNC_TOKEN_SECRET_HEX is missing or too short; using an ephemeral token secret suitable only for local development")
secret = make([]byte, 32)
if _, err := rand.Read(secret); err != nil {
panic(err)
}
}
return Config{
Addr: envString("KSYNC_ADDR", "127.0.0.1:8080"),
BaseURL: envString("KSYNC_BASE_URL", "https://api.waozi.xyz"),
DBPath: envString("KSYNC_DB", "ksync.db"),
ChallengeTTL: envDurationSeconds("KSYNC_CHALLENGE_TTL_SECONDS", 60*time.Second),
TokenTTL: envDurationSeconds("KSYNC_TOKEN_TTL_SECONDS", 3600*time.Second),
TokenSecret: secret,
MaxBodyBytes: envInt64("KSYNC_MAX_BODY_BYTES", 1<<20),
}
}
func envString(key, fallback string) string {
if value := os.Getenv(key); value != "" {
return value
}
return fallback
}
func envDurationSeconds(key string, fallback time.Duration) time.Duration {
if value := os.Getenv(key); value != "" {
seconds, err := strconv.Atoi(value)
if err == nil && seconds > 0 {
return time.Duration(seconds) * time.Second
}
}
return fallback
}
func envInt64(key string, fallback int64) int64 {
if value := os.Getenv(key); value != "" {
n, err := strconv.ParseInt(value, 10, 64)
if err == nil && n > 0 {
return n
}
}
return fallback
}
func envBool(key string, fallback bool) bool {
if value := strings.ToLower(strings.TrimSpace(os.Getenv(key))); value != "" {
return value == "1" || value == "true" || value == "yes" || value == "on"
}
return fallback
}
func envBytesHex(key string, fallback []byte) []byte {
if value := os.Getenv(key); value != "" {
decoded, err := hex.DecodeString(value)
if err == nil {
return decoded
}
}
return fallback
}