3f3bf299 by 傅小黑

add api comments in file app.go

1 parent 3f0e55de
Showing 1 changed file with 42 additions and 1 deletions
...@@ -10,19 +10,22 @@ import ( ...@@ -10,19 +10,22 @@ import (
10 "github.com/astaxie/beego/context" 10 "github.com/astaxie/beego/context"
11 ) 11 )
12 12
13 // FitlerFunc defines filter function type.
13 type FilterFunc func(*context.Context) 14 type FilterFunc func(*context.Context)
14 15
16 // App defines beego application with a new PatternServeMux.
15 type App struct { 17 type App struct {
16 Handlers *ControllerRegistor 18 Handlers *ControllerRegistor
17 } 19 }
18 20
19 // New returns a new PatternServeMux. 21 // NewApp returns a new beego application.
20 func NewApp() *App { 22 func NewApp() *App {
21 cr := NewControllerRegistor() 23 cr := NewControllerRegistor()
22 app := &App{Handlers: cr} 24 app := &App{Handlers: cr}
23 return app 25 return app
24 } 26 }
25 27
28 // Run beego application.
26 func (app *App) Run() { 29 func (app *App) Run() {
27 addr := HttpAddr 30 addr := HttpAddr
28 31
...@@ -84,39 +87,77 @@ func (app *App) Run() { ...@@ -84,39 +87,77 @@ func (app *App) Run() {
84 } 87 }
85 } 88 }
86 89
90 // Router adds a url-patterned controller handler.
91 // The path argument supports regex rules and specific placeholders.
92 // The c argument needs a controller handler implemented beego.ControllerInterface.
93 // The mapping methods argument only need one string to define custom router rules.
94 // usage:
95 // simple router
96 // beego.Router("/admin", &admin.UserController{})
97 // beego.Router("/admin/index", &admin.ArticleController{})
98 //
99 // regex router
100 //
101 // beego.Router(“/api/:id([0-9]+)“, &controllers.RController{})
102 //
103 // custom rules
104 // beego.Router("/api/list",&RestController{},"*:ListFood")
105 // beego.Router("/api/create",&RestController{},"post:CreateFood")
106 // beego.Router("/api/update",&RestController{},"put:UpdateFood")
107 // beego.Router("/api/delete",&RestController{},"delete:DeleteFood")
87 func (app *App) Router(path string, c ControllerInterface, mappingMethods ...string) *App { 108 func (app *App) Router(path string, c ControllerInterface, mappingMethods ...string) *App {
88 app.Handlers.Add(path, c, mappingMethods...) 109 app.Handlers.Add(path, c, mappingMethods...)
89 return app 110 return app
90 } 111 }
91 112
113 // AutoRouter adds beego-defined controller handler.
114 // if beego.AddAuto(&MainContorlller{}) and MainController has methods List and Page,
115 // visit the url /main/list to exec List function or /main/page to exec Page function.
92 func (app *App) AutoRouter(c ControllerInterface) *App { 116 func (app *App) AutoRouter(c ControllerInterface) *App {
93 app.Handlers.AddAuto(c) 117 app.Handlers.AddAuto(c)
94 return app 118 return app
95 } 119 }
96 120
121 // UrlFor does another controller handler with params in current context.
122 // The endpoint is formed as path.controller.name to defined the controller method which will run.
123 // The values need key-pair data to assign into controller method.
97 func (app *App) UrlFor(endpoint string, values ...string) string { 124 func (app *App) UrlFor(endpoint string, values ...string) string {
98 return app.Handlers.UrlFor(endpoint, values...) 125 return app.Handlers.UrlFor(endpoint, values...)
99 } 126 }
127
128 // [Deprecated]
129 // Filter adds a FilterFunc under pattern condition and named action.
130 // The actions contains BeforeRouter,AfterStatic,BeforeExec,AfterExec and FinishRouter.
100 func (app *App) Filter(pattern, action string, filter FilterFunc) *App { 131 func (app *App) Filter(pattern, action string, filter FilterFunc) *App {
101 app.Handlers.AddFilter(pattern, action, filter) 132 app.Handlers.AddFilter(pattern, action, filter)
102 return app 133 return app
103 } 134 }
104 135
136 // InsertFilter adds a FilterFunc with pattern condition and action constant.
137 // The pos means action constant including
138 // beego.BeforeRouter, beego.AfterStatic, beego.BeforeExec, beego.AfterExec and beego.FinishRouter.
105 func (app *App) InsertFilter(pattern string, pos int, filter FilterFunc) *App { 139 func (app *App) InsertFilter(pattern string, pos int, filter FilterFunc) *App {
106 app.Handlers.InsertFilter(pattern, pos, filter) 140 app.Handlers.InsertFilter(pattern, pos, filter)
107 return app 141 return app
108 } 142 }
109 143
144 // SetViewsPath sets view directory path in beego application.
145 // it returns beego application self.
110 func (app *App) SetViewsPath(path string) *App { 146 func (app *App) SetViewsPath(path string) *App {
111 ViewsPath = path 147 ViewsPath = path
112 return app 148 return app
113 } 149 }
114 150
151 // SetStaticPath sets static directory path and proper url pattern in beego application.
152 // if beego.SetStaticPath("static","public"), visit /static/* to load static file in folder "public".
153 // it returns beego application self.
115 func (app *App) SetStaticPath(url string, path string) *App { 154 func (app *App) SetStaticPath(url string, path string) *App {
116 StaticDir[url] = path 155 StaticDir[url] = path
117 return app 156 return app
118 } 157 }
119 158
159 // DelStaticPath removes the static folder setting in this url pattern in beego application.
160 // it returns beego application self.
120 func (app *App) DelStaticPath(url string) *App { 161 func (app *App) DelStaticPath(url string) *App {
121 delete(StaticDir, url) 162 delete(StaticDir, url)
122 return app 163 return app
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!