/
response.go
104 lines (92 loc) · 3.04 KB
/
response.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
97
98
99
100
101
102
103
104
package azugo
import (
"net/url"
"strconv"
"strings"
"azugo.io/core/paginator"
"github.com/goccy/go-json"
"github.com/valyala/fasthttp"
"go.uber.org/zap"
)
// Response return the *fasthttp.Response object
// This allows you to use all fasthttp response methods
// https://godoc.org/github.com/valyala/fasthttp#Response
func (ctx *Context) Response() *fasthttp.Response {
return &ctx.context.Response
}
// StatusCode sets the HTTP status code for the response.
// This method is chainable.
func (ctx *Context) StatusCode(status int) *Context {
ctx.context.Response.SetStatusCode(status)
return ctx
}
// ContentType sets the Content-Type header for the response with optionally setting charset if provided.
// This method is chainable.
func (ctx *Context) ContentType(contentType string, charset ...string) *Context {
if len(charset) > 0 {
ctx.Response().Header.SetContentType(contentType + "; charset=" + charset[0])
} else {
ctx.Response().Header.SetContentType(contentType)
}
return ctx
}
// Redirect redirects the request to a given URL with status code 302 (Found) if other redirect status code
// not set already.
func (ctx *Context) Redirect(url string) {
if !fasthttp.StatusCodeIsRedirect(ctx.Response().StatusCode()) {
ctx.StatusCode(fasthttp.StatusFound)
}
// TODO: Check if it's safe to redirect to provided URL
ctx.Header.Set("Location", url)
}
// JSON serializes the given struct as JSON and sets it as the response body.
func (ctx *Context) JSON(obj any) {
ctx.Response().Header.SetContentTypeBytes(contentTypeJSON)
buf, err := json.Marshal(obj)
if err != nil {
ctx.Error(err)
return
}
ctx.Response().SetBodyRaw(buf)
}
// Text sets the response body to the given text.
func (ctx *Context) Text(text string) {
ctx.Response().Header.SetContentTypeBytes(contentTypeText)
ctx.Response().SetBodyString(text)
}
// Raw sets response body, but without copying it.
//
// WARNING: From this point onward the body argument must not be changed.
func (ctx *Context) Raw(data []byte) {
ctx.Response().SetBodyRaw(data)
}
// Error return the error response. Calls either custom ErrorHandler or default if not specified.
func (ctx *Context) Error(err error) {
ctx.mux.HandleError(ctx, err)
}
// NotFound returns an not found response. Calls either custom NotFound or default if not specified.
func (ctx *Context) NotFound() {
ctx.mux.HandleNotFound(ctx)
}
func (ctx *Context) SetPaging(values map[string]string, paginator *paginator.Paginator) {
ctx.Header.Set(HeaderTotalCount, strconv.Itoa(paginator.Total()))
ctx.Header.AppendAccessControlExposeHeaders(HeaderTotalCount)
route := ctx.RouterPath()
if len(route) == 0 {
return
}
for k, v := range values {
route = strings.Replace(route, "{"+k+"}", url.PathEscape(v), 1)
}
curl, err := url.Parse(ctx.BaseURL() + route)
if err != nil {
ctx.Log().Error("Failed to prepare paging header", zap.Error((err)))
return
}
paginator.SetURL(curl)
links := paginator.Links()
if len(links) > 0 {
ctx.Header.Set(HeaderLink, strings.Join(links, ","))
ctx.Header.AppendAccessControlExposeHeaders(HeaderLink)
}
}