Skip to content

Commit

Permalink
Revert "feat: supports adding page info to http response"
Browse files Browse the repository at this point in the history
This reverts commit e6753f7.
  • Loading branch information
SongZhen0704 committed Jan 19, 2025
1 parent 484dcb7 commit 1a779d2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 134 deletions.
2 changes: 1 addition & 1 deletion server/controller/http/router/common/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"github.com/op/go-logging"
)

var log = logging.MustGetLogger("http.router.common")
var log = logging.MustGetLogger("common/controller")

func ForwardMasterController(c *gin.Context, masterControllerName string, port int) {
requestHosts := strings.Split(c.Request.Host, ":")
Expand Down
98 changes: 28 additions & 70 deletions server/controller/http/router/common/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package common

import (
"encoding/json"
"errors"
"fmt"
"net/http"
Expand All @@ -32,54 +31,14 @@ type Response struct {
OptStatus string `json:"OPT_STATUS"`
Description string `json:"DESCRIPTION"`
Data interface{} `json:"DATA"`
Page Page `json:"PAGE"`
}

// String returns the string representation of the response when Data is a byte slice.
func (r Response) String() string {
return fmt.Sprintf(`{"OPT_STATUS":%s,"DESCRIPTION":%s,"DATA":%s,"PAGE":%s}`, r.OptStatus, r.Description, string(r.Data.([]byte)), r.Page.String())
}

// Bytes returns the byte slice representation of the response when Data is a byte slice.
func (r Response) Bytes() []byte {
return []byte(r.String())
}

type Page struct {
Index int `json:"INDEX"`
Size int `json:"SIZE"`
Total int `json:"TOTAL"`
TotalItem int `json:"TOTAL_ITEM"`
}

func (p Page) String() string {
bytes, _ := json.Marshal(p)
return string(bytes)
}

type ResponseOption func(resp *Response)

func WithPage(p Page) ResponseOption {
return func(resp *Response) {
resp.Page = p
}
}

func WithDescription(description string) ResponseOption {
return func(resp *Response) {
resp.Description = description
}
}

func HttpResponse(c *gin.Context, httpCode int, data interface{}, optStatus string, options ...ResponseOption) {
resp := Response{
OptStatus: optStatus,
Data: data,
}
for _, option := range options {
option(&resp)
}
c.JSON(httpCode, resp)
func HttpResponse(c *gin.Context, httpCode int, data interface{}, optStatus string, description string) {
c.JSON(httpCode, Response{
OptStatus: optStatus,
Description: description,
Data: data,
})
}

func BadRequestResponse(c *gin.Context, optStatus string, description string) {
Expand Down Expand Up @@ -120,10 +79,9 @@ func StatusForbiddenResponse(c *gin.Context, description string) {
})
}

// TODO refactor, use ResponseOption
func JsonResponse(c *gin.Context, data interface{}, err error, respOptions ...ResponseOption) {
func JsonResponse(c *gin.Context, data interface{}, err error) {
if _, ok := data.([]byte); ok {
bytesResponse(c, data, err, respOptions...)
bytesResponse(c, data, err)
return
}

Expand Down Expand Up @@ -152,47 +110,47 @@ func JsonResponse(c *gin.Context, data interface{}, err error, respOptions ...Re
InternalErrorResponse(c, data, httpcommon.FAIL, err.Error())
}
} else {
HttpResponse(c, 200, data, httpcommon.SUCCESS, respOptions...)
HttpResponse(c, 200, data, httpcommon.SUCCESS, "")
}
}

func bytesResponse(c *gin.Context, data interface{}, err error, respOptions ...ResponseOption) {
resp := Response{Data: data}
for _, opt := range respOptions {
opt(&resp)
}
func bytesResponse(c *gin.Context, data interface{}, err error) {
if err != nil {
switch t := err.(type) {
case *servicecommon.ServiceError:
resp.OptStatus = t.Status
resp.Description = t.Message
switch t.Status {
case httpcommon.NO_PERMISSIONS:
c.Data(http.StatusForbidden, gin.MIMEJSON, resp.Bytes())
d := fmt.Sprintf(`{"OPT_STATUS":%s,"DESCRIPTION":%s,"DATA":%s}`, t.Status, t.Message, string(data.([]byte)))
c.Data(http.StatusForbidden, gin.MIMEJSON, []byte(d))
case httpcommon.RESOURCE_NOT_FOUND, httpcommon.INVALID_POST_DATA, httpcommon.RESOURCE_NUM_EXCEEDED,
httpcommon.SELECTED_RESOURCES_NUM_EXCEEDED, httpcommon.RESOURCE_ALREADY_EXIST,
httpcommon.PARAMETER_ILLEGAL, httpcommon.INVALID_PARAMETERS:
c.Data(http.StatusBadRequest, gin.MIMEJSON, resp.Bytes())
d := fmt.Sprintf(`{"OPT_STATUS":%s,"DESCRIPTION":%s,"DATA":%s}`, t.Status, t.Message, string(data.([]byte)))
c.Data(http.StatusBadRequest, gin.MIMEJSON, []byte(d))
case httpcommon.SERVER_ERROR, httpcommon.CONFIG_PENDING:
c.Data(http.StatusInternalServerError, gin.MIMEJSON, resp.Bytes())
d := fmt.Sprintf(`{"OPT_STATUS":%s,"DESCRIPTION":%s,"DATA":%s}`, t.Status, t.Message, string(data.([]byte)))
c.Data(http.StatusInternalServerError, gin.MIMEJSON, []byte(d))
case httpcommon.SERVICE_UNAVAILABLE:
c.Data(http.StatusServiceUnavailable, gin.MIMEJSON, resp.Bytes())
d := fmt.Sprintf(`{"OPT_STATUS":%s,"DESCRIPTION":%s,"DATA":%s}`, t.Status, t.Message, string(data.([]byte)))
c.Data(http.StatusServiceUnavailable, gin.MIMEJSON, []byte(d))
case httpcommon.STATUES_PARTIAL_CONTENT:
c.Data(http.StatusPartialContent, gin.MIMEJSON, resp.Bytes())
d := fmt.Sprintf(`{"OPT_STATUS":%s,"DESCRIPTION":%s,"DATA":%s}`, t.Status, t.Message, string(data.([]byte)))
c.Data(http.StatusPartialContent, gin.MIMEJSON, []byte(d))
default:
if errors.Is(err, httpcommon.ERR_NO_PERMISSIONS) {
c.Data(http.StatusForbidden, gin.MIMEJSON, resp.Bytes())
d := fmt.Sprintf(`{"OPT_STATUS":%s,"DESCRIPTION":%s,"DATA":%s}`, t.Status, t.Message, string(data.([]byte)))
c.Data(http.StatusForbidden, gin.MIMEJSON, []byte(d))
return
}
c.Data(http.StatusBadRequest, gin.MIMEJSON, resp.Bytes())
d := fmt.Sprintf(`{"OPT_STATUS":%s,"DESCRIPTION":%s,"DATA":%s}`, t.Status, t.Message, string(data.([]byte)))
c.Data(http.StatusBadRequest, gin.MIMEJSON, []byte(d))
}
default:
resp.OptStatus = httpcommon.FAIL
resp.Description = err.Error()
c.Data(http.StatusInternalServerError, gin.MIMEJSON, resp.Bytes())
d := fmt.Sprintf(`{"OPT_STATUS":%s,"DESCRIPTION":%s,"DATA":%s}`, httpcommon.FAIL, err.Error(), string(data.([]byte)))
c.Data(http.StatusInternalServerError, gin.MIMEJSON, []byte(d))
}
} else {
resp.OptStatus = httpcommon.SUCCESS
c.Data(http.StatusOK, gin.MIMEJSON, resp.Bytes())
d := fmt.Sprintf(`{"OPT_STATUS":"SUCCESS","DESCRIPTION":"","DATA": %v}`, string(data.([]byte)))
c.Data(http.StatusOK, gin.MIMEJSON, []byte(d))
}
}
63 changes: 0 additions & 63 deletions server/controller/http/router/common/response_test.go

This file was deleted.

0 comments on commit 1a779d2

Please sign in to comment.