[feat] 优化 support/response, 并新增 NewHttpStatusResponse
This commit is contained in:
parent
90a3c22545
commit
58511825d3
@ -13,21 +13,14 @@ import (
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
type HttpStatusResponse struct {
|
||||
status int
|
||||
body req.HttpResponse
|
||||
}
|
||||
|
||||
type RawResponse struct {
|
||||
raw []byte
|
||||
}
|
||||
type JsonResponse struct {
|
||||
raw []byte
|
||||
}
|
||||
|
||||
type MsgResponse struct {
|
||||
code int
|
||||
msg string
|
||||
}
|
||||
|
||||
type ErrResponse struct {
|
||||
*MsgResponse
|
||||
stack []byte
|
||||
headers [][2]string
|
||||
raw []byte
|
||||
}
|
||||
|
||||
type FileResponse struct {
|
||||
@ -41,11 +34,24 @@ var wsUpgrader = websocket.Upgrader{
|
||||
},
|
||||
}
|
||||
|
||||
func (this HttpStatusResponse) Get(path ...string) req.GlobalParams {
|
||||
return this.body.Get(path...)
|
||||
}
|
||||
|
||||
func (this HttpStatusResponse) Send(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(this.status)
|
||||
this.body.Send(w, r)
|
||||
}
|
||||
|
||||
func (this RawResponse) Get(path ...string) req.GlobalParams {
|
||||
return lo.Ternary(len(path) == 0, req.NewGlobalParam(string(this.raw), nil), req.NewGlobalParam(string(this.raw), nil).Get(strings.Join(path, ".")))
|
||||
}
|
||||
|
||||
func (this RawResponse) Send(w http.ResponseWriter, r *http.Request) {
|
||||
for i := 0; i < len(this.headers); i++ {
|
||||
w.Header().Set(this.headers[i][0], this.headers[i][1])
|
||||
}
|
||||
|
||||
if r.Header.Get("Upgrade") != "" && strings.ToLower(r.Header.Get("Upgrade")) == "websocket" {
|
||||
c, _ := wsUpgrader.Upgrade(w, r, nil)
|
||||
c.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, string(this.raw)))
|
||||
@ -55,60 +61,6 @@ func (this RawResponse) Send(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
func (this JsonResponse) Get(path ...string) req.GlobalParams {
|
||||
return lo.Ternary(len(path) == 0, req.NewGlobalParam(string(this.raw), nil), req.NewGlobalParam(string(this.raw), nil).Get(strings.Join(path, ".")))
|
||||
}
|
||||
|
||||
func (this JsonResponse) Send(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Header.Get("Upgrade") != "" && strings.ToLower(r.Header.Get("Upgrade")) == "websocket" {
|
||||
c, _ := wsUpgrader.Upgrade(w, r, nil)
|
||||
c.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, string(this.raw)))
|
||||
c.Close()
|
||||
} else {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(this.raw)
|
||||
}
|
||||
}
|
||||
|
||||
func (this MsgResponse) Get(path ...string) req.GlobalParams {
|
||||
if resp, err := json.Marshal(map[string]any{
|
||||
"code": this.code,
|
||||
"msg": this.msg,
|
||||
}); err == nil {
|
||||
return lo.Ternary(len(path) == 0, req.NewGlobalParam(string(resp), nil), req.NewGlobalParam(string(resp), nil).Get(strings.Join(path, ".")))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this MsgResponse) Send(w http.ResponseWriter, r *http.Request) {
|
||||
if resp, err := json.Marshal(this.Get().Value()); err == nil {
|
||||
(&JsonResponse{raw: resp}).Send(w, r)
|
||||
} else {
|
||||
HttpResponse(err).Send(w, r)
|
||||
}
|
||||
}
|
||||
|
||||
func (this ErrResponse) Get(path ...string) req.GlobalParams {
|
||||
if resp, err := json.Marshal(map[string]any{
|
||||
"code": this.code,
|
||||
"msg": this.msg,
|
||||
"error": string(this.stack),
|
||||
}); err == nil {
|
||||
return lo.Ternary(len(path) == 0, req.NewGlobalParam(string(resp), nil), req.NewGlobalParam(string(resp), nil).Get(strings.Join(path, ".")))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this ErrResponse) Send(w http.ResponseWriter, r *http.Request) {
|
||||
if resp, err := json.Marshal(this.Get().Value()); err == nil {
|
||||
(&JsonResponse{raw: resp}).Send(w, r)
|
||||
} else {
|
||||
HttpResponse(err).Send(w, r)
|
||||
}
|
||||
}
|
||||
|
||||
func (this FileResponse) Get(path ...string) req.GlobalParams {
|
||||
if resp, err := json.Marshal(map[string]any{
|
||||
"name": this.name,
|
||||
@ -131,20 +83,24 @@ func (this FileResponse) Send(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
func NewRawResponse(b []byte) req.HttpResponse {
|
||||
return &RawResponse{raw: b}
|
||||
func NewHttpStatusResponse(status int, body req.HttpResponse) req.HttpResponse {
|
||||
return &HttpStatusResponse{status: status, body: body}
|
||||
}
|
||||
|
||||
func NewJsonResponse(b []byte) req.HttpResponse {
|
||||
return &JsonResponse{raw: b}
|
||||
func NewRawResponse(b []byte, headers ...[2]string) req.HttpResponse {
|
||||
return &RawResponse{raw: b}
|
||||
}
|
||||
|
||||
func NewFileResponse(name string, disposition string) req.HttpResponse {
|
||||
return &FileResponse{name: name, disposition: disposition}
|
||||
}
|
||||
|
||||
func NewJsonResponse(b []byte) req.HttpResponse {
|
||||
return NewRawResponse(b, [2]string{"Content-Type", "application/json"})
|
||||
}
|
||||
|
||||
func NewMsgResponse(msg string, code int) req.HttpResponse {
|
||||
return &MsgResponse{msg: msg, code: code}
|
||||
return NewJsonResponse([]byte(fmt.Sprintf(`{"code": %d, "msg": %q}`, code, msg)))
|
||||
}
|
||||
|
||||
func NewErrResponse(err *contracts.Err) req.HttpResponse {
|
||||
@ -173,7 +129,7 @@ func HttpResponse(data any) req.HttpResponse {
|
||||
"msg": contracts.OK.Msg,
|
||||
"data": json.RawMessage(v),
|
||||
}); e == nil {
|
||||
return &JsonResponse{raw: b}
|
||||
return NewJsonResponse(b)
|
||||
} else {
|
||||
err = e
|
||||
}
|
||||
@ -183,7 +139,7 @@ func HttpResponse(data any) req.HttpResponse {
|
||||
"msg": contracts.OK.Msg,
|
||||
"data": v,
|
||||
}); e == nil {
|
||||
return &JsonResponse{raw: b}
|
||||
return NewJsonResponse(b)
|
||||
} else {
|
||||
err = e
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user