-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
117 lines (99 loc) · 2.69 KB
/
config.go
File metadata and controls
117 lines (99 loc) · 2.69 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
112
113
114
115
116
117
package main
import (
"log"
"os"
"reflect"
"strconv"
"github.com/hammertrack/tracker/errors"
"github.com/joho/godotenv"
)
var ErrUnsupportedSecretLength = errors.New("secret length must be 256 bits or 32 bytes")
var (
APIPort string
DBHost string
DBKeyspace string
DBPort string
DBUser string
DBPassword string
DBConnTimeoutSeconds int
DBPageSize int
DBCursorSecretString string
DBCursorSecret []byte
Debug bool
ServerReadTimeout int
ServerWriteTimeout int
ServerIdleTimeout int
ServerReadBufferSize int
ServerWriteBufferSize int
ServerProxyHeader string
ServerBodyLimitBytes int
ServerConcurrency int
)
type SupportStringconv interface {
~int | ~int64 | ~float32 | ~string | ~bool
}
func conv(v string, to reflect.Kind) any {
var err error
if to == reflect.String {
return v
}
if to == reflect.Bool {
if bool, err := strconv.ParseBool(v); err == nil {
return bool
}
}
if to == reflect.Int {
if int, err := strconv.Atoi(v); err == nil {
return int
}
}
if to == reflect.Int64 {
if i64, err := strconv.ParseInt(v, 10, 64); err == nil {
return i64
}
}
if to == reflect.Float32 {
if f32, err := strconv.ParseFloat(v, 32); err == nil {
return f32
}
}
errors.WrapFatalWithContext(err, struct {
EnvKey string
}{v})
return nil
}
func Env[T SupportStringconv](key string, def T) T {
if v, ok := os.LookupEnv(key); ok {
val := conv(v, reflect.TypeOf(def).Kind()).(T)
log.Printf("[%s] => %v", key, val)
return val
}
return def
}
func LoadConfig() {
if err := godotenv.Load(); err != nil {
errors.WrapFatal(err)
}
APIPort = Env("API_PORT", "3000")
DBHost = Env("DB_HOST", "127.0.0.1")
DBKeyspace = Env("DB_KEYSPACE", "hammertrack")
DBPort = Env("DB_PORT", "5200")
DBUser = Env("DB_USER", "tracker")
DBPassword = Env("DB_PASSWORD", "unsafepassword")
DBConnTimeoutSeconds = Env("DB_CONN_TIMEOUT_SECONDS", 20)
DBPageSize = Env("DB_PAGE_SIZE", 200)
DBCursorSecretString = Env("DB_CURSOR_SECRET", "unsafesecret")
DBCursorSecret = []byte(DBCursorSecretString)
Debug = Env("DEBUG", false)
ServerReadTimeout = Env("SERVER_READ_TIMEOUT", 1)
ServerWriteTimeout = Env("SERVER_WRITE_TIMEOUT", 1)
ServerIdleTimeout = Env("SERVER_IDLE_TIMEOUT", 30)
ServerReadBufferSize = Env("SERVER_READ_BUFFER_SIZE", 4096)
ServerWriteBufferSize = Env("SERVER_WRITE_BUFFER_SIZE", 4096)
ServerProxyHeader = Env("SERVER_PROXY_HEADER", "")
ServerBodyLimitBytes = Env("SERVER_BODY_LIMIT_BYTES", 1*1024*1024)
ServerConcurrency = Env("SERVER_CONCURRENCY", 256*1024)
if len(DBCursorSecret) != 32 {
errors.WrapFatal(ErrUnsupportedSecretLength)
}
}