v0.0.198
This commit is contained in:
parent
0ead99608a
commit
0cfa159cb1
@ -7,50 +7,93 @@ import (
|
||||
json "gogs.mikescher.com/BlackForestBytes/goext/gojson"
|
||||
)
|
||||
|
||||
type headerval struct {
|
||||
Key string
|
||||
Val string
|
||||
}
|
||||
|
||||
type HTTPResponse interface {
|
||||
Write(g *gin.Context)
|
||||
WithHeader(k string, v string) HTTPResponse
|
||||
}
|
||||
|
||||
type jsonHTTPResponse struct {
|
||||
statusCode int
|
||||
data any
|
||||
headers []headerval
|
||||
}
|
||||
|
||||
func (j jsonHTTPResponse) Write(g *gin.Context) {
|
||||
for _, v := range j.headers {
|
||||
g.Header(v.Key, v.Val)
|
||||
}
|
||||
g.Render(j.statusCode, json.GoJsonRender{Data: j.data, NilSafeSlices: true, NilSafeMaps: true})
|
||||
}
|
||||
|
||||
func (j jsonHTTPResponse) WithHeader(k string, v string) HTTPResponse {
|
||||
j.headers = append(j.headers, headerval{k, v})
|
||||
return j
|
||||
}
|
||||
|
||||
type emptyHTTPResponse struct {
|
||||
statusCode int
|
||||
headers []headerval
|
||||
}
|
||||
|
||||
func (j emptyHTTPResponse) Write(g *gin.Context) {
|
||||
for _, v := range j.headers {
|
||||
g.Header(v.Key, v.Val)
|
||||
}
|
||||
g.Status(j.statusCode)
|
||||
}
|
||||
|
||||
func (j emptyHTTPResponse) WithHeader(k string, v string) HTTPResponse {
|
||||
j.headers = append(j.headers, headerval{k, v})
|
||||
return j
|
||||
}
|
||||
|
||||
type textHTTPResponse struct {
|
||||
statusCode int
|
||||
data string
|
||||
headers []headerval
|
||||
}
|
||||
|
||||
func (j textHTTPResponse) Write(g *gin.Context) {
|
||||
for _, v := range j.headers {
|
||||
g.Header(v.Key, v.Val)
|
||||
}
|
||||
g.String(j.statusCode, "%s", j.data)
|
||||
}
|
||||
|
||||
func (j textHTTPResponse) WithHeader(k string, v string) HTTPResponse {
|
||||
j.headers = append(j.headers, headerval{k, v})
|
||||
return j
|
||||
}
|
||||
|
||||
type dataHTTPResponse struct {
|
||||
statusCode int
|
||||
data []byte
|
||||
contentType string
|
||||
headers []headerval
|
||||
}
|
||||
|
||||
func (j dataHTTPResponse) Write(g *gin.Context) {
|
||||
for _, v := range j.headers {
|
||||
g.Header(v.Key, v.Val)
|
||||
}
|
||||
g.Data(j.statusCode, j.contentType, j.data)
|
||||
}
|
||||
|
||||
func (j dataHTTPResponse) WithHeader(k string, v string) HTTPResponse {
|
||||
j.headers = append(j.headers, headerval{k, v})
|
||||
return j
|
||||
}
|
||||
|
||||
type fileHTTPResponse struct {
|
||||
mimetype string
|
||||
filepath string
|
||||
filename *string
|
||||
headers []headerval
|
||||
}
|
||||
|
||||
func (j fileHTTPResponse) Write(g *gin.Context) {
|
||||
@ -59,26 +102,46 @@ func (j fileHTTPResponse) Write(g *gin.Context) {
|
||||
g.Header("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", *j.filename))
|
||||
|
||||
}
|
||||
for _, v := range j.headers {
|
||||
g.Header(v.Key, v.Val)
|
||||
}
|
||||
g.File(j.filepath)
|
||||
}
|
||||
|
||||
func (j fileHTTPResponse) WithHeader(k string, v string) HTTPResponse {
|
||||
j.headers = append(j.headers, headerval{k, v})
|
||||
return j
|
||||
}
|
||||
|
||||
type redirectHTTPResponse struct {
|
||||
statusCode int
|
||||
url string
|
||||
headers []headerval
|
||||
}
|
||||
|
||||
func (j redirectHTTPResponse) Write(g *gin.Context) {
|
||||
g.Redirect(j.statusCode, j.url)
|
||||
}
|
||||
|
||||
func (j redirectHTTPResponse) WithHeader(k string, v string) HTTPResponse {
|
||||
j.headers = append(j.headers, headerval{k, v})
|
||||
return j
|
||||
}
|
||||
|
||||
type jsonAPIErrResponse struct {
|
||||
err *exerr.ExErr
|
||||
err *exerr.ExErr
|
||||
headers []headerval
|
||||
}
|
||||
|
||||
func (j jsonAPIErrResponse) Write(g *gin.Context) {
|
||||
j.err.Output(g)
|
||||
}
|
||||
|
||||
func (j jsonAPIErrResponse) WithHeader(k string, v string) HTTPResponse {
|
||||
j.headers = append(j.headers, headerval{k, v})
|
||||
return j
|
||||
}
|
||||
|
||||
func Status(sc int) HTTPResponse {
|
||||
return &emptyHTTPResponse{statusCode: sc}
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
package goext
|
||||
|
||||
const GoextVersion = "0.0.197"
|
||||
const GoextVersion = "0.0.198"
|
||||
|
||||
const GoextVersionTimestamp = "2023-07-24T12:27:06+0200"
|
||||
const GoextVersionTimestamp = "2023-07-24T14:16:02+0200"
|
||||
|
Loading…
Reference in New Issue
Block a user