forked from ejoy/goscon
-
Notifications
You must be signed in to change notification settings - Fork 3
/
manager.go
53 lines (45 loc) · 1.17 KB
/
manager.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
package main
import (
"encoding/json"
"io"
"net"
"net/http"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/xjdrew/glog"
"github.com/xtaci/kcp-go"
)
func startManager(laddr string) (err error) {
if laddr == "" {
return
}
ln, err := net.Listen("tcp", laddr)
if err != nil {
glog.Infof("start manager failed: listen=%s, err=%s", laddr, err.Error())
return
}
glog.Infof("start manager: listen=%s", laddr)
http.HandleFunc("/config", func(w http.ResponseWriter, _ *http.Request) {
w.Header().Add("Content-Type", "text/vnd.yaml")
io.WriteString(w, marshalConfigFile())
})
http.HandleFunc("/reload", func(w http.ResponseWriter, _ *http.Request) {
err := reloadConfig()
if err == nil {
io.WriteString(w, "succeed")
} else {
io.WriteString(w, "failed: "+err.Error())
}
})
http.HandleFunc("/kcp/snmp", func(w http.ResponseWriter, _ *http.Request) {
w.Header().Add("Content-Type", "application/json")
enc := json.NewEncoder(w)
enc.Encode(kcp.DefaultSnmp.Copy())
})
http.Handle("/metrics", promhttp.Handler())
go func() {
defer ln.Close()
err := http.Serve(ln, nil)
glog.Errorf("manager exit: err=%v", err)
}()
return nil
}