Beego-ROUTER Beego-ROUTER

司空鸿熙
2023-12-01

对GOPATH的配置
其实GOPATH可以使用多个路径 用来区别GOPATH的插件和开发 eg:/Users/Z/develop/gopath:/Users/Z/Dropbox/gopath

Beego的几个重要功能

new Creates a Beego application

run Run the application by starting a local development server

pack Compresses a Beego application into a single file

api Creates a Beego API application

beego路由类型
函数级别的注册 RESTFul (PAST,GET 。。。。)

controller级别的注册自动化映射

更加自动化的handler注册,即实现ServerHTTP(respost,request)函数都可以

基础路由

beego.Get(router,beego.FilterFunc)

beego.post(router,beego.FilterFunc)

beego.Put(router,beego.FilterFunc)

beego.Head(router,beego.FilterFunc)

beego.Option(router,beego.FilterFunc)

beego.Delete(router,beego.FilterFunc)

beego.Any(router,beego.FilterFunc)
// 基础Get请求 beego.Get("/", func(ctx *context.Context){     ctx.Output.Body([]byte("hello world")) })  // 基础post请求 beego.Post("/foo", fun(ctx *context.Context){     ctx.OutPut.Body([]byte("bar")) })  // 任意HTTP请求 beego.Any("/falice", fun(ctx *context.Context){     ctx.OutPut.Body([]byte("bar")) }) 

基础路由之controller
beego.Router(router,controller)

type ObjectController struct {     beego.Controller }  func (this *ObjectController) Get() {     obs := models.GetAll()     this.Data["json"] = obs     this.ServerJson() }  beego.Router("/object", &ObjectController{}) 

映射路由
// 一个RESTful的设计示例 beego.Router("/api/list",&ResrController{},"*:ListFood") beego.Router("/api/create",&ResrController{},"post:CreatFood") beego.Router("/api/updata",&ResrController{},"put:UpdateFood") beego.Router("/api/delete",&ResrController{},"delete:DeleteFood")  // 多个HTTP Method beego.Router("/api",&RestController{},"get,post:ApiFunc")  // 不同Method对应不同的函数 beego.Router("/simple",&SimpleController{})  

自动化路由
beego.AutoRouter(&controllers.ObjectController{})  // 请求如下 /object/login       // 调用ObjectController 中的 Login /object/logout      // 调用ObjectController 中的 Logout  

自动化路由之API模式解析
/controller/simple /controller/simple.html /controller/simple.json /controller/simple.rss 可以通过  this.Ctx.Input.Param(":ext")获取后缀 

namespace接口
//初始化 namespace
ns :=
beego.NewNamespace("/v1",
    beego.NSCond(func(ctx *context.Context) bool {
        if ctx.Input.Domain() == "api.beego.me" {
            return true
        }
        return false
    }),
    beego.NSBefore(auth),
    beego.NSGet("/notallowed", func(ctx *context.Context) {
        ctx.Output.Body([]byte("notAllowed"))
    }),
    beego.NSRouter("/version", &AdminController{}, "get:ShowAPIVersion"),
    beego.NSRouter("/changepassword", &UserController{}),
    beego.NSNamespace("/shop",
        beego.NSBefore(sentry),
        beego.NSGet("/:id", func(ctx *context.Context) {
            ctx.Output.Body([]byte("notAllowed"))
        }),
    ),
    beego.NSNamespace("/cms",
        beego.NSInclude(
            &controllers.MainController{},
            &controllers.CMSController{},
            &controllers.BlockController{},
        ),
    ),
)
//注册 namespace
beego.AddNamespace(ns)
ns:=NewNameSpace("V1")
ns.Cond(func(ctx *context.Context)bool{
    if ctx.Input.Domain() == "api.beego.mo" {
        return true
    }
    return false
})


Filter(action string,)
 类似资料: