1 Star 1 Fork 0

oshine/murphy

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
complex.go 2.16 KB
一键复制 编辑 原始数据 按行查看 历史
taiyun 提交于 2024-07-19 16:56 . up
package main
import (
"fmt"
"gitee.com/oshine/go-murphy/core"
"gitee.com/oshine/go-murphy/core/text"
"github.com/goccy/go-json"
"github.com/valyala/fasthttp"
"log"
"mime/multipart"
)
func main() {
svr := core.Server{}
svr.Http().
Group("/api", func(group *core.SvrGroup) {
group.Auth("AuthMember")
group.Home("/member/index")
group.Bind(
&MemberController{},
)
}).RunTLS("127.0.0.1:18000", "", "")
}
type MemberController struct {
BaseController
AuthData string
}
// AuthMember 首页
func (mc *MemberController) AuthMember(ctx *fasthttp.RequestCtx, prefix, action string) bool {
if prefix == "/api" {
mc.AuthData = "member_id=1"
return true
}
fmt.Println("router:", prefix, action)
ctx.SetContentType("application/json")
ctx.SetStatusCode(fasthttp.StatusOK)
mc.RenderJson(nil).Err("Auth fail").To(ctx)
return false
}
// Index 首页
func (mc *MemberController) Index(ctx *fasthttp.RequestCtx) error {
method := string(ctx.Request.Header.Method())
log.Println(method)
if text.InArray(method, []string{"GET", "POST", "PUT", "DELETE"}) {
return mc.RenderJson(mc.AuthData).Ok("Hello World").To(ctx)
}
return mc.RenderJson(nil).Ok("Not Support Method:" + method).To(ctx)
}
type BaseController struct {
_ctx *fasthttp.RequestCtx
_form *multipart.Form
}
func (m *BaseController) WithParamsCtx(ctx *fasthttp.RequestCtx) *BaseController {
m._ctx = ctx
m._form, _ = ctx.Request.MultipartForm()
return m
}
func (m *BaseController) Params(p string) string {
if m._form == nil {
return ""
}
var r = m._form.Value[p]
if len(r) > 0 {
return r[0]
}
return ""
}
func (m *BaseController) RenderJson(data any) *Msg {
return &Msg{Data: data}
}
type Msg struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data interface{} `json:"data"`
}
func (msg *Msg) Ok(msg1 string) *Msg {
msg.Msg = msg1
msg.Code = 0
return msg
}
func (msg *Msg) Err(msg1 string) *Msg {
msg.Msg = msg1
msg.Code = -1
return msg
}
func (msg *Msg) To(ctx *fasthttp.RequestCtx) error {
b, err := json.Marshal(msg)
if err != nil {
return err
}
ctx.Response.Header.Set("Content-Type", "application/json")
ctx.Response.SetBody(b)
return nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/oshine/murphy.git
git@gitee.com:oshine/murphy.git
oshine
murphy
murphy
v1.0.16

搜索帮助

0d507c66 1850385 C8b1a773 1850385