forked from urandom/readeef
/
config.go
114 lines (94 loc) · 1.9 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
package readeef
import (
"os"
"time"
"code.google.com/p/gcfg"
)
var apiversion = 1
type Config struct {
Readeef struct {
Debug bool
}
API struct {
Version int
}
Timeout struct {
Connect string
ReadWrite string `gcfg:"read-write"`
Converted struct {
Connect time.Duration
ReadWrite time.Duration
}
}
DB struct {
Driver string
Connect string
}
Auth struct {
Secret string
IgnoreURLPrefix []string `gcfg:"ignore-url-prefix"`
}
Hubbub struct {
CallbackURL string `gcfg:"callback-url"` // http://www.example.com
RelativePath string `gcfg:"relative-path"`
From string
}
Updater struct {
Interval string
Converted struct {
Interval time.Duration
}
}
}
func ReadConfig(path ...string) (Config, error) {
def, err := defaultConfig()
if err != nil {
return Config{}, err
}
c := def
if len(path) != 0 {
err = gcfg.ReadFileInto(&c, path[0])
if err != nil && !os.IsNotExist(err) {
return Config{}, err
}
}
c.API.Version = apiversion
if d, err := time.ParseDuration(c.Timeout.Connect); err == nil {
c.Timeout.Converted.Connect = d
} else {
c.Timeout.Converted.Connect = time.Second
}
if d, err := time.ParseDuration(c.Timeout.ReadWrite); err == nil {
c.Timeout.Converted.ReadWrite = d
} else {
c.Timeout.Converted.ReadWrite = time.Second
}
if d, err := time.ParseDuration(c.Updater.Interval); err == nil {
c.Updater.Converted.Interval = d
} else {
c.Updater.Converted.Interval = 30 * time.Minute
}
return c, nil
}
func defaultConfig() (Config, error) {
var def Config
err := gcfg.ReadStringInto(&def, cfg)
if err != nil {
return Config{}, err
}
def.API.Version = apiversion
return def, nil
}
var cfg string = `
[readeef]
debug = true
[db]
driver = sqlite3
connect = file:./readeef.sqlite3?cache=shared&mode=rwc
[timeout]
connect = 1s
read-write = 2s
[hubbub]
relative-path = /hubbub
from = readeef
`