803d91c0 by astaxie

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)
1 parent 62ee48dc
Showing 1 changed file with 60 additions and 4 deletions
...@@ -16,10 +16,60 @@ const VERSION = "1.0.1" ...@@ -16,10 +16,60 @@ const VERSION = "1.0.1"
16 type hookfunc func() error //hook function to run 16 type hookfunc func() error //hook function to run
17 var hooks []hookfunc //hook function slice to store the hookfunc 17 var hooks []hookfunc //hook function slice to store the hookfunc
18 18
19 func init() { 19 type groupRouter struct {
20 hooks = make([]hookfunc, 0) 20 pattern string
21 //init mime 21 controller ControllerInterface
22 AddAPPStartHook(initMime) 22 mappingMethods string
23 }
24
25 // RouterGroups which will store routers
26 type GroupRouters []groupRouter
27
28 // Get a new GroupRouters
29 func NewGroupRouters() GroupRouters {
30 return make([]groupRouter, 0)
31 }
32
33 // Add Router in the GroupRouters
34 // it is for plugin or module to register router
35 func (gr GroupRouters) AddRouter(pattern string, c ControllerInterface, mappingMethod ...string) {
36 var newRG groupRouter
37 if len(mappingMethod) > 0 {
38 newRG = groupRouter{
39 pattern,
40 c,
41 mappingMethod[0],
42 }
43 } else {
44 newRG = groupRouter{
45 pattern,
46 c,
47 "",
48 }
49 }
50 gr = append(gr, newRG)
51 }
52
53 // AddGroupRouter with the prefix
54 // it will register the router in BeeApp
55 // the follow code is write in modules:
56 // GR:=NewGroupRouters()
57 // GR.AddRouter("/login",&UserController,"get:Login")
58 // GR.AddRouter("/logout",&UserController,"get:Logout")
59 // GR.AddRouter("/register",&UserController,"get:Reg")
60 // the follow code is write in app:
61 // import "github.com/beego/modules/auth"
62 // AddRouterGroup("/admin", auth.GR)
63 func AddGroupRouter(prefix string, groups GroupRouters) *App {
64 for _, v := range groups {
65 if v.mappingMethods != "" {
66 BeeApp.Router(prefix+v.pattern, v.controller, v.mappingMethods)
67 } else {
68 BeeApp.Router(prefix+v.pattern, v.controller)
69 }
70
71 }
72 return BeeApp
23 } 73 }
24 74
25 // Router adds a patterned controller handler to BeeApp. 75 // Router adds a patterned controller handler to BeeApp.
...@@ -151,3 +201,9 @@ func Run() { ...@@ -151,3 +201,9 @@ func Run() {
151 201
152 BeeApp.Run() 202 BeeApp.Run()
153 } 203 }
204
205 func init() {
206 hooks = make([]hookfunc, 0)
207 //init mime
208 AddAPPStartHook(initMime)
209 }
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!