diff --git a/Dockerfile b/Dockerfile index 3fea87d..7c210bc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -22,5 +22,7 @@ EXPOSE 8080 CMD ["docker-flow-swarm-listener"] +HEALTHCHECK --interval=5s --start-period=3s --timeout=5s CMD wget -qO- "http://localhost:8080/v1/docker-flow-swarm-listener/ping" + COPY --from=build /src/docker-flow-swarm-listener /usr/local/bin/docker-flow-swarm-listener RUN chmod +x /usr/local/bin/docker-flow-swarm-listener diff --git a/serve.go b/serve.go index 6a02a20..1ac525e 100644 --- a/serve.go +++ b/serve.go @@ -19,11 +19,25 @@ type Serve struct { Notification service.Sender } +//Response message +type Response struct { + Status string +} + +// NewServe returns a new instance of the `Serve` +func NewServe(service service.Servicer, notification service.Sender) *Serve { + return &Serve{ + Service: service, + Notification: notification, + } +} + // Run executes a server func (m *Serve) Run() error { mux := http.NewServeMux() mux.HandleFunc("/v1/docker-flow-swarm-listener/notify-services", m.NotifyServices) mux.HandleFunc("/v1/docker-flow-swarm-listener/get-services", m.GetServices) + mux.HandleFunc("/v1/docker-flow-swarm-listener/ping", m.PingHandler) mux.Handle("/metrics", prometheus.Handler()) if err := httpListenAndServe(":8080", mux); err != nil { return err @@ -35,8 +49,10 @@ func (m *Serve) Run() error { func (m *Serve) NotifyServices(w http.ResponseWriter, req *http.Request) { services, _ := m.Service.GetServices() go m.Notification.ServicesCreate(services, 10, 5) - // TODO: Add response message + 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` @@ -54,10 +70,10 @@ func (m *Serve) GetServices(w http.ResponseWriter, req *http.Request) { httpWriterSetContentType(w, "application/json") } -// NewServe returns a new instance of the `Serve` -func NewServe(service service.Servicer, notification service.Sender) *Serve { - return &Serve{ - Service: service, - Notification: notification, - } -} +// 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) +} \ No newline at end of file diff --git a/serve_test.go b/serve_test.go index f4bd9cf..0d1d142 100644 --- a/serve_test.go +++ b/serve_test.go @@ -58,6 +58,24 @@ func (s *ServerTestSuite) Test_Run_ReturnsError_WhenHTTPListenAndServeFails() { // NotifyServices +func (s *ServerTestSuite) Test_NotifyServices_ReturnsStatus200() { + servicerMock := getServicerMock("") + notifMock := NotificationMock{ + ServicesCreateMock: func(services *[]service.SwarmService, retries, interval int) error { + return nil + }, + } + rw := getResponseWriterMock() + req, _ := http.NewRequest("GET", "/v1/docker-flow-swarm-listener/notify-services", nil) + expected, _ := json.Marshal(Response{Status: "OK"}) + + srv := NewServe(servicerMock, notifMock) + srv.NotifyServices(rw, req) + + rw.AssertCalled(s.T(), "WriteHeader", 200) + rw.AssertCalled(s.T(), "Write", []byte(expected)) +} + func (s *ServerTestSuite) Test_NotifyServices_SetsContentTypeToJSON() { var actual string httpWriterSetContentType = func(w http.ResponseWriter, value string) { @@ -109,7 +127,6 @@ func (s *ServerTestSuite) Test_NotifyServices_InvokesServicesCreate() { // GetServices func (s *ServerTestSuite) Test_GetServices_ReturnsServices() { - servicerMock := getServicerMock("GetServicesParameters") mapParam := []map[string]string{ {"serviceName": "demo", @@ -129,9 +146,25 @@ func (s *ServerTestSuite) Test_GetServices_ReturnsServices() { rsp := []map[string]string{} json.Unmarshal(value, &rsp) s.Equal(&mapParam, &rsp) +} + +// PingHandler +func (s *ServerTestSuite) Test_PingHandler_ReturnsStatus200() { + servicerMock := getServicerMock("") + notifMock := NotificationMock{} + rw := getResponseWriterMock() + req, _ := http.NewRequest("GET", "/v1/docker-flow-swarm-listener/ping", nil) + expected, _ := json.Marshal(Response{Status: "OK"}) + + srv := NewServe(servicerMock, notifMock) + srv.PingHandler(rw, req) + + rw.AssertCalled(s.T(), "WriteHeader", 200) + rw.AssertCalled(s.T(), "Write", []byte(expected)) } + // NewServe func (s *ServerTestSuite) Test_NewServe_SetsService() {