-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.go
60 lines (51 loc) · 1.37 KB
/
status.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
package server
import (
"net/http"
"time"
"github.com/gin-gonic/gin"
"github.com/bbengfort/epistolary/pkg"
"github.com/bbengfort/epistolary/pkg/api/v1"
)
const (
serverStatusOK = "ok"
serverStatusStopping = "stopping"
serverStatusMaintenance = "maintenance"
)
func (s *Server) Status(c *gin.Context) {
// The available middleware handles stopping and maintenance mode. If the request
// has come this far, the status is necessarily ok.
out := api.StatusReply{
Status: serverStatusOK,
Version: pkg.Version(),
Uptime: time.Since(s.started).String(),
}
// Render the response
c.JSON(http.StatusOK, out)
}
// Available is middleware that uses the healthy boolean to return a service unavailable
// http status code if the server is shutting down. This middleware must be first in the
// chain to ensure that complex handling to slow the shutdown of the server.
func (s *Server) Available() gin.HandlerFunc {
return func(c *gin.Context) {
// Check health status
s.RLock()
if !s.healthy {
var status string
if s.conf.Maintenance {
status = serverStatusMaintenance
} else {
status = serverStatusStopping
}
c.JSON(http.StatusServiceUnavailable, api.StatusReply{
Status: status,
Uptime: time.Since(s.started).String(),
Version: pkg.Version(),
})
c.Abort()
s.RUnlock()
return
}
s.RUnlock()
c.Next()
}
}