* beego 默认可以通过 swagger 来创建路由,大概的形式是在 route.go 文件中添加如下代码:

var names []*beego.Namespace
ns := beego.NewNamespace("/myapp",
beego.NSNamespace("/info", beego.NSInclude(&app.InfoController{})),
)
names = append(names, ns)
beego.AddNamespace(names...)

* 然后在 InfoController 中再添加对应的路由注解,如:

// @Title 测试信息
// @Description 用来测试的接口
// @Param timestamp query int true "timestamp,时间戳"
// @Param sign query string true "sign,签名"
// @Param id query int true "Id"
// @Success 200 {object} SendInfo
// @router /info [get]
func (this *InfoController) info() {

* 但有时候,因为注解错误,swagger 无法正确生成路由文件,导致路由失败,查找错误很麻烦

* 所以可以使用 beego 自己的路由方法,通过 AssembleRouteNameSpace 和 web.Router 来创建自定义路由

* 以下代码是对 beego 框架的 get,post,put,delete 请求路由进行二次封装的示例代码,主要思路是模拟 php laravel 的 namespace 组下的路由及子路由组合

* 以下示例基于 beego v2.x 版本, 如果是 beego v1.x 版本,则需要修改 import 的包名,将    beego "github.com/beego/beego/v2/server/web" 修改为 "github.com/astaxie/beego" 即可

* 首先需要在routers目录下创建一个路由封装类,文件名为 routerext.go


package routers

import (
	"fmt"
	"strings"

	beego "github.com/beego/beego/v2/server/web"
)

// get 路由
func Get(rootPath string, path string, c beego.ControllerInterface, mappingMethods string) {
	method := fmt.Sprintf("get:%s", mappingMethods)
	rootPath = AssembleRouteNameSpace(rootPath, path)
	beego.Router(rootPath, c, method)
}

// post 路由
func Post(rootPath string, path string, c beego.ControllerInterface, mappingMethods string) {
	method := fmt.Sprintf("post:%s", mappingMethods)
	rootPath = AssembleRouteNameSpace(rootPath, path)
	beego.Router(rootPath, c, method)
}

//put 路由
func Put(rootPath string, path string, c beego.ControllerInterface, mappingMethods string) {
	method := fmt.Sprintf("put:%s", mappingMethods)
	rootPath = AssembleRouteNameSpace(rootPath, path)
	beego.Router(rootPath, c, method)
}

//delete 路由
func Delete(rootPath string, path string, c beego.ControllerInterface, mappingMethods string) {
	method := fmt.Sprintf("delete:%s", mappingMethods)
	rootPath = AssembleRouteNameSpace(rootPath, path)
	beego.Router(rootPath, c, method)
}

// 组装路由,使其满足 /v1/app/info 这样的形式
func AssembleRouteNameSpace(rootPath string, nameSpace string) string {
	headIndex := strings.Index(nameSpace, "/")
	if headIndex != 0 {
		// 不是以 / 开头的命名
		if rootPath != "/" {
			return fmt.Sprintf("/%s", nameSpace)
		}
	}
	if rootPath != "/" {
		// 避免 //
		return fmt.Sprintf("%s%s", rootPath, nameSpace)
	}

	return nameSpace
}

type RouterCallbackMethod func(rootPath string)

type NameSpaceRouter struct {
	// 命名路径前缀,下面的子路由都属于此命名之下
	NameSpace string
	// 此命名下的路由回调
	Router RouterCallbackMethod
	// 此命名下的子路由
	ChildRouters []*NameSpaceRouter
}

// 主命名的路由,根节点
type RootNameSpaceRouter struct {
	NameSpaceRouter
}

func (t *RootNameSpaceRouter) MakeRouter() {
	t.initRouter("")
}

func (t *NameSpaceRouter) initRouter(rootPath string) {
	rootPath = AssembleRouteNameSpace(rootPath, t.NameSpace)
	if t.Router != nil {
		t.Router(rootPath)
	}
	if t.ChildRouters != nil {
		for _, value := range t.ChildRouters {
			value.initRouter(rootPath)
		}
	}
}

func InitRouter() {
	//一些自定义初始化操作

}

* 之后在routers目录下创建 router.go, 使用上面的封装类来创建自定义路由, 以下示例会创建  /v100/mypath/event 和 /v100/mypath/event/count 两个 post 路由


package routers

import (
	v10000 "myproject/controllers/app/v10000"
)

func init() {

	InitRouter()

	routerWs := wsRouter()
	childRouters := []*NameSpaceRouter{
		routerWs,
	}

	rootRouter := RootNameSpaceRouter{
		NameSpaceRouter: NameSpaceRouter{
			NameSpace: "/",
			Router: func(rootPath string) {

			},
			ChildRouters: childRouters,
		},
	}

	rootRouter.MakeRouter()
}

// /v100/mypath/event
func wsRouter() *NameSpaceRouter {
	routerV100 := NameSpaceRouter{
		NameSpace: "/v100",
		Router: func(rootPath string) {

		},
		ChildRouters: []*NameSpaceRouter{
			eventRouter(),
		},
	}

	routerWs := NameSpaceRouter{
		NameSpace: "/mypath",
		Router: func(rootPath string) {

		},
		ChildRouters: []*routersExt.NameSpaceRouter{
			&routerV100,
		},
	}

	return &routerWs
}

// 事件路由
func eventRouter() *NameSpaceRouter {
	routerWs := NameSpaceRouter{
		NameSpace: "/event",
		Router: func(rootPath string) {
			routersExt.Post(rootPath, "/", &v10000.InfosController{}, "Event")
			routersExt.Post(rootPath, "/count", &v10000.InfosController{}, "EventCount")
		},
	}

	return &routerWs
}

点赞(0) 打赏

微信小程序

微信扫一扫体验

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部