-
Notifications
You must be signed in to change notification settings - Fork 8
/
config.go
148 lines (124 loc) · 4.31 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"encoding/json"
"os"
"time"
)
type Config struct {
// Name is the string by which this instance of NodeAtlas will be
// referred to. It usually describes the entire project name or
// the region about which it focuses.
Name string
// Hostname is the address which NodeAtlas should identify itself
// as. For example, in a verification email, NodeAtlas would give
// the verification link as
// http://<hostname><prefix>/<long-random-id>
Hostname string
// Prefix is the URL prefix which is required to access the front
// end. For example, with a prefix of "/nodeatlas", NodeAtlas
// would be able to respond to http://example.com/nodeatlas.
Prefix string
// Addr is the network interface and port to which NodeAtlas
// should bind. This of the form "0.0.0.0:8077" (for global
// binding on port 8077) or "127.0.0.1:8077" (for only localhost
// binding on port 8077) or ":8077" (which is equivalent to
// "0.0.0.0:8077".)
Addr string
// ChildMaps is a list of addresses from which to pull lists of
// nodes every heartbeat. Please note that these maps are trusted
// fully, and they could easily introduce false nodes to the
// database temporarily (until cleared by the CacheExpiration.
ChildMaps []string
// Database is the structure which contains the database driver
// name, such as "sqlite3" or "mysql", and the database resource,
// such as a path to .db file, or username, password, and name.
Database struct {
DriverName string
Resource string
ReadOnly bool
}
// HeartbeatRate is the amount of time to wait between performing
// regular tasks, such as clearing expired nodes from the queue
// and cache.
HeartbeatRate Duration
// CacheExpiration is the amount of time for which to store cached
// nodes before considering them outdated, and removing them.
CacheExpiration Duration
// VerificationExpiration is the amount of time to allow users to
// verify nodes by email after initially placing them. See the
// documentation for time.ParseDuration for format information.
VerificationExpiration Duration
// SMTP contains the information necessary to connect to a mail
// relay, so as to send verification email to registered nodes. If
// it is nil, then node registration will be disabled.
SMTP *struct {
// EmailAddress will be given as the "From" address when
// sending email.
EmailAddress string
// NoAuthenticate determines whether NodeAtlas should attempt to
// authenticate with the SMTP relay or not. Unless the relay
// is local, leave this false.
NoAuthenticate bool
// Username and Password are the credentials required by the
// server to log in.
Username, Password string
// ServerAddress is the address of the SMTP relay, including
// the port.
ServerAddress string
}
// Map contains the information used by NodeAtlas to power the
// Leaflet.js map.
Map struct {
// Tileserver is the URL used for loading tiles. It is of the
// form "http://{s}.tile.osm.org/{z}/{x}/{y}.png", so that
// Leaflet.js can use it.
Tileserver string
// Center contains the coordinates on which to center the map.
Center struct {
Latitude, Longitude float64
}
// Zoom is the Leaflet.js zoom level to start the map at.
Zoom int
}
}
// ReadConfig uses os and encoding/json to read a configuration from
// the filesystem. It returns any errors it encounters.
func ReadConfig(path string) (conf *Config, err error) {
f, err := os.Open(path)
if err != nil {
return
}
defer f.Close()
conf = &Config{}
err = json.NewDecoder(f).Decode(conf)
return
}
// WriteConfig uses os and encoding/json to write a configuration to
// the filesystem. It creates the file if it doesn't exist and returns
// any errors it encounters.
func WriteConfig(conf *Config, path string) (err error) {
f, err := os.Create(path)
if err != nil {
return
}
defer f.Close()
err = json.NewEncoder(f).Encode(conf)
return
}
type Duration time.Duration
func (d Duration) MarshalJSON() ([]byte, error) {
return json.Marshal(time.Duration(d).String())
}
func (d *Duration) UnmarshalJSON(b []byte) error {
if b[0] != '"' {
// If the duration is not a string, then consider it to be the
// zero duration, so we do not have to set it.
return nil
}
dur, err := time.ParseDuration(string(b[1 : len(b)-1]))
if err != nil {
return err
}
*d = *(*Duration)(&dur)
return nil
}