-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
108 lines (96 loc) · 2.65 KB
/
config.go
File metadata and controls
108 lines (96 loc) · 2.65 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
package main
import (
"gopkg.in/yaml.v2"
"io"
"log"
"os"
)
type serverConfig struct {
Port int `yaml:"listen_port"`
Url string `yaml:"public_url"`
CertPath string `yaml:"cert_path"`
UseAutoCert bool `yaml:"use_auto_cert"`
UseManualCert bool `yaml:"use_manual_cert"`
ManualFullChain string `yaml:"manual_full_chain"`
ManualPrivate string `yaml:"manual_private"`
}
type loggingConfig struct {
File string `yaml:"file"`
JSON bool `yaml:"json"`
Prod bool `yaml:"production"`
}
type authConfig struct {
Password string `yaml:"password"`
ConnectionsPerSecond int `yaml:"connections_per_second"`
MaxNamespaceLength int `yaml:"max_namespace_length"`
UseClientPassword bool `yaml:"use_client_password"`
ClientPassword string `yaml:"client_password"`
}
type config struct {
Server serverConfig `yaml:"server"`
Logging loggingConfig `yaml:"logging"`
Auth authConfig `yaml:"auth"`
logFileHandle io.WriteCloser
}
func getDefaultConfig() *config {
cfg := &config{}
cfg.Server.Url = "messaget.example.com"
cfg.Server.UseAutoCert = false
cfg.Server.UseManualCert = false
cfg.Server.ManualFullChain = ""
cfg.Server.ManualPrivate = ""
cfg.Server.Port = 443
cfg.Server.CertPath = "/var/www/.cache"
cfg.Auth.ConnectionsPerSecond = 2
cfg.Auth.Password = "super-secure-password"
cfg.Auth.MaxNamespaceLength = 50
cfg.Auth.UseClientPassword = false
cfg.Auth.ClientPassword = "another-secret-password"
cfg.Logging.Prod = true
return cfg
}
func (cfg *config) setupLogging() error {
if cfg.logFileHandle != nil {
cfg.logFileHandle.Close()
}
if cfg.Logging.File != "" {
logFile, err := os.OpenFile(cfg.Logging.File, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
log.SetOutput(logFile)
cfg.logFileHandle = logFile
} else {
log.SetOutput(os.Stdout)
cfg.logFileHandle = nil
}
if cfg.Logging.JSON {
log.SetFlags(0)
} else {
log.SetFlags(log.LstdFlags)
}
return nil
}
func getConfig(configString string, file *string) (*config, error) {
cfg := getDefaultConfig()
if err := yaml.UnmarshalStrict([]byte(configString), cfg); err != nil {
return nil, err
}
if err := cfg.setupLogging(); err != nil {
return nil, err
}
// save default if it doesn't exist
if _, err := os.Stat(*file); os.IsNotExist(err) {
d, err := yaml.Marshal(&cfg)
if err != nil {
errorLogger.Fatalf("Failed to get config: %v", err)
}
f, createError := os.Create(*file)
if createError != nil {
errorLogger.Fatalf("Failed to create config: %v, %s", err, *file)
}
var _, _ = f.WriteString(string(d))
defer f.Close()
}
return cfg, nil
}