-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathresponse.go
98 lines (78 loc) · 1.81 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
package fiber
import (
"net/url"
"path/filepath"
"github.com/gofiber/fiber/v2"
)
type DataResponse struct {
code int
contentType string
data []byte
instance *fiber.Ctx
}
func (r *DataResponse) Render() error {
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 {
return r.instance.Download(r.filepath, r.filename)
}
type FileResponse struct {
filepath string
instance *fiber.Ctx
}
func (r *FileResponse) Render() error {
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 {
return r.instance.Status(r.code).JSON(r.obj)
}
type NoContentResponse struct {
code int
instance *fiber.Ctx
}
func (r *NoContentResponse) Render() error {
return r.instance.Status(r.code).Send(nil)
}
type RedirectResponse struct {
code int
location string
instance *fiber.Ctx
}
func (r *RedirectResponse) Render() error {
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 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 {
return r.instance.Render(r.view, r.data)
}