1
0
mirror of https://github.com/astaxie/beego.git synced 2024-12-23 18:30:50 +00:00
Beego/docs/en/Quickstart.md

1129 lines
33 KiB
Markdown
Raw Normal View History

2013-04-20 18:51:59 +00:00
# Quick start
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
Hey, you say you've never heard about Beego and don't know how to use it? Don't worry, after you read this section, you will know a lot about Beego. Before you start reading, make sure you installed Beego in your computer, if not, check this tutorial: [Installation](Install.md)
**Navigation**
2013-04-21 21:44:53 +00:00
- [Hello world](#hello-world)
- [New project](#new-project)
- [Development mode](#development-mode)
- [Router](#router)
2013-04-21 22:07:17 +00:00
- [Static files](#static-files)
- [Filter and middleware](#filter-and-middleware)
- [Controller](#controller)
- [Template](#template)
- [Handle request](#handle-request)
- [Redirect and error](#redirect-and-error)
- [Handle response](#handle-response)
2013-04-20 18:51:59 +00:00
- [Sessions](#sessions)
- [Cache](#cache)
- [Safe map](#safe-map)
- [Log](#log)
- [Configuration](#configuration)
- [Beego arguments](#beego-arguments)
- [Integrated third-party applications](#integrated-third-party-applications)
- [Deployment](#deployment)
2013-04-20 18:51:59 +00:00
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
## Hello world
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
This is an example of "Hello world" in Beego:
package main
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
import (
"github.com/astaxie/beego"
)
type MainController struct {
beego.Controller
}
func (this *MainController) Get() {
this.Ctx.WriteString("hello world")
}
func main() {
beego.Router("/", &MainController{})
beego.Run()
}
Save file as "hello.go", build and run it:
$ go build main.go
$ ./hello
Open address [http://127.0.0.1:8080](http://127.0.0.1:8080) in your browser and you will see "hello world".
What happened in behind above example?
2013-06-21 08:35:46 +00:00
1. We import package `github.com/astaxie/beego`. As we know that Go initialize packages and runs init() function in every package ([more details](https://github.com/Unknwon/build-web-application-with-golang_EN/blob/master/eBook/02.3.md#main-function-and-init-function)), so Beego initializes the BeeApp application at this time.
2013-04-20 18:51:59 +00:00
2. Define controller. We define a struct called `MainController` with a anonymous field `beego.Controller`, so the `MainController` has all methods that `beego.Controller` has.
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
3. Define RESTful methods. Once we use anonymous combination, `MainController` has already had `Get`, `Post`, `Delete`, `Put` and other methods, these methods will be called when user sends corresponding request, like `Post` method for requests that are using POST method. Therefore, after we overloaded `Get` method in `MainController`, all GET requests will use `Get` method in `MainController` instead of in `beego.Controller`.
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
4. Define main function. All applications in Go use main function as entry point as C does.
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
5. Register routers, it tells Beego which controller is responsibility for specific requests. Here we register `/` for `MainController`, so all requests in `/` will be handed to `MainController`. Be aware that the first argument is the path and the second one is pointer of controller that you want to register.
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
6. Run application in port 8080 as default, press `Ctrl+c` to exit.
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
## New project
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
Get into your $GOPATH, then use following command to setup Beego project:
bee create hello
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
It generates folders and files for your project, directory structure as follows:
.
├── conf
│ └── app.conf
├── controllers
│ └── default.go
├── main.go
├── models
├── static
│ ├── css
│ ├── img
│ └── js
└── views
└── index.tpl
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
## Development mode
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
Beego uses development mode as default, you can use following code to change mode in your application:
beego.RunMode = "pro"
Or use configuration file in `conf/app.conf`, and input following content:
runmode = pro
No differences between two ways.
In development mode, you have following effects:
2013-06-21 08:35:46 +00:00
- If you don't have directory `views`, it prints following error prompt:
2013-04-20 18:51:59 +00:00
2013/04/13 19:36:17 [W] [stat views: no such file or directory]
- It doesn't cache template and reload every time.
- If panic occurs in your server, it prints information like following screen shot:
![](images/dev.png)
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
## Router
2013-06-21 08:35:46 +00:00
2013-04-21 21:44:53 +00:00
The main function of router is to connect request URL and handler. Beego wrapped `Controller`, so it connects request URL and `ControllerInterface`. The `ControllerInterface` has following methods:
2013-04-20 18:51:59 +00:00
type ControllerInterface interface {
Init(ct *Context, cn string)
Prepare()
Get()
Post()
Delete()
Put()
Head()
Patch()
Options()
Finish()
Render() error
}
2013-04-21 21:44:53 +00:00
`beego.Controller` implemented all of them, so you just use this struct as anonymous field in your controller struct. Of course you have to overload corresponding methods for more specific usages.
2013-04-20 18:51:59 +00:00
2013-04-21 21:44:53 +00:00
Users can use following ways to register route rules:
2013-04-20 18:51:59 +00:00
beego.Router("/", &controllers.MainController{})
beego.Router("/admin", &admin.UserController{})
beego.Router("/admin/index", &admin.ArticleController{})
beego.Router("/admin/addpkg", &admin.AddController{})
2013-04-21 21:44:53 +00:00
For more convenient configure route rules, Beego references the idea from sinatra, so it supports more kinds of route rules as follows:
2013-04-20 18:51:59 +00:00
2013-06-21 08:35:46 +00:00
- beego.Router("/api/:id([0-9]+)", &controllers.RController{})
2013-04-20 18:51:59 +00:00
2013-06-21 08:35:46 +00:00
Customized regular expression match // match /api/123 :id= 123
- beego.Router("/news/:all", &controllers.RController{})
2013-04-21 21:44:53 +00:00
Match rest of all // match /news/path/to/123.html :all= path/to/123.html
2013-06-21 08:35:46 +00:00
- beego.Router("/user/:username([\w]+)", &controllers.RController{})
2013-04-21 21:44:53 +00:00
Regular expression // match /user/astaxie :username = astaxie
2013-06-21 08:35:46 +00:00
- beego.Router("/download/`*`.`*`", &controllers.RController{})
2013-04-21 21:44:53 +00:00
Wildcard character // match /download/file/api.xml :path= file/api :ext=xml
2013-06-21 08:35:46 +00:00
- beego.Router("/download/ceshi/`*`", &controllers.RController{})
2013-04-21 21:44:53 +00:00
wildcard character match rest of all // match /download/ceshi/file/api.json :splat=file/api.json
2013-06-21 08:35:46 +00:00
- beego.Router("/:id:int", &controllers.RController{})
2013-04-21 21:44:53 +00:00
Match type int // match :id is int type, Beego uses regular expression ([0-9]+) automatically
2013-06-21 08:35:46 +00:00
- beego.Router("/:hi:string", &controllers.RController{})
2013-04-20 18:51:59 +00:00
2013-04-21 21:44:53 +00:00
Match type string // match :hi is string type, Beego uses regular expression ([\w]+) automatically
2013-06-21 08:35:46 +00:00
## Static files
2013-04-21 22:07:17 +00:00
Go provides `http.ServeFile` for static files, Beego wrapped this function and use following way to register static file folder:
2013-04-20 18:51:59 +00:00
beego.SetStaticPath("/static","public")
2013-06-21 08:35:46 +00:00
2013-04-21 22:07:17 +00:00
- The first argument is the path of your URL.
- The second argument is the directory in your application path.
2013-04-20 18:51:59 +00:00
2013-04-21 22:07:17 +00:00
Beego supports multiple static file directories as follows:
2013-04-20 18:51:59 +00:00
beego.SetStaticPath("/images","images")
beego.SetStaticPath("/css","css")
beego.SetStaticPath("/js","js")
2013-04-21 22:07:17 +00:00
After you setting static directory, when users visit `/images/login/login.png`Beego accesses `images/login/login.png` in related to your application directory. One more example, if users visit `/static/img/logo.png`, Beego accesses file `public/img/logo.png`.
2013-04-20 18:51:59 +00:00
2013-06-21 08:35:46 +00:00
## Filter and middleware
2013-04-21 22:07:17 +00:00
Beego supports customized filter and middleware, such as security verification, force redirect, etc.
2013-04-20 18:51:59 +00:00
2013-04-21 22:07:17 +00:00
Here is an example of verify user name of all requests, check if it's admin.
2013-04-20 18:51:59 +00:00
var FilterUser = func(w http.ResponseWriter, r *http.Request) {
2013-06-21 08:35:46 +00:00
if r.URL.User == nil || r.URL.User.Username() != "admin" {
http.Error(w, "", http.StatusUnauthorized)
}
2013-04-20 18:51:59 +00:00
}
2013-06-21 08:35:46 +00:00
beego.Filter(FilterUser)
2013-04-21 22:07:17 +00:00
You can also filter by arguments:
2013-04-20 18:51:59 +00:00
beego.Router("/:id([0-9]+)", &admin.EditController{})
beego.FilterParam("id", func(rw http.ResponseWriter, r *http.Request) {
2013-06-21 08:35:46 +00:00
dosomething()
2013-04-20 18:51:59 +00:00
})
2013-06-21 08:35:46 +00:00
2013-04-21 22:07:17 +00:00
Filter by prefix is also available:
2013-04-20 18:51:59 +00:00
beego.FilterPrefixPath("/admin", func(rw http.ResponseWriter, r *http.Request) {
2013-06-21 08:35:46 +00:00
dosomething()
2013-04-20 18:51:59 +00:00
})
2013-06-21 08:35:46 +00:00
## Controller
2013-04-21 22:07:17 +00:00
Use `beego.controller` as anonymous in your controller struct to implement the interface in Beego:
2013-04-20 18:51:59 +00:00
type xxxController struct {
2013-06-21 08:35:46 +00:00
beego.Controller
2013-04-20 18:51:59 +00:00
}
2013-04-21 22:07:17 +00:00
`beego.Controller` implemented`beego.ControllerInterface`, `beego.ControllerInterface` defined following methods:
2013-04-20 18:51:59 +00:00
2013-04-21 22:07:17 +00:00
- Init(ct `*`Context, cn string)
2013-04-20 18:51:59 +00:00
2013-04-21 22:07:17 +00:00
Initialize context, controller's name, template's name, and container of template arguments
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
- Prepare()
2013-04-21 22:07:17 +00:00
This is for expend usages, it executes before all the following methods. Users can overload this method for verification for example.
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
- Get()
2013-04-21 22:07:17 +00:00
This method executes when client sends request as GET method, 403 as default status code. Users overload this method for customized handle process of GET method.
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
- Post()
2013-04-21 22:07:17 +00:00
This method executes when client sends request as POST method, 403 as default status code. Users overload this method for customized handle process of POST method.
2013-04-20 18:51:59 +00:00
- Delete()
2013-04-21 22:07:17 +00:00
This method executes when client sends request as DELETE method, 403 as default status code. Users overload this method for customized handle process of DELETE method.
2013-04-20 18:51:59 +00:00
- Put()
2013-04-21 22:07:17 +00:00
This method executes when client sends request as PUT method, 403 as default status code. Users overload this method for customized handle process of PUT method.
2013-04-20 18:51:59 +00:00
- Head()
2013-04-21 22:07:17 +00:00
This method executes when client sends request as HEAD method, 403 as default status code. Users overload this method for customized handle process of HEAD method.
2013-04-20 18:51:59 +00:00
- Patch()
2013-04-21 22:07:17 +00:00
This method executes when client sends request as PATCH method, 403 as default status code. Users overload this method for customized handle process of PATCH method.
2013-04-20 18:51:59 +00:00
- Options()
2013-04-21 22:07:17 +00:00
This method executes when client sends request as OPTIONS method, 403 as default status code. Users overload this method for customized handle process of OPTIONS method.
2013-04-20 18:51:59 +00:00
- Finish()
2013-04-21 22:07:17 +00:00
This method executes after corresponding method finished, empty as default. User overload this method for more usages like close database, clean data, etc.
2013-04-20 18:51:59 +00:00
- Render() error
2013-04-21 22:07:17 +00:00
This method is for rendering template, it executes automatically when you set beego.AutoRender to true.
2013-04-20 18:51:59 +00:00
2013-04-21 22:07:17 +00:00
Overload all methods for all customized logic processes, let's see an example:
2013-04-20 18:51:59 +00:00
type AddController struct {
2013-06-21 08:35:46 +00:00
beego.Controller
2013-04-20 18:51:59 +00:00
}
func (this *AddController) Prepare() {
}
func (this *AddController) Get() {
2013-06-21 08:35:46 +00:00
this.Data["content"] = "value"
this.Layout = "admin/layout.html"
this.TplNames = "admin/add.tpl"
2013-04-20 18:51:59 +00:00
}
func (this *AddController) Post() {
2013-06-21 08:35:46 +00:00
pkgname := this.GetString("pkgname")
content := this.GetString("content")
pk := models.GetCruPkg(pkgname)
if pk.Id == 0 {
var pp models.PkgEntity
pp.Pid = 0
pp.Pathname = pkgname
pp.Intro = pkgname
models.InsertPkg(pp)
pk = models.GetCruPkg(pkgname)
}
var at models.Article
at.Pkgid = pk.Id
at.Content = content
models.InsertArticle(at)
this.Ctx.Redirect(302, "/admin/index")
}
## Template
### Template directory
2013-04-20 18:51:59 +00:00
Beego uses `views` as the default directory for template files, parses and caches them as needed(cache is not enable in develop mode), but you can **change**(because only one directory can be used for template files) its directory using following code:
2013-04-20 18:51:59 +00:00
beego.ViewsPath = "/myviewpath"
2013-06-21 08:35:46 +00:00
### Auto-render
You don't need to call render function manually, Beego calls it automatically after corresponding methods executed. If your application is somehow doesn't need templates, you can disable this feature either in code of `main.go` or configuration file.
To disable auto-render in configuration file:
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
autorender = false
To disable auto-render in `main.go`(before you call `beego.Run()` to run the application):
2013-04-20 18:51:59 +00:00
beego.AutoRender = false
2013-06-21 08:35:46 +00:00
### Template data
You can use `this.Data` in controller methods to access the data in templates. Suppose you want to get content of `{{.Content}}`, you can use following code to do this:
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
this.Data["Context"] = "value"
2013-06-21 08:35:46 +00:00
### Template name
Beego uses built-in template engine of Go, so there is no different in syntax. As for how to write template file, please visit [Template tutorial](https://github.com/Unknwon/build-web-application-with-golang_EN/blob/master/eBook/07.4.md).
2013-04-20 18:51:59 +00:00
Beego parses template files in `viewpath` and render it after you set the name of the template file in controller methods. For example, Beego finds the file `add.tpl` in directory `admin` in following code:
2013-04-20 18:51:59 +00:00
this.TplNames = "admin/add.tpl"
Beego supports two kinds of extensions for template files, which are `tpl` and `html`, if you want to use other extensions, you have to use following code to let Beego know:
2013-04-20 18:51:59 +00:00
beego.AddTemplateExt("<your template file extension>")
2013-04-20 18:51:59 +00:00
If you enabled auto-render and you don't tell Beego which template file you are going to use in controller methods, Beego uses following format to find the template file if it exists:
2013-04-20 18:51:59 +00:00
c.TplNames = c.ChildName + "/" + c.Ctx.Request.Method + "." + c.TplExt
Which is `<corresponding controller name>/<request method name>.<template extension>`. For example, your controller name is `AddController` and the request method is POST, and the default file extension is `tpl`, so Beego will try to find file `/<viewpath>/AddController/POST.tpl`.
2013-04-20 18:51:59 +00:00
2013-06-21 08:35:46 +00:00
### Layout design
Beego supports layout design, which means if you are working on an administration application, and some part of its user interface is exactly same all the time, then you can make this part as a layout.
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
this.Layout = "admin/layout.html"
this.TplNames = "admin/add.tpl"
You have to set following variable in order to make Beego possible to insert your dynamic content:
2013-04-20 18:51:59 +00:00
{{.LayoutContent}}
2013-06-21 08:35:46 +00:00
Beego parses template file and assign content to `LayoutContent`, and render them together.
2013-04-20 18:51:59 +00:00
Right now, Beego caches all template files, so you can use following way to implement another kind of layout:
2013-04-20 18:51:59 +00:00
{{template "header.html"}}
Handle logic
2013-04-20 18:51:59 +00:00
{{template "footer.html"}}
2013-06-21 08:35:46 +00:00
### Template function
Beego supports customized template functions that are registered before you call `beego.Run()`.
2013-04-20 18:51:59 +00:00
func hello(in string)(out string){
2013-06-21 08:35:46 +00:00
out = in + "world"
return
2013-04-20 18:51:59 +00:00
}
beego.AddFuncMap("hi",hello)
Then you can use this function in your template files:
2013-04-20 18:51:59 +00:00
{{.Content | hi}}
There are some built-in template functions:
2013-04-20 18:51:59 +00:00
2013-06-21 08:35:46 +00:00
* markdown
This function converts markdown content to HTML format, use {{markdown .Content}} in template files.
2013-06-21 08:35:46 +00:00
* dateformat
2013-04-20 18:51:59 +00:00
This function converts time to formatted string, use {{dateformat .Time "2006-01-02T15:04:05Z07:00"}} in template files.
2013-06-21 08:35:46 +00:00
* date
2013-04-20 18:51:59 +00:00
This function implements date function like in PHP, use formatted string to get corresponding time, use {{date .T "Y-m-d H:i:s"}} in template files.
2013-06-21 08:35:46 +00:00
* compare
2013-04-20 18:51:59 +00:00
This functions compares two objects, returns true if they are same, false otherwise, use {{compare .A .B}} in template files.
2013-06-21 08:35:46 +00:00
* substr
2013-04-20 18:51:59 +00:00
This function cuts out string from another string by index, it supports UTF-8 characters, use {{substr .Str 0 30}} in template files.
2013-06-21 08:35:46 +00:00
* html2str
2013-04-20 18:51:59 +00:00
This function escapes HTML to raw string, use {{html2str .Htmlinfo}} in template files.
2013-06-21 08:35:46 +00:00
* str2html
2013-04-20 18:51:59 +00:00
This function outputs string in HTML format without escaping, use {{str2html .Strhtml}} in template files.
2013-06-21 08:35:46 +00:00
* htmlquote
2013-04-20 18:51:59 +00:00
This functions implements basic HTML escape, use {{htmlquote .quote}} in template files.
2013-06-21 08:35:46 +00:00
* htmlunquote
2013-04-20 18:51:59 +00:00
This functions implements basic invert-escape of HTML, use {{htmlunquote .unquote}} in template files.
2013-06-21 08:35:46 +00:00
## Handle request
We always need to get data from users, including methods like GET, POST, etc. Beego parses these data automatically, and you can access them by following code:
2013-04-20 18:51:59 +00:00
- GetString(key string) string
- GetInt(key string) (int64, error)
- GetBool(key string) (bool, error)
Usage example:
2013-04-20 18:51:59 +00:00
func (this *MainController) Post() {
jsoninfo := this.GetString("jsoninfo")
if jsoninfo == "" {
this.Ctx.WriteString("jsoninfo is empty")
return
}
}
If you need other types that are not included above, like you need int64 instead of int, then you need to do following way:
2013-04-20 18:51:59 +00:00
func (this *MainController) Post() {
id := this.Input().Get("id")
intid, err := strconv.Atoi(id)
2013-06-21 08:35:46 +00:00
}
2013-04-20 18:51:59 +00:00
To use `this.Ctx.Request` for more information about request, and object properties and method please read [Request](http://golang.org/pkg/net/http/#Request)
2013-06-21 08:35:46 +00:00
### File upload
It's very easy to upload file through Beego, but don't forget to add `enctype="multipart/form-data"` in your form, otherwise the browser will not upload anything.
2013-04-20 18:51:59 +00:00
2013-06-21 08:35:46 +00:00
Files will be saved in memory, if the size is greater than cache memory, the rest part will be saved as temporary file. The default cache memory is 64 MB, and you can use following ways to change this size.
2013-04-20 18:51:59 +00:00
In code:
2013-04-20 18:51:59 +00:00
2013-06-21 08:35:46 +00:00
beego.MaxMemory = 1<<22
2013-04-20 18:51:59 +00:00
In configuration file:
2013-04-20 18:51:59 +00:00
maxmemory = 1<<22
Beego provides two convenient functions to upload files:
2013-04-20 18:51:59 +00:00
- GetFile(key string) (multipart.File, `*`multipart.FileHeader, error)
2013-04-20 18:51:59 +00:00
This function is mainly used to read file name element `the_file` in form and returns corresponding information. You can use this information either filter or save files.
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
- SaveToFile(fromfile, tofile string) error
This function a wrapper of GetFile and gives ability to save file.
2013-06-21 08:35:46 +00:00
This is an example to save file that is uploaded:
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
func (this *MainController) Post() {
this.SaveToFile("the_file","/var/www/uploads/uploaded_file.txt"")
}
2013-06-21 08:35:46 +00:00
### Output Json and XML
Beego considered API function design at the beginning, and we often use Json or XML format data as output. Therefore, it's no reason that Beego doesn't support it:
2013-04-20 18:51:59 +00:00
Set `content-type` to `application/json` for output raw Json format data:
2013-04-20 18:51:59 +00:00
func (this *AddController) Get() {
2013-06-21 08:35:46 +00:00
mystruct := { ... }
this.Data["json"] = &mystruct
this.ServeJson()
}
2013-04-20 18:51:59 +00:00
Set `content-type` to `application/xml` for output raw XML format data:
2013-04-20 18:51:59 +00:00
func (this *AddController) Get() {
2013-06-21 08:35:46 +00:00
mystruct := { ... }
this.Data["xml"]=&mystruct
this.ServeXml()
}
## Redirect and error
You can use following to redirect:
2013-04-20 18:51:59 +00:00
func (this *AddController) Get() {
2013-06-21 08:35:46 +00:00
this.Redirect("/", 302)
}
2013-04-20 18:51:59 +00:00
2013-05-14 05:08:20 +00:00
You can also throw an exception in your controller as follows:
func (this *MainController) Get() {
2013-06-21 08:35:46 +00:00
this.Abort("401")
v := this.GetSession("asta")
if v == nil {
this.SetSession("asta", int(1))
this.Data["Email"] = 0
} else {
this.SetSession("asta", v.(int)+1)
this.Data["Email"] = v.(int)
}
this.TplNames = "index.tpl"
2013-05-14 05:08:20 +00:00
}
Then Beego will not execute rest code of the function body when you call `this.Abort("401")`, and gives following default page view to users:
![](images/401.png)
Beego supports following error code: 404, 401, 403, 500 and 503, you can customize your error handle, for example, use following code to replace 404 error handle process:
2013-06-21 08:35:46 +00:00
func page_not_found(rw http.ResponseWriter, r *http.Request) {
t, _ := template.New("beegoerrortemp").ParseFiles(beego.ViewsPath + "/404.html")
data := make(map[string]interface{})
data["content"] = "page not found"
t.Execute(rw, data)
2013-05-14 05:08:20 +00:00
}
func main() {
2013-06-21 08:35:46 +00:00
beego.Errorhandler("404", page_not_found)
beego.Router("/", &controllers.MainController{})
beego.Run()
}
2013-05-14 05:08:20 +00:00
You may be able to use your own `404.html` for your 404 error.
Beego also gives you ability to modify error message that shows on the error page, the following example shows how to set more meaningful error message when database has problems:
2013-06-21 08:35:46 +00:00
func dbError(rw http.ResponseWriter, r *http.Request) {
t, _ := template.New("beegoerrortemp").ParseFiles(beego.ViewsPath + "/dberror.html")
data := make(map[string]interface{})
data["content"] = "database is now down"
t.Execute(rw, data)
2013-05-14 05:08:20 +00:00
}
func main() {
2013-06-21 08:35:46 +00:00
beego.Errorhandler("dbError", dbError)
beego.Router("/", &controllers.MainController{})
beego.Run()
}
2013-05-14 05:08:20 +00:00
After you registered this customized error, you can use `this.Abort("dbError")` for any database error in your applications.
2013-04-20 18:51:59 +00:00
2013-06-21 08:35:46 +00:00
## Handle response
There are some situations that you may have in response:
2013-04-20 18:51:59 +00:00
1. Output template
2013-04-20 18:51:59 +00:00
I've already talked about template above, Beego outputs template after corresponding method executed.
2013-06-21 08:35:46 +00:00
2. Redirect
2013-04-20 18:51:59 +00:00
You can use this.Redirect("/", 302) to redirect page.
2013-06-21 08:35:46 +00:00
3. Output string
2013-04-20 18:51:59 +00:00
Sometimes we just need to print string on the screen:
2013-04-20 18:51:59 +00:00
this.Ctx.WriteString("ok")
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
## Sessions
2013-06-21 08:35:46 +00:00
Beego has a built-in session module and supports four engines, including memory, file, MySQL and redis. You can implement your own engine based on the interface.
2013-04-20 18:51:59 +00:00
It's easy to use session in Beego, use following code in your main() function:
2013-04-20 18:51:59 +00:00
beego.SessionOn = true
Or use configuration file:
2013-04-20 18:51:59 +00:00
sessionon = true
The following example shows you how to use session in Beego:
2013-04-20 18:51:59 +00:00
func (this *MainController) Get() {
v := this.GetSession("asta")
if v == nil {
this.SetSession("asta", int(1))
this.Data["num"] = 0
} else {
this.SetSession("asta", v.(int)+1)
this.Data["num"] = v.(int)
}
this.TplNames = "index.tpl"
}
We can see that there are few convenient methods:
2013-04-20 18:51:59 +00:00
- SetSession(name string, value interface{})
- GetSession(name string) interface{}
- DelSession(name string)
There are three kinds of operation for session: set, get, and delete.
2013-04-20 18:51:59 +00:00
Of course you can use following code to customized session logic:
2013-04-20 18:51:59 +00:00
sess:=this.StartSession()
defer sess.SessionRelease()
The sess object has following methods:
2013-04-20 18:51:59 +00:00
* sess.Set()
* sess.Get()
* sess.Delete()
* sess.SessionID()
However, I recommend you to use SetSession、GetSession、DelSession these three operations in order to prevent resource leak.
2013-04-20 18:51:59 +00:00
There are some arguments you can use in session module:
2013-04-20 18:51:59 +00:00
- SessionOn
Whether enable session or not, default is false, corresponding arguments in configuration file: sessionon.
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
- SessionProvider
Setting session engine, default is memory, other options are file, MySQL and redis, corresponding arguments in configuration file: sessionprovider.
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
- SessionName
Setting name of cookies, it saves in users' browser with name beegosessionID, corresponding arguments in configuration file: sessionname.
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
- SessionGCMaxLifetime
Setting session expired time, default is 3600 seconds, corresponding arguments in configuration: sessiongcmaxlifetime
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
- SessionSavePath
2013-06-21 08:35:46 +00:00
Setting save path or link address of corresponding file, MySQL and redis engines, default is empty, corresponding arguments in configuration file: sessionsavepath
2013-04-20 18:51:59 +00:00
When the SessionProvider is file, SessionSavePath saves file path:
2013-04-20 18:51:59 +00:00
beego.SessionProvider = "file"
beego.SessionSavePath = "./tmp"
When the SessionProvider is mysql, SessionSavePath is link address, it uses driver [go-sql-driver](https://github.com/go-sql-driver/mysql):
2013-04-20 18:51:59 +00:00
beego.SessionProvider = "mysql"
beego.SessionSavePath = "username:password@protocol(address)/dbname?param=value"
2013-06-21 08:35:46 +00:00
When the SessionProvider is redis, SessionSavePath is link address of redis, it uses driver [redigo](https://github.com/garyburd/redigo):
2013-04-20 18:51:59 +00:00
beego.SessionProvider = "redis"
2013-06-21 08:35:46 +00:00
beego.SessionSavePath = "127.0.0.1:6379"
2013-04-20 18:51:59 +00:00
## Cache
2013-06-21 08:35:46 +00:00
Beego has a built-in cache module, it's like memcache, which caches data in memory. Here is an example of using cache module in Beego:
2013-04-20 18:51:59 +00:00
var (
urllist *beego.BeeCache
)
func init() {
urllist = beego.NewBeeCache()
urllist.Every = 0 // Not expired
2013-04-20 18:51:59 +00:00
urllist.Start()
}
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
func (this *ShortController) Post() {
var result ShortResult
longurl := this.Input().Get("longurl")
beego.Info(longurl)
result.UrlLong = longurl
urlmd5 := models.GetMD5(longurl)
beego.Info(urlmd5)
if urllist.IsExist(urlmd5) {
result.UrlShort = urllist.Get(urlmd5).(string)
} else {
result.UrlShort = models.Generate()
err := urllist.Put(urlmd5, result.UrlShort, 0)
if err != nil {
beego.Info(err)
}
err = urllist.Put(result.UrlShort, longurl, 0)
if err != nil {
beego.Info(err)
}
}
this.Data["json"] = result
this.ServeJson()
2013-06-21 08:35:46 +00:00
}
To use cache, you need to initialize a `beego.NewBeeCache` object and set expired time, and enable expired check. Then you can use following methods to achieve other operations:
2013-04-20 18:51:59 +00:00
- Get(name string) interface{}
- Put(name string, value interface{}, expired int) error
- Delete(name string) (ok bool, err error)
- IsExist(name string) bool
2013-06-21 08:35:46 +00:00
## Safe map
We know that map is not thread safe in Go, if you don't know it, this article may be helpful for you: [atomic_maps](http://golang.org/doc/faq#atomic_maps). However, we need a kind of thread safe map in practice, especially when we are using goroutines. Therefore, Beego provides a simple built-in thread safe map implementation.
2013-04-20 18:51:59 +00:00
bm := NewBeeMap()
if !bm.Set("astaxie", 1) {
t.Error("set Error")
}
if !bm.Check("astaxie") {
t.Error("check err")
}
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
if v := bm.Get("astaxie"); v.(int) != 1 {
t.Error("get err")
}
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
bm.Delete("astaxie")
if bm.Check("astaxie") {
t.Error("delete err")
}
2013-06-21 08:35:46 +00:00
This map has following interfaces:
2013-04-20 18:51:59 +00:00
- Get(k interface{}) interface{}
- Set(k interface{}, v interface{}) bool
- Check(k interface{}) bool
- Delete(k interface{})
2013-06-21 08:35:46 +00:00
## Log
Beego has a default BeeLogger object that outputs log into stdout, and you can use your own logger as well:
2013-04-20 18:51:59 +00:00
beego.SetLogger(*log.Logger)
2013-06-26 16:24:06 +00:00
Now Beego supports new way to record your log with automatically log rotate. Use following code in your main function:
2013-04-20 18:51:59 +00:00
2013-06-26 16:24:06 +00:00
filew := beego.NewFileWriter("tmp/log.log", true)
err := filew.StartLogger()
2013-04-20 18:51:59 +00:00
if err != nil {
2013-06-26 16:24:06 +00:00
beego.Critical("NewFileWriter err", err)
2013-04-20 18:51:59 +00:00
}
2013-06-26 16:24:06 +00:00
So Beego records your log into file `tmp/log.log`, the second argument indicates whether enable log rotate or not. The rules of rotate as follows:
1. segment log every 1,000,000 lines.
2. segment log every 256 MB file size.
3. segment log daily.
4. save log file up to 7 days as default.
You cannot segment log over 999 times everyday, the segmented file name with format `<defined file name>.<date>.<three digits>`.
You are able to modify rotate rules with following methods, be sure that you call them before `StartLogger()`.
- func (w *FileLogWriter) SetRotateDaily(daily bool) *FileLogWriter
- func (w *FileLogWriter) SetRotateLines(maxlines int) *FileLogWriter
- func (w *FileLogWriter) SetRotateMaxDays(maxdays int64) *FileLogWriter
- func (w *FileLogWriter) SetRotateSize(maxsize int) *FileLogWriter
2013-06-21 08:35:46 +00:00
### Different levels of log
2013-04-20 18:51:59 +00:00
* Trace(v ...interface{})
* Debug(v ...interface{})
* Info(v ...interface{})
* Warn(v ...interface{})
* Error(v ...interface{})
* Critical(v ...interface{})
You can use following code to set log level:
2013-04-20 18:51:59 +00:00
beego.SetLevel(beego.LevelError)
2013-06-21 08:35:46 +00:00
Your project may have a lot of log outputs, but you don't want to output everything after your application is running on the internet, for example, you want to ignore Trace, Debug and Info level log outputs, you can use following setting:
2013-04-20 18:51:59 +00:00
beego.SetLevel(beego.LevelWarning)
Then Beego will not output log that has lower level of LevelWarning. Here is the list of all log levels, order from lower to higher:
2013-04-20 18:51:59 +00:00
2013-06-21 08:35:46 +00:00
LevelTrace, LevelDebug, LevelInfo, LevelWarning, LevelError, LevelCritical
2013-04-20 18:51:59 +00:00
You can use different log level to output different error messages, it's based on how critical the error you think it is:
2013-04-20 18:51:59 +00:00
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
### Examples of log messages
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
- Trace
* "Entered parse function validation block"
* "Validation: entered second 'if'"
* "Dictionary 'Dict' is empty. Using default value"
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
- Debug
* "Web page requested: http://somesite.com Params='...'"
* "Response generated. Response size: 10000. Sending."
* "New file received. Type:PNG Size:20000"
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
- Info
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
* "Web server restarted"
* "Hourly statistics: Requested pages: 12345 Errors: 123 ..."
* "Service paused. Waiting for 'resume' call"
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
- Warn
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
* "Cache corrupted for file='test.file'. Reading from back-end"
* "Database 192.168.0.7/DB not responding. Using backup 192.168.0.8/DB"
* "No response from statistics server. Statistics not sent"
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
- Error
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
* "Internal error. Cannot process request #12345 Error:...."
* "Cannot perform login: credentials DB not responding"
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
- Critical
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
* "Critical panic received: .... Shutting down"
* "Fatal error: ... App is shutting down to prevent data corruption or loss"
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
### Example
func internalCalculationFunc(x, y int) (result int, err error) {
beego.Debug("calculating z. x:",x," y:",y)
z := y
switch {
case x == 3 :
beego.Trace("x == 3")
panic("Failure.")
case y == 1 :
beego.Trace("y == 1")
return 0, errors.New("Error!")
case y == 2 :
beego.Trace("y == 2")
z = x
default :
beego.Trace("default")
z += x
}
retVal := z-3
beego.Debug("Returning ", retVal)
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
return retVal, nil
}
func processInput(input inputData) {
defer func() {
if r := recover(); r != nil {
2013-06-21 08:35:46 +00:00
beego.Error("Unexpected error occurred: ", r)
2013-04-20 18:51:59 +00:00
outputs <- outputData{result : 0, error : true}
2013-06-21 08:35:46 +00:00
}
2013-04-20 18:51:59 +00:00
}()
2013-06-21 08:35:46 +00:00
beego.Info("Received input signal. x:",input.x," y:", input.y)
2013-04-20 18:51:59 +00:00
2013-06-21 08:35:46 +00:00
res, err := internalCalculationFunc(input.x, input.y)
2013-04-20 18:51:59 +00:00
if err != nil {
beego.Warn("Error in calculation:", err.Error())
}
2013-06-21 08:35:46 +00:00
beego.Info("Returning result: ",res," error: ",err)
outputs <- outputData{result : res, error : err != nil}
2013-04-20 18:51:59 +00:00
}
func main() {
inputs = make(chan inputData)
outputs = make(chan outputData)
criticalChan = make(chan int)
beego.Info("App started.")
go consumeResults(outputs)
beego.Info("Started receiving results.")
go generateInputs(inputs)
beego.Info("Started sending signals.")
for {
select {
case input := <- inputs:
processInput(input)
case <- criticalChan:
beego.Critical("Caught value from criticalChan: Go shut down.")
panic("Shut down due to critical fault.")
}
}
}
2013-06-21 08:35:46 +00:00
## Configuration
2013-04-30 18:54:11 +00:00
Beego supports to parse .ini file in path `conf/app.conf`, and you have following options:
2013-04-20 18:51:59 +00:00
appname = beepkg
httpaddr = "127.0.0.1"
httpport = 9090
runmode ="dev"
autorender = false
autorecover = false
viewspath = "myview"
2013-06-21 08:35:46 +00:00
2013-04-30 18:54:11 +00:00
If you set value in configuration file, Beego uses it to replace default value.
2013-04-20 18:51:59 +00:00
2013-04-30 18:54:11 +00:00
You can also have other values for your application, for example, database connection information:
2013-04-20 18:51:59 +00:00
mysqluser = "root"
mysqlpass = "rootpass"
mysqlurls = "127.0.0.1"
mysqldb = "beego"
2013-06-21 08:35:46 +00:00
2013-04-30 18:54:11 +00:00
Then use following code to load your settings:
2013-04-20 18:51:59 +00:00
beego.AppConfig.String("mysqluser")
beego.AppConfig.String("mysqlpass")
beego.AppConfig.String("mysqlurls")
beego.AppConfig.String("mysqldb")
2013-04-30 18:54:11 +00:00
AppConfig supports following methods:
2013-04-20 18:51:59 +00:00
- Bool(key string) (bool, error)
- Int(key string) (int, error)
- Int64(key string) (int64, error)
- Float(key string) (float64, error)
- String(key string) string
2013-06-21 08:35:46 +00:00
## Beego arguments
2013-04-30 18:54:11 +00:00
Beego has many configurable arguments, let me introduce to you all of them, so you can use them for more usage in your application:
2013-04-20 18:51:59 +00:00
* BeeApp
2013-04-30 18:54:11 +00:00
Entry point of Beego, it initialized in init() function when you import Beego package.
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
* AppConfig
2013-04-30 18:54:11 +00:00
It stores values from file `conf/app.conf` and initialized in init() function.
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
* HttpAddr
2013-04-30 18:54:11 +00:00
Application listening address, default is empty for listening all IP.
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
* HttpPort
2013-04-30 18:54:11 +00:00
Application listening port, default is 8080.
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
* AppName
2013-04-30 18:54:11 +00:00
Application name, default is "beego".
2013-06-21 08:35:46 +00:00
* RunMode
2013-04-20 18:51:59 +00:00
2013-04-30 18:54:11 +00:00
Application mode, default is "dev" develop mode and gives friendly error messages.
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
* AutoRender
2013-04-30 18:54:11 +00:00
This value indicates whether auto-render or not, default is true, you should set to false for API usage applications.
2013-04-20 18:51:59 +00:00
* RecoverPanic
2013-04-30 18:54:11 +00:00
This value indicates whether recover from panic or not, default is true, and program will not exit when error occurs.
2013-04-20 18:51:59 +00:00
* PprofOn
2013-04-30 18:54:11 +00:00
This value indicates whether enable pprof or not, default is false, and you can use following address to see goroutine execution status once you enable this feature.
2013-04-20 18:51:59 +00:00
/debug/pprof
/debug/pprof/cmdline
/debug/pprof/profile
/debug/pprof/symbol
2013-06-21 08:35:46 +00:00
For more information about pprof, please read [pprof](http://golang.org/pkg/net/http/pprof/)
2013-04-20 18:51:59 +00:00
* ViewsPath
2013-04-30 18:54:11 +00:00
Template path, default is "views".
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
* SessionOn
2013-04-30 18:54:11 +00:00
This value indicate whether enable session or not, default is false.
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
* SessionProvider
2013-04-30 18:54:11 +00:00
Session engine, default is memory.
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
* SessionName
2013-04-30 18:54:11 +00:00
Name for cookie that save in client browser, default is "beegosessionID".
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
* SessionGCMaxLifetime
2013-04-30 18:54:11 +00:00
Session expired time, default is 3600 seconds.
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
* SessionSavePath
2013-04-30 18:54:11 +00:00
Save path of session, default is empty.
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
* UseFcgi
2013-04-30 18:54:11 +00:00
This value indicates whether enable fastcgi or not, default is false.
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
* MaxMemory
2013-04-30 18:54:11 +00:00
Maximum memory size for file upload, default is `1 << 26`(64M).
2013-04-20 18:51:59 +00:00
2013-05-14 05:08:20 +00:00
* EnableGzip
This value indicate whether enable gzip or not, default is false.
2013-06-21 08:35:46 +00:00
## Integrated third-party applications
2013-04-30 18:54:11 +00:00
Beego supports to integrate third-party application, you can customized `http.Handler` as follows:
2013-04-20 18:51:59 +00:00
beego.RouterHandler("/chat/:info(.*)", sockjshandler)
2013-06-21 08:35:46 +00:00
2013-04-30 18:54:11 +00:00
sockjshandler implemented interface `http.Handler`.
2013-04-20 18:51:59 +00:00
2013-04-30 18:54:11 +00:00
Beego has an example for supporting chat of sockjs, here is the code:
2013-04-20 18:51:59 +00:00
package main
import (
"fmt"
"github.com/astaxie/beego"
"github.com/fzzy/sockjs-go/sockjs"
"strings"
)
var users *sockjs.SessionPool = sockjs.NewSessionPool()
func chatHandler(s sockjs.Session) {
users.Add(s)
defer users.Remove(s)
2013-06-21 08:35:46 +00:00
2013-04-20 18:51:59 +00:00
for {
m := s.Receive()
if m == nil {
break
}
fullAddr := s.Info().RemoteAddr
addr := fullAddr[:strings.LastIndex(fullAddr, ":")]
m = []byte(fmt.Sprintf("%s: %s", addr, m))
users.Broadcast(m)
}
}
type MainController struct {
beego.Controller
}
func (m *MainController) Get() {
m.TplNames = "index.html"
}
func main() {
conf := sockjs.NewConfig()
sockjshandler := sockjs.NewHandler("/chat", chatHandler, conf)
beego.Router("/", &MainController{})
beego.RouterHandler("/chat/:info(.*)", sockjshandler)
beego.Run()
}
2013-04-30 18:54:11 +00:00
The above example implemented a simple chat room for sockjs, and you can use `http.Handler` for more extensions.
2013-04-20 18:51:59 +00:00
2013-06-21 08:35:46 +00:00
## Deployment
2013-04-30 18:54:11 +00:00
Go compiles program to binary file, you only need to copy this binary to your server and run it. Because Beego uses MVC model, so you may have folders for static files, configuration files and template files, so you have to copy those files as well. Here is a real example for deployment.
2013-04-20 18:51:59 +00:00
$ mkdir /opt/app/beepkg
$ cp beepkg /opt/app/beepkg
$ cp -fr views /opt/app/beepkg
$ cp -fr static /opt/app/beepkg
$ cp -fr conf /opt/app/beepkg
2013-06-21 08:35:46 +00:00
2013-04-30 18:54:11 +00:00
Here is the directory structure pf `/opt/app/beepkg`.
2013-04-20 18:51:59 +00:00
.
├── conf
│ ├── app.conf
├── static
│ ├── css
│ ├── img
│ └── js
└── views
└── index.tpl
2013-06-21 08:35:46 +00:00
├── beepkg
2013-04-20 18:51:59 +00:00
2013-04-30 18:54:11 +00:00
Now you can run your application in server, here are two good ways to manage your applications, and I recommend the first one.
2013-04-20 18:51:59 +00:00
- Supervisord
2013-06-21 08:35:46 +00:00
2013-04-30 18:54:11 +00:00
More information: [Supervisord](Supervisord.md)
2013-04-20 18:51:59 +00:00
2013-04-30 18:54:11 +00:00
- nohup
2013-04-20 18:51:59 +00:00
nohup ./beepkg &
- [Introduction](README.md)
2013-06-21 08:35:46 +00:00
- [Step by step](Tutorial.md)