contracts/support/response.go

161 lines
4.1 KiB
Go

package support
import (
"encoding/json"
"fmt"
"net/http"
"os"
"strings"
"git.fsdpf.net/go/contracts"
"git.fsdpf.net/go/req"
"github.com/gorilla/websocket"
"github.com/samber/lo"
)
type HttpStatusResponse struct {
status int
body req.HttpResponse
}
type RawResponse struct {
headers [][2]string
raw []byte
}
type FileResponse struct {
headers [][2]string
disposition string
name string
}
var wsUpgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
}
func (this HttpStatusResponse) Get(path ...string) req.GlobalParams {
if this.body == nil {
return req.NewGlobalParam("", nil)
}
return this.body.Get(path...)
}
func (this HttpStatusResponse) Send(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(this.status)
if this.body != nil {
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)))
c.Close()
} else {
w.Write(this.raw)
}
}
func (this FileResponse) Get(path ...string) req.GlobalParams {
if resp, err := json.Marshal(map[string]any{
"name": this.name,
"disposition": this.disposition,
}); 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 FileResponse) Send(w http.ResponseWriter, r *http.Request) {
if _, err := os.Stat(this.Get("name").String()); err != nil && os.IsNotExist(err) {
w.WriteHeader(http.StatusNotFound)
} else {
if this.Get("disposition").String() != "" {
w.Header().Set("Content-Disposition", `attachment; filename="`+this.Get("disposition").String()+`"`)
}
for i := 0; i < len(this.headers); i++ {
w.Header().Set(this.headers[i][0], this.headers[i][1])
}
http.ServeFile(w, r, this.Get("name").String())
}
}
func NewHttpStatusResponse(status int, body req.HttpResponse) req.HttpResponse {
return &HttpStatusResponse{status: status, body: body}
}
func NewRawResponse(b []byte, headers ...[2]string) req.HttpResponse {
return &RawResponse{raw: b, headers: headers}
}
func NewFileResponse(name string, disposition string, headers ...[2]string) req.HttpResponse {
return &FileResponse{name: name, disposition: disposition, headers: headers}
}
func NewJsonResponse(b []byte) req.HttpResponse {
return NewRawResponse(b, [2]string{"Content-Type", "application/json"})
}
func NewMsgResponse(msg string, code int) req.HttpResponse {
return NewJsonResponse([]byte(fmt.Sprintf(`{"code": %d, "msg": %q}`, code, msg)))
}
func NewErrResponse(err *contracts.Err) req.HttpResponse {
return NewJsonResponse([]byte(fmt.Sprintf(`{"code": %d, "msg": %q}`, err.Code, err.Error())))
}
func HttpResponse(data any) req.HttpResponse {
var err error
// fmt.Printf("%#v \n", data)
switch v := data.(type) {
case req.HttpResponse:
return v
case contracts.Errno:
return NewMsgResponse(v.Error(), v.Code)
case *contracts.Errno:
return NewMsgResponse(v.Error(), v.Code)
case contracts.Err:
return NewErrResponse(&v)
case *contracts.Err:
return NewErrResponse(v)
case error:
err = v
case []byte:
if b, e := json.Marshal(map[string]any{
"code": contracts.OK.Code,
"msg": contracts.OK.Msg,
"data": json.RawMessage(v),
}); e == nil {
return NewJsonResponse(b)
} else {
err = e
}
default:
if b, e := json.Marshal(map[string]any{
"code": contracts.OK.Code,
"msg": contracts.OK.Msg,
"data": v,
}); e == nil {
return NewJsonResponse(b)
} else {
err = e
}
}
return NewErrResponse(contracts.NewErr(contracts.InternalServerError, err))
}