-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathserve.go
119 lines (105 loc) · 3.8 KB
/
serve.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
package main
import (
"encoding/json"
"log"
"net/http"
"github.com/docker-flow/docker-flow-swarm-listener/metrics"
"github.com/docker-flow/docker-flow-swarm-listener/service"
"github.com/prometheus/client_golang/prometheus"
)
var httpListenAndServe = http.ListenAndServe
var httpWriterSetContentType = func(w http.ResponseWriter, value string) {
w.Header().Set("Content-Type", value)
}
//Response message
type Response struct {
Status string
}
// Serve is the instance structure
type Serve struct {
SwarmListener service.SwarmListening
Log *log.Logger
}
type server interface {
NotifyServices(w http.ResponseWriter, req *http.Request)
GetServices(w http.ResponseWriter, req *http.Request)
GetNodes(w http.ResponseWriter, req *http.Request)
PingHandler(w http.ResponseWriter, req *http.Request)
}
// NewServe returns a new instance of the `Serve`
func NewServe(swarmListener service.SwarmListening, logger *log.Logger) *Serve {
return &Serve{
SwarmListener: swarmListener,
Log: logger,
}
}
// Run executes a server
func Run(s server) error {
mux := attachRoutes(s)
return httpListenAndServe(":8080", mux)
}
// attachRoutes attaches routes to services and returns the mux
func attachRoutes(s server) *http.ServeMux {
mux := http.NewServeMux()
mux.HandleFunc("/v1/docker-flow-swarm-listener/notify-services", s.NotifyServices)
mux.HandleFunc("/v1/docker-flow-swarm-listener/get-nodes", s.GetNodes)
mux.HandleFunc("/v1/docker-flow-swarm-listener/get-services", s.GetServices)
mux.HandleFunc("/v1/docker-flow-swarm-listener/ping", s.PingHandler)
mux.Handle("/metrics", prometheus.Handler())
return mux
}
// NotifyServices notifies all configured endpoints of new, updated, or removed services
func (m Serve) NotifyServices(w http.ResponseWriter, req *http.Request) {
go m.SwarmListener.NotifyServices(false)
js, _ := json.Marshal(Response{Status: "OK"})
httpWriterSetContentType(w, "application/json")
w.WriteHeader(http.StatusOK)
w.Write(js)
}
// GetServices retrieves all services with the `com.df.notify` label set to `true`
func (m Serve) GetServices(w http.ResponseWriter, req *http.Request) {
parameters, err := m.SwarmListener.GetServicesParameters(req.Context())
if err != nil {
m.Log.Printf("ERROR: Unable to prepare response: %s", err)
metrics.RecordError("serveGetServices")
w.WriteHeader(http.StatusInternalServerError)
}
bytes, err := json.Marshal(parameters)
if err != nil {
m.Log.Printf("ERROR: Unable to prepare response: %s", err)
metrics.RecordError("serveGetServices")
w.WriteHeader(http.StatusInternalServerError)
} else {
// NOTE: For an unknown reason, `httpWriterSetContentType` does not work so the header is set directly
w.Header().Set("Content-Type", "application/json")
httpWriterSetContentType(w, "application/json")
w.Write(bytes)
}
}
// GetNodes retrieves all nodes
func (m Serve) GetNodes(w http.ResponseWriter, req *http.Request) {
parameters, err := m.SwarmListener.GetNodesParameters(req.Context())
if err != nil {
m.Log.Printf("ERROR: Unable to prepare response: %s", err)
metrics.RecordError("serveGetNodes")
w.WriteHeader(http.StatusInternalServerError)
}
bytes, err := json.Marshal(parameters)
if err != nil {
m.Log.Printf("ERROR: Unable to prepare response: %s", err)
metrics.RecordError("serveGetNodes")
w.WriteHeader(http.StatusInternalServerError)
} else {
// NOTE: For an unknown reason, `httpWriterSetContentType` does not work so the header is set directly
w.Header().Set("Content-Type", "application/json")
httpWriterSetContentType(w, "application/json")
w.Write(bytes)
}
}
// PingHandler is used for health checks
func (m Serve) PingHandler(w http.ResponseWriter, req *http.Request) {
js, _ := json.Marshal(Response{Status: "OK"})
httpWriterSetContentType(w, "application/json")
w.WriteHeader(http.StatusOK)
w.Write(js)
}