-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
96 lines (81 loc) · 2.72 KB
/
handlers.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package core
import (
"errors"
mantisHttp "github.com/sphireinc/mantis/http"
"github.com/valyala/fasthttp"
"strconv"
"time"
)
// ResponseJson creates a body and returns a JSON response
func ResponseJson(ctx *Context, output string, status int, error string) error {
body := Res{
Body: []byte(output),
BodyString: output,
Error: errors.New(error),
}
ctx.SetContentType("application/json")
// Set session token and request ID if available
if App.Server.ResponseConfig.SessionToken {
ctx.Response.Header.Set("X-Session-Token", HeaderFromCtx(ctx, "Session-Token"))
}
if App.Server.ResponseConfig.RequestId {
ctx.Response.Header.Set("X-Request-Id", HeaderFromCtx(ctx, "Request-Id"))
}
ctx.SetStatusCode(fasthttp.StatusOK)
if status != 200 {
ctx.SetStatusCode(status)
}
ctx.SetBody(body.Byte())
return nil
}
// HandleResponseJSON handles general responses via JSON.
func HandleResponseJSON(ctx *Context, body []byte, status int) error {
ctx.SetContentType("application/json")
// Set session token and request ID if available
if App.Server.ResponseConfig.SessionToken {
ctx.Response.Header.Set("X-Session-Token", HeaderFromCtx(ctx, "Session-Token"))
}
if App.Server.ResponseConfig.RequestId {
ctx.Response.Header.Set("X-Request-Id", HeaderFromCtx(ctx, "Request-Id"))
}
ctx.SetStatusCode(fasthttp.StatusOK)
if status != 200 {
ctx.SetStatusCode(status)
}
ctx.SetBody(body)
return nil
}
// NotFoundServer is the default 404 handler
func NotFoundServer(ctx *Context) error {
body := mantisHttp.Response{
Body: []byte("404 Not Found: " + string(ctx.Request.RequestURI())),
Error: errors.New("404 Not Found"),
}
return HandleResponseJSON(ctx, body.Byte(), fasthttp.StatusNotFound)
}
func MethodNotAllowed(ctx *Context) error {
body := mantisHttp.Response{
Body: []byte("405 Method Not Allowed" + string(ctx.Request.RequestURI())),
Error: errors.New("405 Method Not Allowed"),
}
return HandleResponseJSON(ctx, body.Byte(), fasthttp.StatusOK)
}
// Status simply returns a 200 OK
func Status(ctx *Context) error {
body := mantisHttp.Response{Body: []byte("OK")}
return HandleResponseJSON(ctx, body.Byte(), fasthttp.StatusOK)
}
// TeaPot is a 418 handler easter egg
func TeaPot(ctx *Context) error {
ctx.Response.Header.Set("X-Teapot", "Chai")
body := mantisHttp.Response{Body: []byte("Are you a teapot?")}
return HandleResponseJSON(ctx, body.Byte(), fasthttp.StatusTeapot)
}
// HeaderFromCtx returns the requested header from the given fasthttp context
func HeaderFromCtx(ctx *Context, key string) string {
return string(ctx.Request.Header.Peek(key))
}
// NextRequestID returns the next request id, which is a unix timestamp.
func NextRequestID() string {
return strconv.FormatInt(time.Now().UnixNano(), 36)
}