1 Star 0 Fork 0

lipore/plume

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
rest.go 2.32 KB
一键复制 编辑 原始数据 按行查看 历史
lipore 提交于 2022-08-19 15:35 . fix(1.2): fix issues
package web
import (
"encoding/json"
"fmt"
"gitee.com/lipore/plume/errors"
"net/http"
"strconv"
)
type RestFunc func(r *http.Request) (interface{}, error)
type RestResponse struct {
Data interface{} `json:"data"`
Status string `json:"status"`
Msg string `json:"msg"`
}
type ApiStatus struct {
code string
module string
}
func NewApiStatus(code string, module string) ApiStatus {
return ApiStatus{code, module}
}
func (s *ApiStatus) toString() string {
return fmt.Sprintf("%s-%s", s.module, s.code)
}
var UnknownError = NewApiStatus("system", "0")
func NewRestError(module string, code string, msg string, err error) *RestError {
return &RestError{
Status: NewApiStatus(code, module),
Msg: msg,
err: err,
}
}
type RestError struct {
Status ApiStatus
Msg string
err error
}
func (r *RestError) Error() string {
if r.Msg == "" && r.err != nil {
return r.err.Error()
}
return r.Msg
}
func (r *RestError) Unwrap() error {
return r.err
}
func ResponseJson(response interface{}, statusCode int, w http.ResponseWriter) {
responseStr, err := json.Marshal(response)
if err != nil {
panic(err)
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(statusCode)
_, _ = w.Write(responseStr)
}
func RestControllerHandler(handle RestFunc) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
response, err := handle(r)
res := &RestResponse{Data: response, Status: "ok", Msg: ""}
statusCode := http.StatusOK
if err != nil {
if rErr, ok := err.(*RestError); ok {
res.Status = rErr.Status.toString()
} else {
coder := errors.ParseCoder(err)
statusCode = coder.HTTPStatus()
res.Status = strconv.FormatInt(int64(coder.Code()), 10)
res.Data = nil
}
res.Msg = err.Error()
}
ResponseJson(res, statusCode, w)
})
}
type CRUDApi interface {
Get(r *http.Request) (interface{}, error)
List(r *http.Request) (interface{}, error)
Create(r *http.Request) (interface{}, error)
Update(r *http.Request) (interface{}, error)
Delete(r *http.Request) (interface{}, error)
}
func RegisterCRUDApi(route *Route, pathPrefix string, api CRUDApi) {
route.RestMethodRoute(pathPrefix).GET(api.List).POST(api.Create)
route.RestMethodRoute(fmt.Sprintf("%s/{:id}", pathPrefix)).GET(api.Get).PUT(api.Update).DELETE(api.Delete)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/lipore/plume.git
git@gitee.com:lipore/plume.git
lipore
plume
plume
v1.7.10

搜索帮助