-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmain.go
71 lines (59 loc) · 1.5 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
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"time"
"github.com/julienschmidt/httprouter"
"gopkg.in/yaml.v2"
)
const CONFIG_FILE = "/etc/config/configmap-microservice-demo.yaml"
const BIND = "0.0.0.0:8080"
func check(err error) {
if err != nil {
log.Fatalf("error: %v", err)
}
}
/*
This is the struct that holds our application's configuration
*/
type Config struct {
Message string `yaml:"message"`
}
/*
Simple Yaml Config file loader
*/
func loadConfig(configFile string) *Config {
conf := &Config{}
configData, err := ioutil.ReadFile(configFile)
check(err)
err = yaml.Unmarshal(configData, conf)
check(err)
return conf
}
func main() {
confManager := NewMutexConfigManager(loadConfig(CONFIG_FILE))
//confManager := NewChannelConfigManager(loadConfig(CONFIG_FILE))
// Create a single GET Handler to print out our simple config message
router := httprouter.New()
router.GET("/", func(resp http.ResponseWriter, req *http.Request, params httprouter.Params) {
conf := confManager.Get()
fmt.Fprintf(resp, "%s", conf.Message)
})
// Watch the file for modification and update the config manager with the new config when it's available
watcher, err := WatchFile(CONFIG_FILE, time.Second, func() {
fmt.Printf("Configfile Updated\n")
conf := loadConfig(CONFIG_FILE)
confManager.Set(conf)
})
check(err)
// Clean up
defer func() {
watcher.Close()
confManager.Close()
}()
fmt.Printf("Listening on '%s'....\n", BIND)
err = http.ListenAndServe(BIND, router)
check(err)
}