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.
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`
this function is init the Context, ChildStruct' name and the Controller's variables.
- Prepare()
this function is Run before the HTTP METHOD's Function,as follow defined. In the ChildStruct you can define this function to auth user or init database et.
- Get()
When the HTTP' Method is GET, the beego router will run this function.Default is HTTP-403. In the ChildStruct you must define the same functon to logical processing.
- Post()
When the HTTP' Method is POST, the beego router will run this function.Default is HTTP-403. In the ChildStruct you must define the same functon to logical processing.
- Delete()
When the HTTP' Method is DELETE, the beego router will run this function.Default is HTTP-403. In the ChildStruct you must define the same functon to logical processing.
- Put()
When the HTTP' Method is PUT, the beego router will run this function.Default is HTTP-403. In the ChildStruct you must define the same functon to logical processing.
- Head()
When the HTTP' Method is HEAD, the beego router will run this function.Default is HTTP-403. In the ChildStruct you must define the same functon to logical processing.
- Patch()
When the HTTP' Method is PATCH, the beego router will run this function.Default is HTTP-403. In the ChildStruct you must define the same functon to logical processing.
- Options()
When the HTTP' Method is OPTIONS, the beego router will run this function.Default is HTTP-403. In the ChildStruct you must define the same functon to logical processing.
- Finish()
this function is run after the HTTP METHOD's Function,as previous defined. In the ChildStruct you can define this function to close database et.
- Render() error
this function is to render the template as user defined. In the strcut you need to call.
So you can define ChildStruct method to accomplish the interface's method, now let us see an example:
MarkDown parses a string in MarkDown format and returns HTML. Used by the template parser as "markdown"
- beegoTplFuncMap["dateformat"] = DateFormat
DateFormat takes a time and a layout string and returns a string with the formatted date. Used by the template parser as "dateformat"
- beegoTplFuncMap["compare"] = Compare
Compare is a quick and dirty comparison function. It will convert whatever you give it to strings and see if the two values are equal.Whitespace is trimmed. Used by the template parser as "eq"
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.
Helper function for serving Json, sets content type to application/json:
beego has many default variables, as follow is a list to show:
- BeeApp *App
global app init by the beego. You needn't to init it, just use it.
- AppName string
appname is what you project named, default is beego
- AppPath string
this is the project path
- StaticDir map[string]string
staticdir store the map which request url to the static file path
default is the request url has prefix `static`, then server the path in the app path
- HttpAddr string
http listen address, defult is ""
- HttpPort int
http listen port, default is 8080
- RecoverPanic bool
RecoverPanic mean when the program panic whether the process auto recover,default is true
- AutoRender bool
whether run the Render function, default is true
- ViewsPath string
the template path, default is /views
- RunMode string //"dev" or "prod"
the runmode ,default is prod
- AppConfig *Config
Appconfig is a result that parse file from conf/app.conf, if this file not exist then the variable is nil. if the file exist, then return the Config as follow.
default is false. turn on pprof, if set to true. you can visit like this:
/debug/pprof
/debug/pprof/cmdline
/debug/pprof/profile
/debug/pprof/symbol
this serves via its HTTP server runtime profiling data in the format expected by the pprof visualization tool. For more information about pprof, see http://golang.org/pkg/net/http/pprof/
- Trace - For pervasive information on states of all elementary constructs. Use 'Trace' for in-depth debugging to find problem parts of a function, to check values of temporary variables, etc.
- Debug - For detailed system behavior reports and diagnostic messages to help to locate problems during development.
- Info - For general information on the application's work. Use 'Info' level in your code so that you could leave it 'enabled' even in production. So it is a 'production log level'.
- Warn - For indicating small errors, strange situations, failures that are automatically handled in a safe manner.
- Error - For severe failures that affects application's workflow, not fatal, however (without forcing app shutdown).
- Critical - For producing final messages before application’s death. Note: critical messages force immediate flush because in critical situation it is important to avoid log message losses if app crashes.
- Off - A special log level used to turn off logging
beego has follow functions:
- Trace(v ...interface{})
- Debug(v ...interface{})
- Info(v ...interface{})
- Warn(v ...interface{})
- Error(v ...interface{})
- Critical(v ...interface{})
you can set log levels like this :
beego.SetLevel(beego.LevelError)
after set the log levels, in the logs function which below the setlevels willn't output anything