-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler_util_http.go
48 lines (40 loc) · 1.42 KB
/
handler_util_http.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
// Copyright The Jet authors. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package handler
import (
"github.com/fengyuan-liang/jet-web-fasthttp/pkg/constant"
"github.com/fengyuan-liang/jet-web-fasthttp/pkg/utils"
"github.com/valyala/fasthttp"
)
const (
Ok = "Ok"
)
func NotFoundHandler(ctx *fasthttp.RequestCtx) {
ctx.SetStatusCode(fasthttp.StatusNotFound)
ctx.Response.Header.SetServer("JetServer")
ctx.SetBodyString("404 Not Found")
}
func SuccessHandler(ctx *fasthttp.RequestCtx, data string) {
ctx.SetStatusCode(fasthttp.StatusOK)
ctx.Response.Header.SetServer("JetServer")
ctx.Response.Header.Set("Content-Type", constant.MIMEApplicationJSON)
ctx.SetBodyString(data)
}
func RestSuccessHandler(ctx *fasthttp.RequestCtx, data any) {
ctx.SetStatusCode(fasthttp.StatusOK)
ctx.Response.Header.SetServer("JetServer")
ctx.SetContentType(constant.MIMEApplicationJSON)
ctx.SetBodyString(utils.ObjToJsonStr(data))
}
func FailHandler(ctx *fasthttp.RequestCtx, data string) {
ctx.Response.Header.SetServer("JetServer")
ctx.SetBodyString(data)
}
// FailServerInternalErrorHandler Internal Server Error
func FailServerInternalErrorHandler(ctx *fasthttp.RequestCtx, data string) {
ctx.Response.Header.SetServer("JetServer")
ctx.SetContentType(constant.MIMEApplicationJSON)
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
ctx.SetBodyString(data)
}