1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 07:50:54 +00:00

simplify beego

This commit is contained in:
xiemengjun 2012-12-17 22:15:21 +08:00
parent ad892008cc
commit d562687868
4 changed files with 79 additions and 14 deletions

View File

@ -34,8 +34,8 @@ Here is the canonical "Hello, world" example app for beego:
} }
func main() { func main() {
beego.BeeApp.RegisterController("/", &MainController{}) beego.RegisterController("/", &MainController{})
beego.BeeApp.Run() beego.Run()
} }
default port:8080 default port:8080
@ -56,16 +56,16 @@ in other way i has writing a tool to quick create an app based on beego. you can
============ ============
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: 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:
beego.BeeApp.RegisterController("/", &controllers.MainController{}) beego.RegisterController("/", &controllers.MainController{})
beego.BeeApp.RegisterController("/admin", &admin.UserController{}) beego.RegisterController("/admin", &admin.UserController{})
beego.BeeApp.RegisterController("/admin/index", &admin.ArticleController{}) beego.RegisterController("/admin/index", &admin.ArticleController{})
beego.BeeApp.RegisterController("/admin/addpkg", &admin.AddController{}) beego.RegisterController("/admin/addpkg", &admin.AddController{})
You can specify custom regular expressions for routes: You can specify custom regular expressions for routes:
beego.BeeApp.RegisterController("/admin/editpkg/:id([0-9]+)", &admin.EditController{}) beego.RegisterController("/admin/editpkg/:id([0-9]+)", &admin.EditController{})
beego.BeeApp.RegisterController("/admin/delpkg/:id([0-9]+)", &admin.DelController{}) beego.RegisterController("/admin/delpkg/:id([0-9]+)", &admin.DelController{})
beego.BeeApp.RegisterController("/:pkg(.*)", &controllers.MainController{}) beego.RegisterController("/:pkg(.*)", &controllers.MainController{})
You can also create routes for static files: You can also create routes for static files:
@ -85,18 +85,18 @@ You can, for example, filter all request to enforce some type of security:
} }
} }
beego.BeeApp.Filter(FilterUser) beego.Filter(FilterUser)
You can also apply filters only when certain REST URL Parameters exist: You can also apply filters only when certain REST URL Parameters exist:
beego.BeeApp.RegisterController("/:id([0-9]+)", &admin.EditController{}) beego.RegisterController("/:id([0-9]+)", &admin.EditController{})
beego.BeeApp.FilterParam("id", func(rw http.ResponseWriter, r *http.Request) { beego.FilterParam("id", func(rw http.ResponseWriter, r *http.Request) {
... ...
}) })
also You can apply filters only when certain prefix URL path exist: also You can apply filters only when certain prefix URL path exist:
beego.BeeApp.FilterPrefixPath("/admin", func(rw http.ResponseWriter, r *http.Request) { beego.FilterPrefixPath("/admin", func(rw http.ResponseWriter, r *http.Request) {
… auth … auth
}) })

View File

@ -125,3 +125,27 @@ func (app *App) ErrorLog(ctx *Context) {
func (app *App) AccessLog(ctx *Context) { func (app *App) AccessLog(ctx *Context) {
BeeLogger.Printf("[ACC] host: '%s', request: '%s %s', proto: '%s', ua: %s'', remote: '%s'\n", ctx.Request.Host, ctx.Request.Method, ctx.Request.URL.Path, ctx.Request.Proto, ctx.Request.UserAgent(), ctx.Request.RemoteAddr) BeeLogger.Printf("[ACC] host: '%s', request: '%s %s', proto: '%s', ua: %s'', remote: '%s'\n", ctx.Request.Host, ctx.Request.Method, ctx.Request.URL.Path, ctx.Request.Proto, ctx.Request.UserAgent(), ctx.Request.RemoteAddr)
} }
func RegisterController(path string, c ControllerInterface) *App {
BeeApp.RegisterController(path, c)
return BeeApp
}
func Filter(filter http.HandlerFunc) *App {
BeeApp.Filter(filter)
return BeeApp
}
func FilterParam(param string, filter http.HandlerFunc) *App {
BeeApp.FilterParam(param, filter)
return BeeApp
}
func FilterPrefixPath(path string, filter http.HandlerFunc) *App {
BeeApp.FilterParam(path, filter)
return BeeApp
}
func Run() {
BeeApp.Run()
}

View File

@ -7,6 +7,7 @@ import (
"html/template" "html/template"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/url"
"path" "path"
"strconv" "strconv"
) )
@ -145,3 +146,8 @@ func (c *Controller) ServeXml() {
c.Ct.ContentType("xml") c.Ct.ContentType("xml")
c.Ct.ResponseWriter.Write(content) c.Ct.ResponseWriter.Write(content)
} }
func (c *Controller) Input() url.Values {
c.Ct.Request.ParseForm()
return c.Ct.Request.Form
}

View File

@ -1 +1,36 @@
package beego package beego
type BeeModel struct {
driver string
}
func (this *BeeModel) Insert() {
}
func (this *BeeModel) MultipleInsert() {
}
func (this *BeeModel) Update() {
}
func (this *BeeModel) Query() {
}
//Deletes from table with clauses where and using.
func (this *BeeModel) Delete() {
}
//Start a transaction
func (this *BeeModel) Transaction() {
}
//commit transaction
func (this *BeeModel) Commit() {
}