-
Notifications
You must be signed in to change notification settings - Fork 5
/
response.go
160 lines (125 loc) · 2.83 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package fiber
import (
"bufio"
"net/url"
"path/filepath"
"github.com/gofiber/fiber/v2"
contractshttp "github.com/goravel/framework/contracts/http"
)
type DataResponse struct {
code int
contentType string
data []byte
instance *fiber.Ctx
}
func (r *DataResponse) Render() error {
if invalidFiber(r.instance) {
return nil
}
r.instance.Response().Header.SetContentType(r.contentType)
return r.instance.Status(r.code).Send(r.data)
}
type DownloadResponse struct {
filename string
filepath string
instance *fiber.Ctx
}
func (r *DownloadResponse) Render() error {
if invalidFiber(r.instance) {
return nil
}
return r.instance.Download(r.filepath, r.filename)
}
type FileResponse struct {
filepath string
instance *fiber.Ctx
}
func (r *FileResponse) Render() error {
if invalidFiber(r.instance) {
return nil
}
dir, file := filepath.Split(r.filepath)
escapedFile := url.PathEscape(file)
escapedPath := filepath.Join(dir, escapedFile)
return r.instance.SendFile(escapedPath, true)
}
type JsonResponse struct {
code int
obj any
instance *fiber.Ctx
}
func (r *JsonResponse) Render() error {
if invalidFiber(r.instance) {
return nil
}
return r.instance.Status(r.code).JSON(r.obj)
}
type NoContentResponse struct {
code int
instance *fiber.Ctx
}
func (r *NoContentResponse) Render() error {
if invalidFiber(r.instance) {
return nil
}
return r.instance.Status(r.code).Send(nil)
}
type RedirectResponse struct {
code int
location string
instance *fiber.Ctx
}
func (r *RedirectResponse) Render() error {
if invalidFiber(r.instance) {
return nil
}
return r.instance.Redirect(r.location, r.code)
}
type StringResponse struct {
code int
format string
instance *fiber.Ctx
values []any
}
func (r *StringResponse) Render() error {
if invalidFiber(r.instance) {
return nil
}
if len(r.values) == 0 {
return r.instance.Status(r.code).SendString(r.format)
}
r.instance.Response().Header.SetContentType(r.format)
return r.instance.Status(r.code).SendString(r.values[0].(string))
}
type HtmlResponse struct {
data any
instance *fiber.Ctx
view string
}
func (r *HtmlResponse) Render() error {
if invalidFiber(r.instance) {
return nil
}
return r.instance.Render(r.view, r.data)
}
type StreamResponse struct {
code int
instance *fiber.Ctx
writer func(w contractshttp.StreamWriter) error
}
func (r *StreamResponse) Render() (err error) {
if invalidFiber(r.instance) {
return nil
}
r.instance.Status(r.code)
ctx := r.instance.Context()
ctx.SetBodyStreamWriter(func(w *bufio.Writer) {
err = r.writer(w)
})
return err
}
// invalidFiber instance.Context() will be nil when the request is timeout,
// the request will panic if ctx.Response() is called in this situation.
func invalidFiber(instance *fiber.Ctx) bool {
return instance.Context() == nil
}