-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
129 lines (111 loc) · 2.45 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
package main
import (
_ "embed"
"encoding/json"
"fmt"
"os"
"time"
"github.com/BurntSushi/toml"
)
var displayConfiguration = false
//go:embed config.default.toml
var ConfigDefault []byte
var (
/*
Note, will cause null pointer
exception if Configuration is
not set. (Good for testing)
*/
Configuration *ConfigStr
)
type ConfigBackup struct {
File ConfigFileSaver
}
type ConfigFileSaver struct {
Enabled bool
Prefix string
}
type ConfigStrSmtp struct {
Enabled bool
Type string // "plain", "tls", "starttls"
From string
Address string
Port string
User string
Pass string
}
type ConfigAdminStr struct {
Email []string // to: addresses for reports (not reported)
}
type ConfigStr struct {
ForumName string
Hostname string
OnionAddress string
Listen string
Priviledges map[string]UserPriviledge
Cert string // Note: filenames
Key string
Database string // note: filename
Keywords string // filename path to bleve
Backup ConfigBackup
LimitConnections int64
LimitWindow time.Duration // in seconds
Log string // filename
Page map[string]string
Admin ConfigAdminStr
Smtp ConfigStrSmtp
Forum []Forum
}
func LoadConfig(path string) error {
/*
Load configuration into
global variable struct
*/
f, err := os.ReadFile(path)
if err != nil {
return err
}
Configuration = new(ConfigStr)
if err := toml.Unmarshal(f, Configuration); err != nil {
return err
}
/*
Fill default values
*/
if Configuration.Listen == "" {
Configuration.Listen = ":1965" // default listening port
}
/*
Check for duplicate forum names
*/
forumNames := map[string]bool{}
subforumNames := map[string]bool{}
for _, f := range Configuration.Forum {
if _, ok := forumNames[f.Name]; ok {
// duplicate forum name
return ErrDuplicateForumName
} else {
forumNames[f.Name] = true
}
for _, s := range f.Subforum {
if _, ok := subforumNames[s.ID]; ok {
return ErrDuplicateSubforumName
} else {
subforumNames[s.ID] = true
}
}
}
/*
Initialize backup recievers
*/
fileSaver.Prefix = Configuration.Backup.File.Prefix
if displayConfiguration {
jsonBytes, err := json.MarshalIndent(Configuration, "", " ")
if err != nil {
fmt.Println("Error while marshalling Configuration:", err.Error())
} else {
fmt.Printf("%s\n", jsonBytes)
}
}
return nil
}