-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
115 lines (107 loc) · 3.07 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
package main
import (
"fmt"
"log"
"os"
"strconv"
"strings"
)
// The SQLITE_DRIVER value comes from modernc.org/sqlite/sqlite.driverName
const SQLITE_DRIVER = "sqlite"
type Config struct {
DbDriver string
DbConnectionString string
Port int
SessionLifetime int
MailHost string
MailPort int
MailLogin string
MailPassword string
MailFrom string
AddCorsHeader bool
CorsWhitelist string
}
var config Config
func mkconf() Config {
portString, exists := os.LookupEnv("GODRINK_PORT")
port := 8080
var err error
if exists {
port, err = strconv.Atoi(portString)
if err != nil {
port = 8080
log.Println(fmt.Sprintf("Error parsing port from env, defaulting to %d:", port), err)
}
}
dbdriver, driverExists := os.LookupEnv("GODRINK_DBDRIVER")
dbUrl, dbUrlExists := os.LookupEnv("GODRINK_DB")
if !driverExists {
log.Println("No database driver given, using embedded sqlite.")
log.Println("##############################################################")
log.Println("# Caution: NO DATA WILL PERSIST ACROSS APPLICATION RESTARTS! #")
log.Println("##############################################################")
dbdriver = SQLITE_DRIVER
dbUrl = "file::memory:?cache=shared"
} else {
dbdriver = strings.ToLower(dbdriver)
if !dbUrlExists {
if dbdriver == SQLITE_DRIVER {
dbUrl = "file::memory:?cache=shared"
} else {
log.Fatalf("The database driver (%s) requires specifying a connection string!", dbdriver)
}
}
}
lifetime := 300
lifetimeString, exists := os.LookupEnv("GODRINK_SESSIONLIFETIME")
if exists {
lifetime, err = strconv.Atoi(lifetimeString)
if err != nil {
lifetime = 300
log.Println(fmt.Sprintf("Error parsing session lifetime from env, defaulting to %d:", lifetime), err)
}
}
smtpserver, exists := os.LookupEnv("GODRINK_SMTPHOST")
var mailHost string
mailPort := 465
if !exists {
log.Println("No SMTP server given, mailing will fail!")
} else {
split := strings.Split(smtpserver, ":")
if len(split) != 2 {
log.Println("SMTP server must be specified as <host:port>, expect errors!")
} else {
mailHost = split[0]
mailPort, err = strconv.Atoi(split[1])
if err != nil {
log.Println("SMTP server must be specified as <host:port>, expect errors!")
}
}
}
mailLogin, exists := os.LookupEnv("GODRINK_SMTPUSER")
if !exists {
log.Println("No SMTP username given, mailing will likely fail!")
}
mailPass, exists := os.LookupEnv("GODRINK_SMTPPASS")
if !exists {
log.Println("No SMTP password given, mailing will likely fail!")
}
mailFrom, exists := os.LookupEnv("GODRINK_SMTPFROM")
if !exists {
mailFrom = mailLogin
}
cors, addCorsHeader := os.LookupEnv("GODRINK_CORS")
return Config{
DbDriver: dbdriver,
DbConnectionString: dbUrl,
Port: port,
SessionLifetime: lifetime,
MailHost: mailHost,
MailPort: mailPort,
MailLogin: mailLogin,
MailPassword: mailPass,
MailFrom: mailFrom,
AddCorsHeader: addCorsHeader,
CorsWhitelist: cors,
}
}