-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
109 lines (90 loc) · 2.72 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
package config
import (
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
"io/ioutil"
)
type Database struct {
DBAccount string
DBPassword string
DBHost string
DBName string
DBPort int
DBSSLMode bool
}
type Server struct {
Mode string
Hostname string
Url string
Port string
ProblemsDir string
TemplatesDir string
CookieSecret string
GoogleAuth struct {
Enabled bool `json:"enabled" mapstructure:"enabled"`
ClientKey string `json:"client_key" mapstructure:"client_key"`
Secret string `json:"secret" mapstructure:"secret"`
Callback string `json:"callback" mapstructure:"callback"`
} `json:"googleAuth" mapstructure:"googleAuth"`
Sendgrid struct {
Enabled bool `json:"enabled" mapstructure:"enabled"`
ApiKey string `json:"api_key" mapstructure:"api_key"`
SenderName string `json:"sender_name" mapstructure:"sender_name"`
SenderAddress string `json:"sender_address" mapstructure:"sender_address"`
} `json:"sendgrid" mapstructure:"sendgrid"`
SMTP struct {
Enabled bool `json:"enabled" mapstructure:"enabled"`
MailAccount string `json:"mail_account" mapstructure:"mail_account"`
MailServerHost string `json:"mail_server" mapstructure:"mail_server"`
MailServerPort string `json:"mail_port" mapstructure:"mail_port"`
MailAccountPassword string `json:"mail_password" mapstructure:"mail_password"`
} `json:"smtp" mapstructure:"smtp"`
Database `mapstructure:",squash"`
Keys Keys
}
type Keys struct {
PrivateKeyLocation string `json:"private_key" mapstructure:"private_key"`
PublicKeyLocation string `json:"public_key" mapstructure:"public_key"`
PrivateKey *rsa.PrivateKey
PublicKey *rsa.PublicKey
}
func (k *Keys) Parse() error {
if k.PrivateKeyLocation != "" {
if k.PublicKeyLocation == "" {
return errors.New("private key filled, public not")
}
privateKeyContents, err := ioutil.ReadFile(k.PrivateKeyLocation)
if err != nil {
return err
}
block, _ := pem.Decode(privateKeyContents)
if block == nil {
return fmt.Errorf("can't parse pem private key file: %s", k.PrivateKeyLocation)
}
var pKey any
if pKey, err = x509.ParsePKCS8PrivateKey(block.Bytes); err != nil {
return err
}
k.PrivateKey = pKey.(*rsa.PrivateKey)
publicKeyContents, err := ioutil.ReadFile(k.PublicKeyLocation)
if err != nil {
return err
}
block, _ = pem.Decode(publicKeyContents)
if block == nil {
return fmt.Errorf("can't parse pem public key file: %s", k.PrivateKeyLocation)
}
if k.PublicKey, err = x509.ParsePKCS1PublicKey(block.Bytes); err != nil {
return err
}
}
return nil
}
func (k *Keys) MustParse() {
if err := k.Parse(); err != nil {
panic(err)
}
}