-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfig.go
162 lines (135 loc) · 3.77 KB
/
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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package lib
import (
"fmt"
"net"
"net/url"
"os"
"strconv"
"strings"
"time"
"github.com/spf13/viper"
)
const (
StatusOnline int8 = iota
StatusOffline = 2
StatusNoNewSyncs = 3
)
type ServiceStatus int8
type Config struct {
Debug string
Database struct {
Type string
Connection string
}
Server struct {
BindIP string
Port string
Prefork bool
ServerHeader string
StrictRouting bool
CaseSensitive bool
ETag bool
Concurrency int
ProxyHeader string
EnableTrustedProxyCheck bool
TrustedProxies []string
DisableStartupMessage bool
AppName string
ReduceMemoryUsage bool
Network string
EnablePrintRoutes bool
}
Service struct {
Location string
Status ServiceStatus
Message string
MaxSyncSize int
}
Limiter struct {
Max int
Expiration time.Duration
}
}
func Cfg() (Config, error) {
viper.SetDefault("Debug", "true")
viper.SetDefault("Database.Type", "sqlite3")
viper.SetDefault("Database.Connection", "file:ent?mode=memory&cache=shared&_fk=1")
viper.SetDefault("Server.BindIP", "0.0.0.0")
viper.SetDefault("Server.Port", "8000")
viper.SetDefault("Server.Prefork", "false")
viper.SetDefault("Server.ServerHeader", "")
viper.SetDefault("Server.StrictRouting", "false")
viper.SetDefault("Server.CaseSensitive", "false")
viper.SetDefault("Server.ETag", "false")
viper.SetDefault("Server.Concurrency", strconv.Itoa(256*1024))
viper.SetDefault("Server.ProxyHeader", "")
viper.SetDefault("Server.EnableTrustedProxyCheck", "false")
viper.SetDefault("Server.TrustedProxies", "")
viper.SetDefault("Server.DisableStartupMessage", "true")
viper.SetDefault("Server.AppName", "xbsapi")
viper.SetDefault("Server.ReduceMemoryUsage", "false")
viper.SetDefault("Server.Network", "tcp")
viper.SetDefault("Server.EnablePrintRoutes", "false")
viper.SetDefault("Service.Location", "US")
viper.SetDefault("Service.Status", strconv.Itoa(int(StatusOnline)))
viper.SetDefault("Service.Message", "It really whips the llama's ass")
viper.SetDefault("Service.MaxSyncSize", strconv.Itoa(204800))
viper.SetDefault("Limiter.Max", strconv.Itoa(20))
viper.SetDefault("Limiter.Expiration", "60s")
viper.SetConfigName("xbsapi.toml")
viper.SetConfigType("toml")
viper.AddConfigPath("/etc/")
viper.AddConfigPath("$XDG_CONFIG_HOME/")
viper.AddConfigPath("$HOME/.config/")
viper.AddConfigPath("$HOME/")
viper.AddConfigPath(".")
viper.SetEnvPrefix("xbsapi")
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AutomaticEnv()
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
return Config{}, err
}
}
var config Config
if err := viper.Unmarshal(&config); err != nil {
return Config{}, err
}
config = *ParseDatabaseURL(&config)
return config, nil
}
func ParseDatabaseURL(config *Config) *Config {
databaseURL := os.Getenv("DATABASE_URL")
if databaseURL == "" {
return config
}
dbURL, err := url.Parse(databaseURL)
if err != nil {
return config
}
host, port, _ := net.SplitHostPort(dbURL.Host)
dbname := strings.TrimLeft(dbURL.Path, "/")
user := dbURL.User.Username()
password, _ := dbURL.User.Password()
switch dbURL.Scheme {
case "postgresql", "postgres":
if port == "" {
port = "5432"
}
config.Database.Type = "postgres"
config.Database.Connection = fmt.Sprintf(
"host=%s port=%s dbname=%s user=%s password=%s",
host, port, dbname, user, password,
)
case "mysql":
if port == "" {
port = "3306"
}
config.Database.Type = "mysql"
config.Database.Connection = fmt.Sprintf(
"%s:%s@tcp(%s:%s)/%s?parseTime=True",
user, password, host, port, dbname,
)
}
return config
}