-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
119 lines (97 loc) · 2.17 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"os"
"strconv"
)
type Config struct {
HTTPAddress string
HTTPAddressUnix string
AdminPassword string
Domain string
DatabaseURL string
Captcha HCaptchaConfig
SMTP SMTPConfig
DiskPath string
UploadingDate string
}
type HCaptchaConfig struct {
Enable bool
SecretKey string
SiteKey string
}
type SMTPConfig struct {
User string
Password string
Host string
Port int
}
func (c *Config) LoadEnv() error {
var ok bool
if captcha, ok := os.LookupEnv("ENABLE_CAPTCHA"); !ok {
if captcha == "true" {
c.Captcha.Enable = true
c.Captcha.SecretKey, ok = os.LookupEnv("HCAPTCHA_SECRET_KEY")
if !ok {
return fmt.Errorf("HCAPTCHA_SECRET_KEY not set")
}
c.Captcha.SiteKey, ok = os.LookupEnv("HCAPTCHA_SITE_KEY")
if !ok {
return fmt.Errorf("HCAPTCHA_SITE_KEY not set")
}
}
}
c.SMTP.User, ok = os.LookupEnv("SMTP_USER")
if !ok {
return fmt.Errorf("SMTP_USER not set")
}
c.SMTP.Password, ok = os.LookupEnv("SMTP_PASSWORD")
if !ok {
return fmt.Errorf("SMTP_PASSWORD not set")
}
c.SMTP.Host, ok = os.LookupEnv("SMTP_HOST")
if !ok {
return fmt.Errorf("SMTP_HOST not set")
}
if SMTPPort, ok := os.LookupEnv("SMTP_PORT"); !ok {
return fmt.Errorf("SMTP_PORT not set")
} else {
var err error
c.SMTP.Port, err = strconv.Atoi(SMTPPort)
if err != nil {
return fmt.Errorf("SMTP_PORT not set: %w", err)
}
}
c.Domain, ok = os.LookupEnv("DOMAIN")
if !ok {
return fmt.Errorf("DOMAIN not set")
}
if c.DiskPath == "" {
c.DiskPath, ok = os.LookupEnv("DISK_PATH")
if !ok {
return fmt.Errorf("DISK_PATH not set")
}
}
if c.DatabaseURL == "" {
c.DatabaseURL, ok = os.LookupEnv("DATABASE_URL")
if !ok {
return fmt.Errorf("DATABASE_URL not set")
}
}
c.AdminPassword, ok = os.LookupEnv("ADMIN_PASSWORD")
if !ok {
return fmt.Errorf("ADMIN_PASSWORD not set")
}
c.UploadingDate, ok = os.LookupEnv("UPLOADING_DATE")
if !ok {
return fmt.Errorf("UPLOADING_DATE not set")
}
return nil
}
func (c *Config) String() string {
b, _ := json.MarshalIndent(c, "", " ")
buf := bytes.NewBuffer(b)
return buf.String()
}