mirror of
https://github.com/astaxie/beego.git
synced 2024-11-25 23:11:27 +00:00
support modules design!
// the follow code is write in modules: // GR:=NewGroupRouters() // GR.AddRouter("/login",&UserController,"get:Login") // GR.AddRouter("/logout",&UserController,"get:Logout") // GR.AddRouter("/register",&UserController,"get:Reg") // the follow code is write in app: // import "github.com/beego/modules/auth" // AddRouterGroup("/admin", auth.GR)
This commit is contained in:
parent
62ee48dcbf
commit
803d91c077
64
beego.go
64
beego.go
@ -16,10 +16,60 @@ const VERSION = "1.0.1"
|
|||||||
type hookfunc func() error //hook function to run
|
type hookfunc func() error //hook function to run
|
||||||
var hooks []hookfunc //hook function slice to store the hookfunc
|
var hooks []hookfunc //hook function slice to store the hookfunc
|
||||||
|
|
||||||
func init() {
|
type groupRouter struct {
|
||||||
hooks = make([]hookfunc, 0)
|
pattern string
|
||||||
//init mime
|
controller ControllerInterface
|
||||||
AddAPPStartHook(initMime)
|
mappingMethods string
|
||||||
|
}
|
||||||
|
|
||||||
|
// RouterGroups which will store routers
|
||||||
|
type GroupRouters []groupRouter
|
||||||
|
|
||||||
|
// Get a new GroupRouters
|
||||||
|
func NewGroupRouters() GroupRouters {
|
||||||
|
return make([]groupRouter, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add Router in the GroupRouters
|
||||||
|
// it is for plugin or module to register router
|
||||||
|
func (gr GroupRouters) AddRouter(pattern string, c ControllerInterface, mappingMethod ...string) {
|
||||||
|
var newRG groupRouter
|
||||||
|
if len(mappingMethod) > 0 {
|
||||||
|
newRG = groupRouter{
|
||||||
|
pattern,
|
||||||
|
c,
|
||||||
|
mappingMethod[0],
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
newRG = groupRouter{
|
||||||
|
pattern,
|
||||||
|
c,
|
||||||
|
"",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
gr = append(gr, newRG)
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddGroupRouter with the prefix
|
||||||
|
// it will register the router in BeeApp
|
||||||
|
// the follow code is write in modules:
|
||||||
|
// GR:=NewGroupRouters()
|
||||||
|
// GR.AddRouter("/login",&UserController,"get:Login")
|
||||||
|
// GR.AddRouter("/logout",&UserController,"get:Logout")
|
||||||
|
// GR.AddRouter("/register",&UserController,"get:Reg")
|
||||||
|
// the follow code is write in app:
|
||||||
|
// import "github.com/beego/modules/auth"
|
||||||
|
// AddRouterGroup("/admin", auth.GR)
|
||||||
|
func AddGroupRouter(prefix string, groups GroupRouters) *App {
|
||||||
|
for _, v := range groups {
|
||||||
|
if v.mappingMethods != "" {
|
||||||
|
BeeApp.Router(prefix+v.pattern, v.controller, v.mappingMethods)
|
||||||
|
} else {
|
||||||
|
BeeApp.Router(prefix+v.pattern, v.controller)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return BeeApp
|
||||||
}
|
}
|
||||||
|
|
||||||
// Router adds a patterned controller handler to BeeApp.
|
// Router adds a patterned controller handler to BeeApp.
|
||||||
@ -151,3 +201,9 @@ func Run() {
|
|||||||
|
|
||||||
BeeApp.Run()
|
BeeApp.Run()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
hooks = make([]hookfunc, 0)
|
||||||
|
//init mime
|
||||||
|
AddAPPStartHook(initMime)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user