github.com/bingoohuang/gg@v0.0.0-20240325092523-45da7dee9335/pkg/ginx/anyfn/direct.go (about) 1 package anyfn 2 3 import ( 4 "net/http" 5 6 "github.com/bingoohuang/gg/pkg/ginx" 7 "github.com/gin-gonic/gin" 8 ) 9 10 // DirectResponse represents the direct response. 11 type DirectResponse struct { 12 Code int 13 Error error 14 JSON interface{} 15 String string 16 ContentType string 17 Header map[string]string 18 } 19 20 func (d DirectResponse) Deal(c *gin.Context) { 21 if d.Code == 0 { 22 if d.Error != nil { 23 d.Code = http.StatusInternalServerError 24 } else { 25 d.Code = http.StatusOK 26 } 27 } 28 29 if d.ContentType != "" { 30 c.Header("Content-Type", d.ContentType) 31 } 32 33 for k, v := range d.Header { 34 c.Header(k, v) 35 } 36 37 if d.Error != nil { 38 errString := d.Error.Error() 39 c.String(d.Code, errString) 40 return 41 } 42 43 if d.JSON != nil { 44 c.Render(d.Code, ginx.JSONRender{Data: d.JSON}) 45 return 46 } 47 48 if d.String != "" { 49 c.String(d.Code, d.String) 50 return 51 } 52 53 c.Status(d.Code) 54 }