-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
80 lines (69 loc) · 2.05 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"regexp"
)
// Config holds the configuration for march.
type Config struct {
Port string `json:"port"`
Database string `json:"database"`
Archivers []Archiver
Archives []Archive
}
// Archive contains information about an archive.
type Archive struct {
Name string `json:"name"`
Storage string `json:"storage"`
Users []struct {
Username string `json:"username"`
Password string `json:"password"`
}
}
// Archiver contains the structure of an archiver script and the regexp that
// will be used to match the incoming URL.
type Archiver struct {
Name string `json:"name"`
Script string `json:"script"`
Regexp string `json:"regexp"`
regexp *regexp.Regexp
}
// loadConfig loads the given configuration file into the app.
func (app *app) loadConfig(configFile string) error {
// Load the contents of the file into memory.
bytes, err := ioutil.ReadFile(configFile)
if err != nil {
fmt.Errorf("error: unable to read config", err)
}
// Reset the archives and archivers in the app.
app.archives = make(map[string]Archive)
app.archivers = make([]Archiver, 0)
// Create a new empty config object and unmarshal the contents of the
// file into it.
config := Config{}
json.Unmarshal(bytes, &config)
// Iterate over the archives, we'll perform a check to make sure that
// we haven't duplicated archives.
for _, a := range config.Archives {
if _, ok := app.archives[a.Name]; ok {
return fmt.Errorf("error: duplicate archive name:", a.Name)
}
app.archives[a.Name] = a
}
// Iterate over the archivers, for each archiver we'll compile a
// regexp so that we can use it easily in a later stage. If the regexp
// fails to compile we'll return an error.
for _, a := range config.Archivers {
a.regexp, err = regexp.Compile(a.Regexp)
if err != nil {
return fmt.Errorf("error: unable to compile regexp:", a.Regexp)
}
app.archivers = append(app.archivers, a)
}
// Store the simpler values.
app.port = config.Port
app.dbPath = config.Database
// Everything was OK.
return nil
}