/
config.go
142 lines (117 loc) · 2.91 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package pkg
import (
"context"
"fmt"
"log"
"os"
"path/filepath"
"github.com/BurntSushi/toml"
"github.com/fsnotify/fsnotify"
)
const (
configFile = "config.toml"
)
var (
cfg Config
)
// Config is the Oogway application config.
type Config struct {
Server ServerConfig `toml:"server"`
Content ContentConfig `toml:"content"`
Sass SassConfig `toml:"sass"`
JS JSConfig `toml:"js"`
Pirsch PirschConfig `toml:"pirsch"`
}
// ServerConfig is the HTTP server configuration.
type ServerConfig struct {
Host string `toml:"host"`
Port int `toml:"port"`
ShutdownTimeout int `toml:"shutdown_time"`
WriteTimeout int `toml:"write_timeout"`
ReadTimeout int `toml:"read_timeout"`
TLSCertFile string `toml:"tls_cert_file"`
TLSKeyFile string `toml:"tls_key_file"`
}
// ContentConfig is the content configuration.
type ContentConfig struct {
NotFound string `toml:"not_found"`
}
// SassConfig is the sass compiler configuration.
type SassConfig struct {
Entrypoint string `toml:"entrypoint"`
Dir string `toml:"dir"`
Watch bool `toml:"watch"`
Out string `toml:"out"`
OutSourceMap string `toml:"out_source_map"`
}
// JSConfig is the JavaScript compiler configuration.
type JSConfig struct {
Entrypoint string `toml:"entrypoint"`
Dir string `toml:"dir"`
Watch bool `toml:"watch"`
Out string `toml:"out"`
SourceMap bool `toml:"source_map"`
}
// PirschConfig is the configuration for pirsch.io.
type PirschConfig struct {
ClientID string `toml:"client_id"`
ClientSecret string `toml:"client_secret"`
Subnets []string `toml:"subnets"`
Header []string `toml:"header"`
}
func loadConfig(dir string) error {
content, err := os.ReadFile(filepath.Join(dir, configFile))
if err != nil {
return fmt.Errorf("error loading config.toml: %s", err)
}
if _, err := toml.Decode(string(content), &cfg); err != nil {
return fmt.Errorf("error loading config.toml: %s", err)
}
setConfigDefaults()
return nil
}
func setConfigDefaults() {
if cfg.Server.Port == 0 {
cfg.Server.Port = 8080
}
if cfg.Server.ShutdownTimeout == 0 {
cfg.Server.ShutdownTimeout = 30
}
if cfg.Server.WriteTimeout == 0 {
cfg.Server.WriteTimeout = 5
}
if cfg.Server.ReadTimeout == 0 {
cfg.Server.ReadTimeout = 5
}
}
func watchConfig(ctx context.Context, dir string) error {
if err := loadConfig(dir); err != nil {
return err
}
watcher, err := fsnotify.NewWatcher()
if err != nil {
return err
}
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
continue
}
if event.Op == fsnotify.Write {
if err := loadConfig(dir); err != nil {
log.Printf("Error updating config.toml: %s", err)
}
}
case <-ctx.Done():
watcher.Close()
return
}
}
}()
if err := watcher.Add(filepath.Join(dir, configFile)); err != nil {
return err
}
return nil
}