-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgfr13.go
52 lines (43 loc) · 838 Bytes
/
gfr13.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
package gfr13
import (
"time"
"github.com/gin-gonic/gin"
)
const (
StatusCode = 513
)
type Opts struct {
UseUTC bool
UseJSON bool
AbortJSONKey string
AbortJSONValue string
}
var DefaultOpts = Opts{
UseUTC: true,
UseJSON: true,
AbortJSONKey: "error",
AbortJSONValue: "server too spooked, today is Friday 13th",
}
func Handler(opts *Opts) gin.HandlerFunc {
return func(c *gin.Context) {
if opts == nil {
opts = &DefaultOpts
}
now := time.Now()
if opts.UseUTC {
now = now.UTC()
}
if now.Day() == 13 && now.Weekday() == time.Friday {
if opts.UseJSON {
c.AbortWithStatusJSON(StatusCode, gin.H{
opts.AbortJSONKey: opts.AbortJSONValue,
"code": StatusCode,
})
} else {
c.AbortWithStatus(StatusCode)
}
return
}
c.Next()
}
}