/
conf.go
62 lines (50 loc) · 1.46 KB
/
conf.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
// Copyright 2014 go-beacon authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package main
import (
"path/filepath"
"github.com/BurntSushi/toml"
)
type configFile struct {
Debug bool `toml:"debug"`
TemplatesDir string `toml:"templates_dir"`
DocumentRoot string `toml:"document_root"`
BeaconURI string `toml:"beacon_uri"`
DB struct {
Redis string `toml:"redis"`
} `toml:"db"`
HTTP struct {
Addr string `toml:"addr"`
XHeaders bool `toml:"xheaders"`
} `toml:"http_server"`
HTTPS struct {
Addr string `toml:"addr"`
CertFile string `toml:"cert_file"`
KeyFile string `toml:"key_file"`
} `toml:"https_server"`
Backend struct {
BackendURL string `toml:"backend_url"`
FlushInterval int `toml:"flush_interval"`
} `toml:"backend"`
}
// LoadConfig reads and parses the configuration file.
func loadConfig(filename string) (*configFile, error) {
c := &configFile{}
if _, err := toml.DecodeFile(filename, c); err != nil {
return nil, err
}
// Make files' path relative to the config file's directory.
basedir := filepath.Dir(filename)
relativePath(basedir, &c.DocumentRoot)
relativePath(basedir, &c.TemplatesDir)
relativePath(basedir, &c.HTTPS.CertFile)
relativePath(basedir, &c.HTTPS.KeyFile)
return c, nil
}
func relativePath(basedir string, path *string) {
p := *path
if p != "" && p[0] != '/' {
*path = filepath.Join(basedir, p)
}
}