-
Notifications
You must be signed in to change notification settings - Fork 3
/
context_response.go
153 lines (115 loc) · 3.75 KB
/
context_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
package gin
import (
"bytes"
"net/http"
"github.com/gin-gonic/gin"
contractshttp "github.com/goravel/framework/contracts/http"
)
type ContextResponse struct {
instance *gin.Context
origin contractshttp.ResponseOrigin
}
func NewContextResponse(instance *gin.Context, origin contractshttp.ResponseOrigin) *ContextResponse {
return &ContextResponse{instance, origin}
}
func (r *ContextResponse) Data(code int, contentType string, data []byte) contractshttp.Response {
return &DataResponse{code, contentType, data, r.instance}
}
func (r *ContextResponse) Download(filepath, filename string) contractshttp.Response {
return &DownloadResponse{filename, filepath, r.instance}
}
func (r *ContextResponse) File(filepath string) contractshttp.Response {
return &FileResponse{filepath, r.instance}
}
func (r *ContextResponse) Header(key, value string) contractshttp.ContextResponse {
r.instance.Header(key, value)
return r
}
func (r *ContextResponse) Json(code int, obj any) contractshttp.Response {
return &JsonResponse{code, obj, r.instance}
}
func (r *ContextResponse) Origin() contractshttp.ResponseOrigin {
return r.origin
}
func (r *ContextResponse) Redirect(code int, location string) contractshttp.Response {
return &RedirectResponse{code, location, r.instance}
}
func (r *ContextResponse) String(code int, format string, values ...any) contractshttp.Response {
return &StringResponse{code, format, r.instance, values}
}
func (r *ContextResponse) Success() contractshttp.ResponseSuccess {
return NewGinSuccess(r.instance)
}
func (r *ContextResponse) Status(code int) contractshttp.ResponseStatus {
return NewStatus(r.instance, code)
}
func (r *ContextResponse) View() contractshttp.ResponseView {
return NewView(r.instance)
}
func (r *ContextResponse) Writer() http.ResponseWriter {
return r.instance.Writer
}
func (r *ContextResponse) Flush() {
r.instance.Writer.Flush()
}
type Success struct {
instance *gin.Context
}
func NewGinSuccess(instance *gin.Context) contractshttp.ResponseSuccess {
return &Success{instance}
}
func (r *Success) Data(contentType string, data []byte) contractshttp.Response {
return &DataResponse{http.StatusOK, contentType, data, r.instance}
}
func (r *Success) Json(obj any) contractshttp.Response {
return &JsonResponse{http.StatusOK, obj, r.instance}
}
func (r *Success) String(format string, values ...any) contractshttp.Response {
return &StringResponse{http.StatusOK, format, r.instance, values}
}
type Status struct {
instance *gin.Context
status int
}
func NewStatus(instance *gin.Context, code int) contractshttp.ResponseSuccess {
return &Status{instance, code}
}
func (r *Status) Data(contentType string, data []byte) contractshttp.Response {
return &DataResponse{r.status, contentType, data, r.instance}
}
func (r *Status) Json(obj any) contractshttp.Response {
return &JsonResponse{r.status, obj, r.instance}
}
func (r *Status) String(format string, values ...any) contractshttp.Response {
return &StringResponse{r.status, format, r.instance, values}
}
func ResponseMiddleware() contractshttp.Middleware {
return func(ctx contractshttp.Context) {
blw := &BodyWriter{body: bytes.NewBufferString("")}
switch ctx := ctx.(type) {
case *Context:
blw.ResponseWriter = ctx.Instance().Writer
ctx.Instance().Writer = blw
}
ctx.WithValue("responseOrigin", blw)
ctx.Request().Next()
}
}
type BodyWriter struct {
gin.ResponseWriter
body *bytes.Buffer
}
func (w *BodyWriter) Write(b []byte) (int, error) {
w.body.Write(b)
return w.ResponseWriter.Write(b)
}
func (w *BodyWriter) WriteString(s string) (int, error) {
w.body.WriteString(s)
return w.ResponseWriter.WriteString(s)
}
func (w *BodyWriter) Body() *bytes.Buffer {
return w.body
}
func (w *BodyWriter) Header() http.Header {
return w.ResponseWriter.Header()
}