-
Notifications
You must be signed in to change notification settings - Fork 3
/
response.go
145 lines (109 loc) · 3.03 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
package gin
import (
"bytes"
"net/http"
"github.com/gin-gonic/gin"
httpcontract "github.com/goravel/framework/contracts/http"
)
type Response struct {
instance *gin.Context
origin httpcontract.ResponseOrigin
}
func NewResponse(instance *gin.Context, origin httpcontract.ResponseOrigin) *Response {
return &Response{instance, origin}
}
func (r *Response) Data(code int, contentType string, data []byte) {
r.instance.Data(code, contentType, data)
}
func (r *Response) Download(filepath, filename string) {
r.instance.FileAttachment(filepath, filename)
}
func (r *Response) File(filepath string) {
r.instance.File(filepath)
}
func (r *Response) Header(key, value string) httpcontract.Response {
r.instance.Header(key, value)
return r
}
func (r *Response) Json(code int, obj any) {
r.instance.JSON(code, obj)
}
func (r *Response) Origin() httpcontract.ResponseOrigin {
return r.origin
}
func (r *Response) Redirect(code int, location string) {
r.instance.Redirect(code, location)
}
func (r *Response) String(code int, format string, values ...any) {
r.instance.String(code, format, values...)
}
func (r *Response) Success() httpcontract.ResponseSuccess {
return NewGinSuccess(r.instance)
}
func (r *Response) Status(code int) httpcontract.ResponseStatus {
return NewGinStatus(r.instance, code)
}
func (r *Response) Writer() http.ResponseWriter {
return r.instance.Writer
}
type Success struct {
instance *gin.Context
}
func NewGinSuccess(instance *gin.Context) httpcontract.ResponseSuccess {
return &Success{instance}
}
func (r *Success) Data(contentType string, data []byte) {
r.instance.Data(http.StatusOK, contentType, data)
}
func (r *Success) Json(obj any) {
r.instance.JSON(http.StatusOK, obj)
}
func (r *Success) String(format string, values ...any) {
r.instance.String(http.StatusOK, format, values...)
}
type Status struct {
instance *gin.Context
status int
}
func NewGinStatus(instance *gin.Context, code int) httpcontract.ResponseSuccess {
return &Status{instance, code}
}
func (r *Status) Data(contentType string, data []byte) {
r.instance.Data(r.status, contentType, data)
}
func (r *Status) Json(obj any) {
r.instance.JSON(r.status, obj)
}
func (r *Status) String(format string, values ...any) {
r.instance.String(r.status, format, values...)
}
func ResponseMiddleware() httpcontract.Middleware {
return func(ctx httpcontract.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()
}