c8cf7c71 by Travis Cline

Update README.md

Add go syntax hilighting, fix odd wording
1 parent 4d4096c7
Showing 1 changed file with 42 additions and 44 deletions
...@@ -13,12 +13,10 @@ To install: ...@@ -13,12 +13,10 @@ To install:
13 13
14 go get github.com/astaxie/beego 14 go get github.com/astaxie/beego
15 15
16 go version: go1 release
17
18 ## Quick Start 16 ## Quick Start
19 ============ 17 ============
20 Here is the canonical "Hello, world" example app for beego: 18 Here is the canonical "Hello, world" example app for beego:
21 19 ```go
22 package main 20 package main
23 21
24 import ( 22 import (
...@@ -35,16 +33,12 @@ Here is the canonical "Hello, world" example app for beego: ...@@ -35,16 +33,12 @@ Here is the canonical "Hello, world" example app for beego:
35 33
36 func main() { 34 func main() {
37 beego.RegisterController("/", &MainController{}) 35 beego.RegisterController("/", &MainController{})
36 //beego.HttpPort = 8080 // default
38 beego.Run() 37 beego.Run()
39 } 38 }
40 39 ```
41 > in tip version the default listin on tcp6. so you must set HttpAddr:
42 40
43 > `beego.HttpAddr = "127.0.0.1"` 41 http get http://localhost:8080/
44
45 default port:8080
46
47 http get http://localhost:8080
48 HTTP/1.1 200 OK 42 HTTP/1.1 200 OK
49 Content-Type: text/plain; charset=utf-8 43 Content-Type: text/plain; charset=utf-8
50 Date: Sat, 15 Dec 2012 16:03:00 GMT 44 Date: Sat, 15 Dec 2012 16:03:00 GMT
...@@ -52,37 +46,37 @@ default port:8080 ...@@ -52,37 +46,37 @@ default port:8080
52 46
53 hello world 47 hello world
54 48
55 also i write an app based on beego. you can get the code from:[beepkg](https://github.com/astaxie/beepkg) 49 A more complete example use of beego exists here:[beepkg](https://github.com/astaxie/beepkg)
56 50
57 in other way i has writing a tool to quick create an app based on beego. you can get the tools from [bee](https://github.com/astaxie/bee) 51 Some associated tools for beego reside in:[bee](https://github.com/astaxie/bee)
58 52
59 ## Router 53 ## Router
60 ============ 54 ============
61 In beego, a route is a struct paired with a URL-matching pattern. The strcut has many method with the same name of http method to server the http request. Each route is associated with a block: 55 In beego, a route is a struct paired with a URL-matching pattern. The struct has many method with the same name of http method to serve the http response. Each route is associated with a block.
62 56 ```go
63 beego.RegisterController("/", &controllers.MainController{}) 57 beego.RegisterController("/", &controllers.MainController{})
64 beego.RegisterController("/admin", &admin.UserController{}) 58 beego.RegisterController("/admin", &admin.UserController{})
65 beego.RegisterController("/admin/index", &admin.ArticleController{}) 59 beego.RegisterController("/admin/index", &admin.ArticleController{})
66 beego.RegisterController("/admin/addpkg", &admin.AddController{}) 60 beego.RegisterController("/admin/addpkg", &admin.AddController{})
67 61 ```
68 You can specify custom regular expressions for routes: 62 You can specify custom regular expressions for routes:
69 63 ```go
70 beego.RegisterController("/admin/editpkg/:id([0-9]+)", &admin.EditController{}) 64 beego.RegisterController("/admin/editpkg/:id([0-9]+)", &admin.EditController{})
71 beego.RegisterController("/admin/delpkg/:id([0-9]+)", &admin.DelController{}) 65 beego.RegisterController("/admin/delpkg/:id([0-9]+)", &admin.DelController{})
72 beego.RegisterController("/:pkg(.*)", &controllers.MainController{}) 66 beego.RegisterController("/:pkg(.*)", &controllers.MainController{})
73 67 ```
74 You can also create routes for static files: 68 You can also create routes for static files:
75 69
76 beego.BeeApp.SetStaticPath("/static","/public") 70 beego.BeeApp.SetStaticPath("/static","/public")
77 71
78 this will serve any files in /static, including files in subdirectories. For example request `/static/logo.gif` or `/static/style/main.css` will server with the file in the path `/pulic/logo.gif` or `/public/style/main.css` 72 This will serve any files in /static, including files in subdirectories. For example request `/static/logo.gif` or `/static/style/main.css` will server with the file in the path `/pulic/logo.gif` or `/public/style/main.css`
79 73
80 ## Filters / Middleware 74 ## Filters / Middleware
81 ============ 75 ============
82 You can apply filters to routes, which is useful for enforcing security, redirects, etc. 76 You can apply filters to routes, which is useful for enforcing security, redirects, etc.
83 77
84 You can, for example, filter all request to enforce some type of security: 78 You can, for example, filter all request to enforce some type of security:
85 79 ```go
86 var FilterUser = func(w http.ResponseWriter, r *http.Request) { 80 var FilterUser = func(w http.ResponseWriter, r *http.Request) {
87 if r.URL.User == nil || r.URL.User.Username() != "admin" { 81 if r.URL.User == nil || r.URL.User.Username() != "admin" {
88 http.Error(w, "", http.StatusUnauthorized) 82 http.Error(w, "", http.StatusUnauthorized)
...@@ -90,30 +84,30 @@ You can, for example, filter all request to enforce some type of security: ...@@ -90,30 +84,30 @@ You can, for example, filter all request to enforce some type of security:
90 } 84 }
91 85
92 beego.Filter(FilterUser) 86 beego.Filter(FilterUser)
93 87 ```
94 You can also apply filters only when certain REST URL Parameters exist: 88 You can also apply filters only when certain REST URL Parameters exist:
95 89 ```go
96 beego.RegisterController("/:id([0-9]+)", &admin.EditController{}) 90 beego.RegisterController("/:id([0-9]+)", &admin.EditController{})
97 beego.FilterParam("id", func(rw http.ResponseWriter, r *http.Request) { 91 beego.FilterParam("id", func(rw http.ResponseWriter, r *http.Request) {
98 ... 92 ...
99 }) 93 })
100 94 ```
101 also You can apply filters only when certain prefix URL path exist: 95 Additionally, You can apply filters only when certain prefix URL path exist:
102 96 ```go
103 beego.FilterPrefixPath("/admin", func(rw http.ResponseWriter, r *http.Request) { 97 beego.FilterPrefixPath("/admin", func(rw http.ResponseWriter, r *http.Request) {
104 auth 98 auth
105 }) 99 })
106 100 ```
107 101
108 ## Controller / Struct 102 ## Controller / Struct
109 ============ 103 ============
110 you type a ChildStruct has anonymous type `beego.Controller` 104 To implement a beego Controller, embed the `beego.Controller` struct:
111 105 ```go
112 type xxxController struct { 106 type xxxController struct {
113 beego.Controller 107 beego.Controller
114 } 108 }
115 109 ```
116 the `beego.Controller` is `beego.ControllerInterface` has the follow method: 110 `beego.Controller` satisfieds the `beego.ControllerInterface` interface, which defines the following methods:
117 111
118 - Init(ct *Context, cn string) 112 - Init(ct *Context, cn string)
119 113
...@@ -161,7 +155,7 @@ the `beego.Controller` is `beego.ControllerInterface` has the follow method: ...@@ -161,7 +155,7 @@ the `beego.Controller` is `beego.ControllerInterface` has the follow method:
161 155
162 156
163 So you can define ChildStruct method to accomplish the interface's method, now let us see an example: 157 So you can define ChildStruct method to accomplish the interface's method, now let us see an example:
164 158 ```go
165 type AddController struct { 159 type AddController struct {
166 beego.Controller 160 beego.Controller
167 } 161 }
...@@ -196,12 +190,12 @@ So you can define ChildStruct method to accomplish the interface's method, now l ...@@ -196,12 +190,12 @@ So you can define ChildStruct method to accomplish the interface's method, now l
196 models.InsertArticle(at) 190 models.InsertArticle(at)
197 this.Ctx.Redirect(302, "/admin/index") 191 this.Ctx.Redirect(302, "/admin/index")
198 } 192 }
199 193 ```
200 ## View / Template 194 ## View / Template
201 ============ 195 ============
202 ### template view path 196 ### template view path
203 197
204 the default viewPath is `/views`,you can put the template file in the views.beego will find the template from viewpath. 198 The default viewPath is `/views`, you can put the template file in the views. beego will find the template from viewpath.
205 199
206 also you can modify the viewpaths like this: 200 also you can modify the viewpaths like this:
207 201
...@@ -222,34 +216,36 @@ So if the ChildName="AddController",Request Method= "POST",default TplEXT="tpl" ...@@ -222,34 +216,36 @@ So if the ChildName="AddController",Request Method= "POST",default TplEXT="tpl"
222 So beego will file the file in the path:`/view/AddController/POST.tpl` 216 So beego will file the file in the path:`/view/AddController/POST.tpl`
223 217
224 ### autoRender 218 ### autoRender
225 In the controller you needn't to call render function. beego will auto call this function after HTTP' Method Call. 219 In the controller you needn't to call render function. beego will auto call this function after HTTP Method Call.
226
227 also you can close the autoRendder like this:
228 220
221 You can disable automatic invokation of autorender via the AutoRender Flag:
222 ```go
229 beego.AutoRender = false 223 beego.AutoRender = false
230 224 ```
231 225
232 ### layout 226 ### layout
233 beego also support layout. beego's layout is like this: 227 beego supports layouts for views. For example:
234 228 ```go
235 this.Layout = "admin/layout.html" 229 this.Layout = "admin/layout.html"
236 this.TplNames = "admin/add.tpl" 230 this.TplNames = "admin/add.tpl"
231 ```
237 232
238 in the layout.html you must define the variable like this to show sub template's content: 233 In layout.html you must define the variable like this to show sub template's content:
239 234
240 {{.LayoutContent}} 235 {{.LayoutContent}}
241 236
242 beego first Parse the file TplNames defined, then get the content from the sub template to the data["LayoutContent"], at last Parse the layout file and show it. 237 beego first parses the TplNames files, renders their content, and appends it to data["LayoutContent"].
243 238
244 ### template function 239 ### template function
245 beego support users to define template function like this: 240 beego support users to define template function like this:
246 241 ```go
247 func hello(in string)(out string){ 242 func hello(in string)(out string){
248 out = in + "world" 243 out = in + "world"
249 return 244 return
250 } 245 }
251 246
252 beego.AddFuncMap("hi",hello) 247 beego.AddFuncMap("hi",hello)
248 ```
253 249
254 then in you template you can use it like this: 250 then in you template you can use it like this:
255 251
...@@ -273,17 +269,20 @@ beego has three default defined funtion: ...@@ -273,17 +269,20 @@ beego has three default defined funtion:
273 You can use `beego.Controller.ServeJson` or `beego.Controller.ServeXml` for serializing to Json and Xml. I found myself constantly writing code to serialize, set content type, content length, etc. Feel free to use these functions to eliminate redundant code in your app. 269 You can use `beego.Controller.ServeJson` or `beego.Controller.ServeXml` for serializing to Json and Xml. I found myself constantly writing code to serialize, set content type, content length, etc. Feel free to use these functions to eliminate redundant code in your app.
274 270
275 Helper function for serving Json, sets content type to application/json: 271 Helper function for serving Json, sets content type to application/json:
276 272 ```go
277 func (this *AddController) Get() { 273 func (this *AddController) Get() {
278 mystruct := { ... } 274 mystruct := { ... }
279 routes.ServeJson(w, &mystruct) 275 routes.ServeJson(w, &mystruct)
280 } 276 }
277 ```
281 Helper function for serving Xml, sets content type to application/xml: 278 Helper function for serving Xml, sets content type to application/xml:
282 279 ```go
283 func (this *AddController) Get() { 280 func (this *AddController) Get() {
284 mystruct := { ... } 281 mystruct := { ... }
285 routes.ServeXml(w, &mystruct) 282 routes.ServeXml(w, &mystruct)
286 } 283 }
284 ```
285
287 ## Beego Variables 286 ## Beego Variables
288 ============ 287 ============
289 beego has many default variables, as follow is a list to show: 288 beego has many default variables, as follow is a list to show:
...@@ -409,4 +408,3 @@ after set the log levels, in the logs function which below the setlevels willn't ...@@ -409,4 +408,3 @@ after set the log levels, in the logs function which below the setlevels willn't
409 after set levels to beego.LevelError 408 after set levels to beego.LevelError
410 409
411 Trace, Debug, Info, Warn will not output anything. So you can change it when in dev and prod mode. 410 Trace, Debug, Info, Warn will not output anything. So you can change it when in dev and prod mode.
412
...\ No newline at end of file ...\ No newline at end of file
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!