This repository has been archived by the owner on May 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
config.go
98 lines (77 loc) · 1.98 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
)
type Config struct {
DbServer string
DbUser string
DbPassword string
DbCatalog string
SmtpMxHost string
SmtpPort int // internal, nginx handles TLS and forwards
HttpPort int // internal, nginx handles SSL and forwards
SeedNotaries []string // for seeding new accounts
ReservedNames []string // reserved usernames
AncestorIDsMaxBytes int // should match the VARCHAR() limit of email > ancestor_ids
}
func GetConfig() *Config {
return &config
}
const LOCAL_MX_HOST = "scramble.io"
var defaultConfig = Config{
"127.0.0.1",
"scramble",
"scramble",
"scramble",
LOCAL_MX_HOST,
8825,
8888,
[]string{"hashed.im", "dev.hashed.im"},
[]string{"admin", "administrator", "root", "support", "help", "spam",
"info", "contact", "webmaster", "abuse", "security", "mailer-daemon",
"mailer", "daemon", "postmaster"},
10240,
}
var config Config
func init() {
configFile := os.Getenv("HOME") + "/.scramble/config.json"
log.Printf("Reading " + configFile)
// try to read configuration. if missing, write default
configBytes, err := ioutil.ReadFile(configFile)
if err != nil {
writeDefaultConfig(configFile)
fmt.Println("Config file written to ~/.scramble/config.json. Please edit & run again")
os.Exit(1)
return
}
// try to parse configuration. on error, die
config = defaultConfig
err = json.Unmarshal(configBytes, &config)
if err != nil {
log.Panicf("Invalid configuration file %s: %v", configFile, err)
}
}
func writeDefaultConfig(configFile string) {
log.Printf("Creating default configration file %s", configFile)
configBytes, err := json.MarshalIndent(defaultConfig, "", " ")
if err != nil {
panic(err)
}
err = ioutil.WriteFile(configFile, configBytes, 0600)
if err != nil {
panic(err)
}
}
func (cfg *Config) IsReservedName(name string) bool {
for _, n := range cfg.ReservedNames {
if strings.ToLower(n) == strings.ToLower(name) {
return true
}
}
return false
}