From 58511825d34f4ae232382cfd212710469c9ecca6 Mon Sep 17 00:00:00 2001 From: what Date: Thu, 24 Oct 2024 15:26:42 +0800 Subject: [PATCH] =?UTF-8?q?[feat]=20=E4=BC=98=E5=8C=96=20support/response,?= =?UTF-8?q?=20=E5=B9=B6=E6=96=B0=E5=A2=9E=20NewHttpStatusResponse?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- support/response.go | 106 +++++++++++++------------------------------- 1 file changed, 31 insertions(+), 75 deletions(-) diff --git a/support/response.go b/support/response.go index ad49b38..acbc7e5 100644 --- a/support/response.go +++ b/support/response.go @@ -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 }