代码拉取完成,页面将自动刷新
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
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。