/
configuration.go
84 lines (70 loc) · 1.54 KB
/
configuration.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
package main
import (
"io"
"os"
"gopkg.in/gcfg.v1"
)
const (
// The file we read from
configFile = "config.gcfg"
// The example file kept in version control. We'll copy and load from this
// by default.
configExample = "config.example.gcfg"
)
type Configuration struct {
Server struct {
Address string
ProfilerAddress string
Logging string
URL string
Ttl int
}
Minecraft struct {
UserAgent string
SessionServerURL string
ProfileURL string
}
Cache map[string]*struct {
Storage string
Size int
}
Redis map[string]*struct {
Address string
Auth string
DB int
Prefix string
PoolSize int
}
}
// Reads the configuration from the config file, copying a config into
// place from the example if one does not yet exist.
func (c *Configuration) load() error {
err := c.ensureConfigExists()
if err != nil {
return err
}
return gcfg.ReadFileInto(c, configFile)
}
// Creates the config.json if it does not exist.
func (c *Configuration) ensureConfigExists() error {
if _, err := os.Stat(configFile); os.IsNotExist(err) {
return copyFile(configExample, configFile)
}
return nil
}
// Copies *only the contents* of one file to a new path.
func copyFile(src string, dest string) error {
original, err := os.Open(src)
if err != nil {
return err
}
defer original.Close()
destination, err := os.Create(dest)
if err != nil {
return err
}
defer destination.Close()
// do the actual work
_, err = io.Copy(destination, original)
return err
}