forked from docker-flow/docker-flow-swarm-listener
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserve.go
44 lines (37 loc) · 882 Bytes
/
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
package main
import (
"net/http"
)
var httpListenAndServe = http.ListenAndServe
var httpWriterSetContentType = func(w http.ResponseWriter, value string) {
w.Header().Set("Content-Type", value)
}
type Server interface {
Run()
}
type Serve struct {
Service Servicer
}
func (m *Serve) Run() error {
if err := httpListenAndServe(":8080", m); err != nil {
return err
}
return nil
}
func (m *Serve) ServeHTTP(w http.ResponseWriter, req *http.Request) {
httpWriterSetContentType(w, "application/json")
switch req.URL.Path {
case "/v1/docker-flow-swarm-listener/notify-services":
services, _ := m.Service.GetServices()
go m.Service.NotifyServicesCreate(services, 10, 5)
// TODO: Add response message
w.WriteHeader(http.StatusOK)
default:
w.WriteHeader(http.StatusNotFound)
}
}
func NewServe(service Servicer) *Serve {
return &Serve{
Service: service,
}
}