forked from ivpusic/neo
/
conf.go
99 lines (80 loc) · 1.99 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
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
package neo
import (
"time"
"net/http"
"github.com/ivpusic/toml"
)
const (
BYTE = 1.0
KILOBYTE = 1024 * BYTE
MEGABYTE = 1024 * KILOBYTE
GIGABYTE = 1024 * MEGABYTE
TERABYTE = 1024 * GIGABYTE
)
///////////////////////////////////////////////////////////////////
// `Hotreload` section
///////////////////////////////////////////////////////////////////
type HotReload struct {
Command string
Watch []string
Ignore []string
}
type LoggerSettings struct {
Name string
Level string
}
type AppModeSettings struct {
Addr string
}
///////////////////////////////////////////////////////////////////
// `App` section
///////////////////////////////////////////////////////////////////
type ApplicationSettings struct {
Addr string
ReadTimeout time.Duration
WriteTimeout time.Duration
// default to http.DefaultMaxHeaderBytes
MaxHeaderBytes int
// Default 10MB
MaxBodyBytes int64
Logger LoggerSettings
}
///////////////////////////////////////////////////////////////////
// `Neo` section
///////////////////////////////////////////////////////////////////
type NeoSettings struct {
Logger LoggerSettings
}
///////////////////////////////////////////////////////////////////
// `Global` section
///////////////////////////////////////////////////////////////////
// Neo Application configuration
type Conf struct {
Hotreload HotReload
App ApplicationSettings
Neo NeoSettings
}
func (c *Conf) loadDefaults() {
// hotreload
c.Hotreload.Ignore = []string{}
c.Hotreload.Watch = []string{"."}
// app
c.App.Addr = ":3000"
c.App.Logger.Level = "INFO"
c.App.Logger.Name = "application"
c.App.MaxHeaderBytes = http.DefaultMaxHeaderBytes
c.App.MaxBodyBytes = 10 * MEGABYTE
c.App.ReadTimeout = 0
c.App.WriteTimeout = 0
}
// Will try to parse TOML configuration file.
func (c *Conf) Parse(path string) {
c.loadDefaults()
if path == "" {
log.Warn("Configuration file not provided!")
return
}
if _, err := toml.DecodeFile(path, c); err != nil {
panic(err)
}
}