-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhealthcheck.go
63 lines (51 loc) · 1.32 KB
/
healthcheck.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
package healthcheck
import (
"net/http"
"github.com/gin-gonic/gin"
)
var (
// DefaultHeaderName default header name
DefaultHeaderName = "X-Health-Check"
// DefaultHeaderValue default header value
DefaultHeaderValue = "1"
// DefaultResponseCode default response code
DefaultResponseCode = http.StatusOK
// DefaultResponseText default response text
DefaultResponseText = "ok"
// DefaultConfig default config
DefaultConfig = Config{
HeaderName: DefaultHeaderName,
HeaderValue: DefaultHeaderValue,
ResponseCode: DefaultResponseCode,
ResponseText: DefaultResponseText}
)
// Config holds the configuration values
type Config struct {
HeaderName string
HeaderValue string
ResponseCode int
ResponseText string
}
// Default creates a new middileware with the default configuration
func Default() gin.HandlerFunc {
return New(DefaultConfig)
}
// New creates a new middileware with the `cfg`
func New(cfg Config) gin.HandlerFunc {
if cfg.HeaderName == "" {
cfg.HeaderName = DefaultHeaderName
}
if cfg.HeaderValue == "" {
cfg.HeaderValue = DefaultHeaderValue
}
if cfg.ResponseCode == 0 {
cfg.ResponseCode = DefaultResponseCode
}
return func(ctx *gin.Context) {
if ctx.GetHeader(cfg.HeaderName) == cfg.HeaderValue {
ctx.String(cfg.ResponseCode, cfg.ResponseText)
ctx.Abort()
}
ctx.Next()
}
}