-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
159 lines (129 loc) · 3.82 KB
/
main.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"sync"
"github.com/imafish/http-test-server/internal/config"
"github.com/imafish/http-test-server/internal/handler"
"github.com/imafish/http-test-server/internal/rules"
"github.com/fsnotify/fsnotify"
)
func main() {
configPath := flag.String("c", "", "path to config file. manditory")
autoReload := flag.Bool("autoreload", false, "relaod config file is content is changed. IMPORTANT: Only rules are reloaded.")
flag.Parse()
if *configPath == "" {
usage()
}
config, err := config.LoadConfigFromFile(*configPath)
if err != nil {
log.Fatalf("Failed to load config file, err: %s", err.Error())
}
compiledRules, err := preprocessConfig(config)
if err != nil {
log.Fatalf("Failed to verify config object, err: %s", err.Error())
}
mtx := sync.Mutex{}
handler := &handler.RequestHandler{
Rules: &compiledRules,
Mtx: &mtx,
}
serverCount := len(config.Servers)
var wg sync.WaitGroup
wg.Add(serverCount)
if *autoReload {
watchConfigFile(*configPath, &compiledRules, &mtx, &wg)
wg.Add(1)
}
for _, server := range config.Servers {
go serverFunc(server, handler, &wg)
}
wg.Wait()
}
func watchConfigFile(configPath string, rules *[]*rules.CompiledRule, mtx *sync.Mutex, wg *sync.WaitGroup) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Printf("Failed to initialize file watcher: %s", err.Error())
return
}
err = watcher.Add(configPath)
if err != nil {
log.Printf("Failed to watch for config file: %s", err.Error())
return
}
log.Printf("Starting to watch for config file change...")
go func() {
wg.Add(1)
defer wg.Done()
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if event.Op&fsnotify.Write == fsnotify.Write {
log.Printf("\n------- ------- -------")
log.Printf("config file changed, reloading...")
config, err := config.LoadConfigFromFile(configPath)
if err != nil {
log.Printf("Failed to load config file, err: %s", err.Error())
continue
}
compiledRules, err := preprocessConfig(config)
if err != nil {
log.Printf("Failed to verify config object, err: %s", err.Error())
continue
}
mtx.Lock()
*rules = compiledRules
mtx.Unlock()
log.Printf("config file reloaded.")
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Printf("erro watching config file: %s", err)
}
}
}()
}
func serverFunc(server config.ServerConfig, handler http.Handler, wg *sync.WaitGroup) {
defer wg.Done()
if server.KeyFile != "" {
log.Printf("HTTPs server listening on %s, key file: %s, cert file: %s", server.Addr, server.KeyFile, server.CertFile)
log.Fatal(http.ListenAndServeTLS(server.Addr, server.CertFile, server.KeyFile, handler))
} else {
log.Printf("HTTP server listening on %s", server.Addr)
log.Fatal(http.ListenAndServe(server.Addr, handler))
}
}
func usage() {
flag.PrintDefaults()
os.Exit(1)
}
// preprocessConfig verifies whether manditory fields exists in config object then
// fills missing fields with default value.
// Also, it compiles plain Rule object into CompiledRule, complaining any error found during the process
func preprocessConfig(config *config.Config) ([]*rules.CompiledRule, error) {
if len(config.Servers) < 1 {
return nil, fmt.Errorf("server count must be greater than 1")
}
for _, server := range config.Servers {
if (server.CertFile != "" && server.KeyFile == "") || (server.KeyFile != "" && server.CertFile == "") {
return nil, fmt.Errorf("server.CertFile and server.KeyFile must come in pair")
}
}
compiledRules := make([]*rules.CompiledRule, len(config.Rules))
for i, r := range config.Rules {
compiledRule, err := rules.CompileRule(r)
if err != nil {
return nil, err
}
compiledRules[i] = compiledRule
}
return compiledRules, nil
}