-
Notifications
You must be signed in to change notification settings - Fork 7
/
response.go
126 lines (96 loc) · 1.93 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
package gin
import (
"net/http"
"github.com/gin-gonic/gin"
contractshttp "github.com/goravel/framework/contracts/http"
)
type DataResponse struct {
code int
contentType string
data []byte
instance *gin.Context
}
func (r *DataResponse) Render() error {
r.instance.Data(r.code, r.contentType, r.data)
return nil
}
type DownloadResponse struct {
filename string
filepath string
instance *gin.Context
}
func (r *DownloadResponse) Render() error {
r.instance.FileAttachment(r.filepath, r.filename)
return nil
}
type FileResponse struct {
filepath string
instance *gin.Context
}
func (r *FileResponse) Render() error {
r.instance.File(r.filepath)
return nil
}
type JsonResponse struct {
code int
obj any
instance *gin.Context
}
func (r *JsonResponse) Render() error {
r.instance.JSON(r.code, r.obj)
return nil
}
type NoContentResponse struct {
code int
instance *gin.Context
}
func (r *NoContentResponse) Render() error {
r.instance.Status(r.code)
return nil
}
type RedirectResponse struct {
code int
location string
instance *gin.Context
}
func (r *RedirectResponse) Render() error {
r.instance.Redirect(r.code, r.location)
return nil
}
type StringResponse struct {
code int
format string
instance *gin.Context
values []any
}
func (r *StringResponse) Render() error {
r.instance.String(r.code, r.format, r.values...)
return nil
}
type HtmlResponse struct {
data any
instance *gin.Context
view string
}
func (r *HtmlResponse) Render() error {
r.instance.HTML(http.StatusOK, r.view, r.data)
return nil
}
type StreamResponse struct {
code int
instance *gin.Context
writer func(w contractshttp.StreamWriter) error
}
func (r *StreamResponse) Render() error {
r.instance.Status(r.code)
w := NewStreamWriter(r.instance)
ctx := r.instance.Request.Context()
for {
select {
case <-ctx.Done():
return nil
default:
return r.writer(w)
}
}
}