mirror of
https://github.com/astaxie/beego.git
synced 2025-07-11 11:01:02 +00:00
Compare commits
53 Commits
Author | SHA1 | Date | |
---|---|---|---|
309f2e199b | |||
26baf0f283 | |||
23ff422826 | |||
d99ae3e3a2 | |||
4b09175d27 | |||
512ddf8a70 | |||
73d45b150c | |||
c5a23d5cde | |||
3578bb3287 | |||
40c7b47c24 | |||
f56e7737ce | |||
86ddfe5375 | |||
1c4d0d72ca | |||
20e101a42d | |||
10417dd6d5 | |||
541d7f71b9 | |||
fdb94aeba0 | |||
67511deea7 | |||
43f588b5c1 | |||
cc6c84b317 | |||
b68a37a115 | |||
b886ed59a5 | |||
3475685233 | |||
0abe3e2157 | |||
25cf08159a | |||
28d0738cd1 | |||
6a260ca76d | |||
27896c5700 | |||
384fa8fda9 | |||
5d531d3da6 | |||
ea756b68c4 | |||
06e002cf2c | |||
73f0f4f31f | |||
b2abb4eb80 | |||
951881b96d | |||
832f160cc8 | |||
2bbd21ab07 | |||
455367769e | |||
f20ad0916f | |||
be9290364d | |||
4577bfb980 | |||
65041aae71 | |||
8b7cba037e | |||
2dee30183d | |||
4fc49b5803 | |||
67695fac9e | |||
22510bc272 | |||
a2dd859e55 | |||
4d06fec52e | |||
0e916a00f9 | |||
ea36f80470 | |||
1745dd2261 | |||
6d3486e2a6 |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
.DS_Store
|
27
README.md
27
README.md
@ -1,32 +1,41 @@
|
||||
## beego
|
||||
|
||||
[](https://drone.io/github.com/astaxie/beego/latest)
|
||||
|
||||
beego is a Go Framework which is inspired from tornado and sinatra.
|
||||
|
||||
It is a simply & powerful web framework.
|
||||
|
||||
Have this features:
|
||||
|
||||
## Features
|
||||
|
||||
* RESTFul support
|
||||
* MVC architecture
|
||||
* Session support memory/file/redis/mysql
|
||||
* Cache support memory/redis/memcache
|
||||
* Session support (store in memory, file, Redis or MySQL)
|
||||
* Cache support (store in memory, Redis or Memcache)
|
||||
* Global Config
|
||||
* Intelligent routing
|
||||
* thread safe map
|
||||
* Friendly error display
|
||||
* Thread-safe map
|
||||
* Friendly displaying of errors
|
||||
* Useful template functions
|
||||
|
||||
[English Documents](https://github.com/astaxie/beego/tree/master/docs/en)
|
||||
|
||||
## Documentation
|
||||
|
||||
[English](https://github.com/astaxie/beego/tree/master/docs/en)
|
||||
|
||||
[API](http://gowalker.org/github.com/astaxie/beego)
|
||||
|
||||
[中文文档](https://github.com/astaxie/beego/tree/master/docs/zh)
|
||||
|
||||
|
||||
## LICENSE
|
||||
|
||||
beego is licensed under the Apache Licence, Version 2.0
|
||||
(http://www.apache.org/licenses/LICENSE-2.0.html).
|
||||
|
||||
## Use Case
|
||||
|
||||
- API documentation [gowalker](https://github.com/Unknwon/gowalker)
|
||||
- CMS [toropress](https://github.com/insionng/toropress)
|
||||
## Use case
|
||||
|
||||
- Displaying API documentation: [gowalker](https://github.com/Unknwon/gowalker)
|
||||
- CMS: [toropress](https://github.com/insionng/toropress)
|
||||
|
25
beego.go
25
beego.go
@ -12,7 +12,7 @@ import (
|
||||
"runtime"
|
||||
)
|
||||
|
||||
const VERSION = "0.6.0"
|
||||
const VERSION = "0.7.0"
|
||||
|
||||
var (
|
||||
BeeApp *App
|
||||
@ -39,6 +39,7 @@ var (
|
||||
UseFcgi bool
|
||||
MaxMemory int64
|
||||
EnableGzip bool // enable gzip
|
||||
DirectoryIndex bool //ebable DirectoryIndex default is false
|
||||
)
|
||||
|
||||
func init() {
|
||||
@ -81,15 +82,27 @@ func NewApp() *App {
|
||||
|
||||
func (app *App) Run() {
|
||||
addr := fmt.Sprintf("%s:%d", HttpAddr, HttpPort)
|
||||
var err error
|
||||
var (
|
||||
err error
|
||||
l net.Listener
|
||||
)
|
||||
if UseFcgi {
|
||||
l, e := net.Listen("tcp", addr)
|
||||
if e != nil {
|
||||
BeeLogger.Fatal("Listen: ", e)
|
||||
l, err = net.Listen("tcp", addr)
|
||||
if err != nil {
|
||||
BeeLogger.Fatal("Listen: ", err)
|
||||
}
|
||||
err = fcgi.Serve(l, app.Handlers)
|
||||
} else {
|
||||
err = http.ListenAndServe(addr, app.Handlers)
|
||||
server := &http.Server{Handler: app.Handlers}
|
||||
laddr, err := net.ResolveTCPAddr("tcp", addr)
|
||||
if nil != err {
|
||||
BeeLogger.Fatal("ResolveTCPAddr:", err)
|
||||
}
|
||||
l, err = GetInitListner(laddr)
|
||||
theStoppable = newStoppable(l)
|
||||
err = server.Serve(theStoppable)
|
||||
theStoppable.wg.Wait()
|
||||
CloseSelf()
|
||||
}
|
||||
if err != nil {
|
||||
BeeLogger.Fatal("ListenAndServe: ", err)
|
||||
|
41
cache/README.md
vendored
41
cache/README.md
vendored
@ -1,52 +1,59 @@
|
||||
## cache
|
||||
cache is a golang cache manager. It can use cache for many adapters. The repo is inspired by `database/sql` .
|
||||
cache is a Go cache manager. It can use many cache adapters. The repo is inspired by `database/sql` .
|
||||
|
||||
##How to install
|
||||
|
||||
## How to install?
|
||||
|
||||
go get github.com/astaxie/beego/cache
|
||||
|
||||
|
||||
##how many adapter support
|
||||
## What adapters are supported?
|
||||
|
||||
Now this cache support memory/redis/memcache
|
||||
As of now this cache support memory, Memcache and Redis.
|
||||
|
||||
## how to use it
|
||||
first you must import it
|
||||
|
||||
## How to use it?
|
||||
|
||||
First you must import it
|
||||
|
||||
import (
|
||||
"github.com/astaxie/beego/cache"
|
||||
)
|
||||
|
||||
then init an Cache(memory adapter)
|
||||
Then init a Cache (example with memory adapter)
|
||||
|
||||
bm, err := NewCache("memory", `{"interval":60}`)
|
||||
|
||||
use it like this:
|
||||
Use it like this:
|
||||
|
||||
bm.Put("astaxie", 1, 10)
|
||||
bm.Get("astaxie")
|
||||
bm.IsExist("astaxie")
|
||||
bm.Delete("astaxie")
|
||||
|
||||
## memory adapter
|
||||
memory adapter config like this:
|
||||
|
||||
## Memory adapter
|
||||
|
||||
Configure memory adapter like this:
|
||||
|
||||
{"interval":60}
|
||||
|
||||
interval means the gc time. The cache will every interval time to check wheather have item expired.
|
||||
interval means the gc time. The cache will check at each time interval, whether item has expired.
|
||||
|
||||
## memcache adapter
|
||||
memory adapter use the vitess's [memcache](code.google.com/p/vitess/go/memcache) client.
|
||||
|
||||
the config like this:
|
||||
## Memcache adapter
|
||||
|
||||
memory adapter use the vitess's [Memcache](http://code.google.com/p/vitess/go/memcache) client.
|
||||
|
||||
Configure like this:
|
||||
|
||||
{"conn":"127.0.0.1:11211"}
|
||||
|
||||
|
||||
## redis adapter
|
||||
redis adapter use the [redigo](github.com/garyburd/redigo/redis) client.
|
||||
## Redis adapter
|
||||
|
||||
the config like this:
|
||||
Redis adapter use the [redigo](http://github.com/garyburd/redigo/redis) client.
|
||||
|
||||
Configure like this:
|
||||
|
||||
{"conn":":6039"}
|
2
cache/cache.go
vendored
2
cache/cache.go
vendored
@ -28,7 +28,7 @@ func Register(name string, adapter Cache) {
|
||||
adapters[name] = adapter
|
||||
}
|
||||
|
||||
//config is json {"interval":360}
|
||||
// config need to be correct JSON as string: {"interval":360}
|
||||
func NewCache(adapterName, config string) (Cache, error) {
|
||||
adapter, ok := adapters[adapterName]
|
||||
if !ok {
|
||||
|
@ -174,6 +174,9 @@ func ParseConfig() (err error) {
|
||||
if ar, err := AppConfig.Bool("enablegzip"); err == nil {
|
||||
EnableGzip = ar
|
||||
}
|
||||
if ar, err := AppConfig.Bool("directoryindex"); err == nil {
|
||||
DirectoryIndex = ar
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -212,10 +212,30 @@ func (c *Controller) ServeJson() {
|
||||
return
|
||||
}
|
||||
c.Ctx.SetHeader("Content-Length", strconv.Itoa(len(content)), true)
|
||||
c.Ctx.ResponseWriter.Header().Set("Content-Type", "application/json")
|
||||
c.Ctx.ResponseWriter.Header().Set("Content-Type", "application/json;charset=UTF-8")
|
||||
c.Ctx.ResponseWriter.Write(content)
|
||||
}
|
||||
|
||||
func (c *Controller) ServeJsonp() {
|
||||
content, err := json.MarshalIndent(c.Data["jsonp"], "", " ")
|
||||
if err != nil {
|
||||
http.Error(c.Ctx.ResponseWriter, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
callback := c.Ctx.Request.Form.Get("callback")
|
||||
if callback == "" {
|
||||
http.Error(c.Ctx.ResponseWriter, `"callback" parameter required`, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
callback_content := bytes.NewBufferString(callback)
|
||||
callback_content.WriteString("(")
|
||||
callback_content.Write(content)
|
||||
callback_content.WriteString(");\r\n")
|
||||
c.Ctx.SetHeader("Content-Length", strconv.Itoa(callback_content.Len()), true)
|
||||
c.Ctx.ResponseWriter.Header().Set("Content-Type", "application/json;charset=UTF-8")
|
||||
c.Ctx.ResponseWriter.Write(callback_content.Bytes())
|
||||
}
|
||||
|
||||
func (c *Controller) ServeXml() {
|
||||
content, err := xml.Marshal(c.Data["xml"])
|
||||
if err != nil {
|
||||
@ -223,7 +243,7 @@ func (c *Controller) ServeXml() {
|
||||
return
|
||||
}
|
||||
c.Ctx.SetHeader("Content-Length", strconv.Itoa(len(content)), true)
|
||||
c.Ctx.ResponseWriter.Header().Set("Content-Type", "application/xml")
|
||||
c.Ctx.ResponseWriter.Header().Set("Content-Type", "application/xml;charset=UTF-8")
|
||||
c.Ctx.ResponseWriter.Write(content)
|
||||
}
|
||||
|
||||
@ -241,6 +261,18 @@ func (c *Controller) GetString(key string) string {
|
||||
return c.Input().Get(key)
|
||||
}
|
||||
|
||||
func (c *Controller) GetStrings(key string) []string {
|
||||
r := c.Ctx.Request;
|
||||
if r.Form == nil {
|
||||
return []string{}
|
||||
}
|
||||
vs := r.Form[key]
|
||||
if len(vs) > 0 {
|
||||
return vs
|
||||
}
|
||||
return []string{}
|
||||
}
|
||||
|
||||
func (c *Controller) GetInt(key string) (int64, error) {
|
||||
return strconv.ParseInt(c.Input().Get(key), 10, 64)
|
||||
}
|
||||
|
46
docs/en/HotUpdate.md
Normal file
46
docs/en/HotUpdate.md
Normal file
@ -0,0 +1,46 @@
|
||||
## What is hot update?
|
||||
|
||||
If you have used nginx, you may know that nginx supports hot update, which means you can update your nginx without stopping and restarting it. It serves old connections with old version, and accepts new connections with new version. Notice that hot compiling is different from hot update, where hot compiling is monitoring your source files and recompile them when the content changes, it requires stop and restart your applications, `bee start` is a tool for hot compiling.
|
||||
|
||||
|
||||
## Is hot update necessary?
|
||||
|
||||
Some people says that hot update is not as useful as its cool name. In my opinion, this is absolutely necessary because zero-down server is our goal for our services. Even though sometimes some errors or hardware problems may occur, but it belongs to design of high availability, don't mix them up. Service update is a known issue, so we need to fix this problem.
|
||||
|
||||
|
||||
## How Beego support hot update?
|
||||
|
||||
The basic principle of hot update: main process fork a process, and child process execute corresponding programs. So what happens? We know that after forked a process, main process will have all handles, data and stack, etc, but all handles are saved in `CloseOnExec`, so all copied handles will be closed when you execute it unless you clarify this, and we need child process to reuse the handle of `net.Listener`. Once a process calls exec functions, it is "dead", system replaces it with new code. The only thing it left is the process ID, which is the same number but it is a new program after executed.
|
||||
|
||||
Therefore, the first thing we need to do is that let child process fork main process and through `os.StartProcess` to append files that contains handle that is going to be inherited.
|
||||
|
||||
The second step is that we hope child process can start listening from same handle, so we can use `net.FileListener` to achieve this goal. Here we also need FD of this file, so we need to add a environment variable to set this FD before we start child process.
|
||||
|
||||
The final step is that we want to serve old connections with old version of application, and serve new connections with new version. So how can we know if there is any old connections? To do this, we have to record all connections, then we are able to know. Another problem is that how to let new application to accept connection? Because two versions of applications are listening to same port, they get connection request randomly, so we just close old accept function, and it will get error in `l.Accept`.
|
||||
|
||||
Above are three problems that we need to solve, you can see my code logic for specific implementation.
|
||||
|
||||
|
||||
## Show time
|
||||
|
||||
1. Write code in your Get method:
|
||||
|
||||
func (this *MainController) Get() {
|
||||
a, _ := this.GetInt("sleep")
|
||||
time.Sleep(time.Duration(a) * time.Second)
|
||||
this.Ctx.WriteString("ospid:" + strconv.Itoa(os.Getpid()))
|
||||
}
|
||||
|
||||
2. Open two terminals:
|
||||
|
||||
One execute: ` ps -ef|grep <application name>`
|
||||
|
||||
Another one execute: `curl "http://127.0.0.1:8080/?sleep=20"`
|
||||
|
||||
3. Hot update
|
||||
|
||||
`kill -HUP <PID>`
|
||||
|
||||
4. Open a terminal to request connection: `curl "http://127.0.0.1:8080/?sleep=0"`
|
||||
|
||||
As you will see, the first request will wait for 20 seconds, but it's served by old process; after hot update, the first request will print old process ID, but the second request will print new process ID.
|
@ -1,4 +1,5 @@
|
||||
#Installation
|
||||
# Installation
|
||||
|
||||
Beego is a simple web framework, but it uses many third-party packages, so you have to install all dependency packages also.
|
||||
|
||||
- Before anything you do, you have to check that you installed Go in your computer, see more detail about Go installation in my book: [Chapter 1](https://github.com/Unknwon/build-web-application-with-golang_EN/blob/master/eBook/01.1.md)
|
||||
@ -16,10 +17,10 @@ Good job, you're ready to Beego with powerful bee tools!
|
||||
|
||||
Beego has following dependency packages:
|
||||
|
||||
- Session module: [github.com/astaxie/beego/session](github.com/astaxie/beego/session)
|
||||
- To support redis engine: [github.com/garyburd/redigo/redis](github.com/garyburd/redigo/redis)
|
||||
- To support mysql engine: [github.com/go-sql-driver/mysql](github.com/go-sql-driver/mysql)
|
||||
- To support markdown as template function: [github.com/russross/blackfriday](github.com/russross/blackfriday)
|
||||
- Session module: [github.com/astaxie/beego/session](https://github.com/astaxie/beego/session)
|
||||
- To support redis engine: [github.com/garyburd/redigo/redis](https://github.com/garyburd/redigo/redis)
|
||||
- To support mysql engine: [github.com/go-sql-driver/mysql](https://github.com/go-sql-driver/mysql)
|
||||
- To support markdown as template function: [github.com/russross/blackfriday](https://github.com/russross/blackfriday)
|
||||
|
||||
- [Introduction](README.md)
|
||||
- [Quick start](Quickstart.md)
|
@ -1,4 +1,5 @@
|
||||
# Quick start
|
||||
|
||||
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**
|
||||
@ -23,7 +24,9 @@ Hey, you say you've never heard about Beego and don't know how to use it? Don't
|
||||
- [Integrated third-party applications](#integrated-third-party-applications)
|
||||
- [Deployment](#deployment)
|
||||
|
||||
|
||||
## Hello world
|
||||
|
||||
This is an example of "Hello world" in Beego:
|
||||
|
||||
package main
|
||||
@ -54,14 +57,21 @@ Open address [http://127.0.0.1:8080](http://127.0.0.1:8080) in your browser and
|
||||
|
||||
What happened in behind above example?
|
||||
|
||||
1. We import package `github.com/astaxie/beego`. As we know that Go initialize packages and runs init() function in every package(more detail [here](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.
|
||||
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.
|
||||
|
||||
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.
|
||||
|
||||
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`.
|
||||
|
||||
4. Define main function. All applications in Go use main function as entry point as C does.
|
||||
|
||||
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.
|
||||
|
||||
6. Run application in port 8080 as default, press `Ctrl+c` to exit.
|
||||
|
||||
|
||||
## New project
|
||||
|
||||
Get into your $GOPATH, then use following command to setup Beego project:
|
||||
|
||||
bee create hello
|
||||
@ -82,7 +92,9 @@ It generates folders and files for your project, directory structure as follows:
|
||||
└── views
|
||||
└── index.tpl
|
||||
|
||||
|
||||
## Development mode
|
||||
|
||||
Beego uses development mode as default, you can use following code to change mode in your application:
|
||||
|
||||
beego.RunMode = "pro"
|
||||
@ -104,7 +116,9 @@ In development mode, you have following effects:
|
||||
|
||||

|
||||
|
||||
|
||||
## Router
|
||||
|
||||
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:
|
||||
|
||||
type ControllerInterface interface {
|
||||
@ -160,7 +174,9 @@ For more convenient configure route rules, Beego references the idea from sinatr
|
||||
|
||||
Match type string // match :hi is string type, Beego uses regular expression ([\w]+) automatically
|
||||
|
||||
##Static files
|
||||
|
||||
## Static files
|
||||
|
||||
Go provides `http.ServeFile` for static files, Beego wrapped this function and use following way to register static file folder:
|
||||
|
||||
beego.SetStaticPath("/static","public")
|
||||
@ -176,7 +192,9 @@ Beego supports multiple static file directories as follows:
|
||||
|
||||
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`.
|
||||
|
||||
##Filter and middleware
|
||||
|
||||
## Filter and middleware
|
||||
|
||||
Beego supports customized filter and middleware, such as security verification, force redirect, etc.
|
||||
|
||||
Here is an example of verify user name of all requests, check if it's admin.
|
||||
@ -202,7 +220,9 @@ Filter by prefix is also available:
|
||||
dosomething()
|
||||
})
|
||||
|
||||
##Controller
|
||||
|
||||
## Controller
|
||||
|
||||
Use `beego.controller` as anonymous in your controller struct to implement the interface in Beego:
|
||||
|
||||
type xxxController struct {
|
||||
@ -266,7 +286,7 @@ Overload all methods for all customized logic processes, let's see an example:
|
||||
}
|
||||
|
||||
func (this *AddController) Get() {
|
||||
this.Data["content"] ="value"
|
||||
this.Data["content"] = "value"
|
||||
this.Layout = "admin/layout.html"
|
||||
this.TplNames = "admin/add.tpl"
|
||||
}
|
||||
@ -290,13 +310,19 @@ Overload all methods for all customized logic processes, let's see an example:
|
||||
this.Ctx.Redirect(302, "/admin/index")
|
||||
}
|
||||
|
||||
##Template
|
||||
###Template directory
|
||||
|
||||
## Template
|
||||
|
||||
|
||||
### Template directory
|
||||
|
||||
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:
|
||||
|
||||
beego.ViewsPath = "/myviewpath"
|
||||
|
||||
###Auto-render
|
||||
|
||||
### 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:
|
||||
@ -307,13 +333,17 @@ To disable auto-render in `main.go`(before you call `beego.Run()` to run the app
|
||||
|
||||
beego.AutoRender = false
|
||||
|
||||
###Template data
|
||||
|
||||
### 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:
|
||||
|
||||
this.Data["Context"] = "value"
|
||||
|
||||
###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)。
|
||||
|
||||
### 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).
|
||||
|
||||
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:
|
||||
|
||||
@ -329,7 +359,9 @@ If you enabled auto-render and you don't tell Beego which template file you are
|
||||
|
||||
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`.
|
||||
|
||||
###Layout design
|
||||
|
||||
### 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.
|
||||
|
||||
this.Layout = "admin/layout.html"
|
||||
@ -347,7 +379,9 @@ Right now, Beego caches all template files, so you can use following way to impl
|
||||
Handle logic
|
||||
{{template "footer.html"}}
|
||||
|
||||
###Template function
|
||||
|
||||
### Template function
|
||||
|
||||
Beego supports customized template functions that are registered before you call `beego.Run()`.
|
||||
|
||||
func hello(in string)(out string){
|
||||
@ -366,32 +400,42 @@ There are some built-in template functions:
|
||||
* markdown
|
||||
|
||||
This function converts markdown content to HTML format, use {{markdown .Content}} in template files.
|
||||
|
||||
* dateformat
|
||||
|
||||
This function converts time to formatted string, use {{dateformat .Time "2006-01-02T15:04:05Z07:00"}} in template files.
|
||||
|
||||
* date
|
||||
|
||||
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.
|
||||
|
||||
* compare
|
||||
|
||||
This functions compares two objects, returns true if they are same, false otherwise, use {{compare .A .B}} in template files.
|
||||
|
||||
* substr
|
||||
|
||||
This function cuts out string from another string by index, it supports UTF-8 characters, use {{substr .Str 0 30}} in template files.
|
||||
|
||||
* html2str
|
||||
|
||||
This function escapes HTML to raw string, use {{html2str .Htmlinfo}} in template files.
|
||||
|
||||
* str2html
|
||||
|
||||
This function outputs string in HTML format without escaping, use {{str2html .Strhtml}} in template files.
|
||||
|
||||
* htmlquote
|
||||
|
||||
This functions implements basic HTML escape, use {{htmlquote .quote}} in template files.
|
||||
|
||||
* htmlunquote
|
||||
|
||||
This functions implements basic invert-escape of HTML, use {{htmlunquote .unquote}} in template files.
|
||||
|
||||
##Handle request
|
||||
|
||||
## 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:
|
||||
|
||||
- GetString(key string) string
|
||||
@ -417,10 +461,12 @@ If you need other types that are not included above, like you need int64 instead
|
||||
|
||||
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)
|
||||
|
||||
###File upload
|
||||
|
||||
### 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.
|
||||
|
||||
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 using following ways to change this size.
|
||||
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.
|
||||
|
||||
In code:
|
||||
|
||||
@ -446,7 +492,9 @@ This is an example to save file that is uploaded:
|
||||
this.SaveToFile("the_file","/var/www/uploads/uploaded_file.txt"")
|
||||
}
|
||||
|
||||
###Output Json and XML
|
||||
|
||||
### 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:
|
||||
|
||||
Set `content-type` to `application/json` for output raw Json format data:
|
||||
@ -465,16 +513,71 @@ Set `content-type` to `application/xml` for output raw XML format data:
|
||||
this.ServeXml()
|
||||
}
|
||||
|
||||
##Redirect and error
|
||||
|
||||
## Redirect and error
|
||||
|
||||
You can use following to redirect:
|
||||
|
||||
func (this *AddController) Get() {
|
||||
this.Redirect("/", 302)
|
||||
}
|
||||
|
||||
@todo Error processing need to be improved.
|
||||
You can also throw an exception in your controller as follows:
|
||||
|
||||
func (this *MainController) Get() {
|
||||
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"
|
||||
}
|
||||
|
||||
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:
|
||||
|
||||

|
||||
|
||||
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:
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
func main() {
|
||||
beego.Errorhandler("404", page_not_found)
|
||||
beego.Router("/", &controllers.MainController{})
|
||||
beego.Run()
|
||||
}
|
||||
|
||||
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:
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
func main() {
|
||||
beego.Errorhandler("dbError", dbError)
|
||||
beego.Router("/", &controllers.MainController{})
|
||||
beego.Run()
|
||||
}
|
||||
|
||||
After you registered this customized error, you can use `this.Abort("dbError")` for any database error in your applications.
|
||||
|
||||
|
||||
## Handle response
|
||||
|
||||
##Handle response
|
||||
There are some situations that you may have in response:
|
||||
|
||||
1. Output template
|
||||
@ -491,7 +594,9 @@ There are some situations that you may have in response:
|
||||
|
||||
this.Ctx.WriteString("ok")
|
||||
|
||||
|
||||
## Sessions
|
||||
|
||||
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.
|
||||
|
||||
It's easy to use session in Beego, use following code in your main() function:
|
||||
@ -575,7 +680,9 @@ When the SessionProvider is redis, SessionSavePath is link address of redis, it
|
||||
beego.SessionProvider = "redis"
|
||||
beego.SessionSavePath = "127.0.0.1:6379"
|
||||
|
||||
|
||||
## Cache
|
||||
|
||||
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:
|
||||
|
||||
var (
|
||||
@ -619,7 +726,9 @@ To use cache, you need to initialize a `beego.NewBeeCache` object and set expire
|
||||
- Delete(name string) (ok bool, err error)
|
||||
- IsExist(name string) bool
|
||||
|
||||
##Safe map
|
||||
|
||||
## 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.
|
||||
|
||||
bm := NewBeeMap()
|
||||
@ -646,7 +755,9 @@ This map has following interfaces:
|
||||
- Check(k interface{}) bool
|
||||
- Delete(k interface{})
|
||||
|
||||
##Log
|
||||
|
||||
## Log
|
||||
|
||||
Beego has a default BeeLogger object that outputs log into stdout, and you can use your own logger as well:
|
||||
|
||||
beego.SetLogger(*log.Logger)
|
||||
@ -661,7 +772,8 @@ You can output everything that implemented `*log.Logger`, for example, write to
|
||||
lg := log.New(fd, "", log.Ldate|log.Ltime)
|
||||
beego.SetLogger(lg)
|
||||
|
||||
###Different levels of log
|
||||
|
||||
### Different levels of log
|
||||
|
||||
* Trace(v ...interface{})
|
||||
* Debug(v ...interface{})
|
||||
@ -680,36 +792,48 @@ Your project may have a lot of log outputs, but you don't want to output everyth
|
||||
|
||||
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:
|
||||
|
||||
LevelTrace、LevelDebug、LevelInfo、LevelWarning、 LevelError、LevelCritical
|
||||
LevelTrace, LevelDebug, LevelInfo, LevelWarning, LevelError, LevelCritical
|
||||
|
||||
You can use different log level to output different error messages, it's based on how critical the error you think it is:
|
||||
|
||||
|
||||
### Examples of log messages
|
||||
|
||||
- Trace
|
||||
|
||||
* "Entered parse function validation block"
|
||||
* "Validation: entered second 'if'"
|
||||
* "Dictionary 'Dict' is empty. Using default value"
|
||||
|
||||
- Debug
|
||||
|
||||
* "Web page requested: http://somesite.com Params='...'"
|
||||
* "Response generated. Response size: 10000. Sending."
|
||||
* "New file received. Type:PNG Size:20000"
|
||||
|
||||
- Info
|
||||
|
||||
* "Web server restarted"
|
||||
* "Hourly statistics: Requested pages: 12345 Errors: 123 ..."
|
||||
* "Service paused. Waiting for 'resume' call"
|
||||
|
||||
- Warn
|
||||
|
||||
* "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"
|
||||
|
||||
- Error
|
||||
|
||||
* "Internal error. Cannot process request #12345 Error:...."
|
||||
* "Cannot perform login: credentials DB not responding"
|
||||
|
||||
- Critical
|
||||
|
||||
* "Critical panic received: .... Shutting down"
|
||||
* "Fatal error: ... App is shutting down to prevent data corruption or loss"
|
||||
|
||||
|
||||
### Example
|
||||
|
||||
func internalCalculationFunc(x, y int) (result int, err error) {
|
||||
@ -776,7 +900,9 @@ You can use different log level to output different error messages, it's based o
|
||||
}
|
||||
}
|
||||
|
||||
##Configuration
|
||||
|
||||
## Configuration
|
||||
|
||||
Beego supports to parse .ini file in path `conf/app.conf`, and you have following options:
|
||||
|
||||
appname = beepkg
|
||||
@ -811,7 +937,9 @@ AppConfig supports following methods:
|
||||
- Float(key string) (float64, error)
|
||||
- String(key string) string
|
||||
|
||||
##Beego arguments
|
||||
|
||||
## Beego arguments
|
||||
|
||||
Beego has many configurable arguments, let me introduce to you all of them, so you can use them for more usage in your application:
|
||||
|
||||
* BeeApp
|
||||
@ -889,7 +1017,13 @@ Beego has many configurable arguments, let me introduce to you all of them, so y
|
||||
|
||||
Maximum memory size for file upload, default is `1 << 26`(64M).
|
||||
|
||||
##Integrated third-party applications
|
||||
* EnableGzip
|
||||
|
||||
This value indicate whether enable gzip or not, default is false.
|
||||
|
||||
|
||||
## Integrated third-party applications
|
||||
|
||||
Beego supports to integrate third-party application, you can customized `http.Handler` as follows:
|
||||
|
||||
beego.RouterHandler("/chat/:info(.*)", sockjshandler)
|
||||
@ -943,7 +1077,9 @@ Beego has an example for supporting chat of sockjs, here is the code:
|
||||
|
||||
The above example implemented a simple chat room for sockjs, and you can use `http.Handler` for more extensions.
|
||||
|
||||
##Deployment
|
||||
|
||||
## Deployment
|
||||
|
||||
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.
|
||||
|
||||
$ mkdir /opt/app/beepkg
|
||||
|
@ -1,4 +1,5 @@
|
||||
#Beego
|
||||
# Beego
|
||||
|
||||
Beego is a lightweight, open source, non-blocking and scalable web framework for the Go programming language. It's like tornado in Python. This web framework has already been using for building web server and tools in SNDA's CDN system. Documentation and downloads available at [http://astaxie.github.com/beego](http://astaxie.github.com/beego)
|
||||
|
||||
It has following main features:
|
||||
@ -19,7 +20,9 @@ The working principles of Beego as follows:
|
||||
Beego is licensed under the Apache Licence, Version 2.0
|
||||
(http://www.apache.org/licenses/LICENSE-2.0.html).
|
||||
|
||||
#Simple example
|
||||
|
||||
# Simple example
|
||||
|
||||
The following example prints string "Hello world" to your browser, it shows how easy to build a web application with Beego.
|
||||
|
||||
package main
|
||||
@ -41,12 +44,17 @@ The following example prints string "Hello world" to your browser, it shows how
|
||||
beego.Run()
|
||||
}
|
||||
|
||||
#Handbook
|
||||
|
||||
# Handbook
|
||||
|
||||
- [Purposes](Why.md)
|
||||
- [Installation](Install.md)
|
||||
- [Quick start](Quickstart.md)
|
||||
- [Step by step](Tutorial.md)
|
||||
- [Real world usage](Application.md)
|
||||
- [Hot update](HotUpdate.md)
|
||||
|
||||
#Documentation
|
||||
[godoc](http://godoc.org/github.com/astaxie/beego)
|
||||
|
||||
# Documentation
|
||||
|
||||
[Go Walker](http://gowalker.org/github.com/astaxie/beego)
|
||||
|
@ -1,4 +1,6 @@
|
||||
##supervisord
|
||||
## Supervisord
|
||||
|
||||
[Supervisord](http://supervisord.org/) will make sure your web app is always up.
|
||||
|
||||
1. Installation
|
||||
|
||||
@ -12,7 +14,7 @@
|
||||
|
||||
mkdir /etc/supervisord.conf.d
|
||||
|
||||
2. Configure /etc/supervisord.conf
|
||||
2. Configure `/etc/supervisord.conf`
|
||||
|
||||
[include]
|
||||
files = /etc/supervisord.conf.d/*.conf
|
||||
|
@ -1,13 +1,19 @@
|
||||
# 一步一步跟我写博客
|
||||
|
||||
|
||||
## 创建项目
|
||||
|
||||
|
||||
## 数据库结构设计
|
||||
|
||||
|
||||
## 控制器设计
|
||||
|
||||
|
||||
## 模板设计
|
||||
|
||||
|
||||
## 用户登陆退出
|
||||
|
||||
|
||||
## 数据库操作
|
@ -1,4 +1,5 @@
|
||||
# Design purposes and ideas
|
||||
|
||||
People may ask me why I want to build a new web framework rather than use other good ones. I know there are many excellent web frameworks on the internet and almost all of them are open source, and I have my reasons to do this.
|
||||
|
||||
Remember when I was writing the book about how to build web applications with Go, I just wanted to tell people what were my valuable experiences with Go in web development, especially I have been working with PHP and Python for almost ten years. At first, I didn't realize that a small web framework can give great help to web developers when they are learning to build web applications in a new programming language, and it also helps people more by studying its source code. Finally, I decided to write a open source web framework called Beego as supporting materiel for my book.
|
||||
@ -9,7 +10,7 @@ I used to use CI in PHP and tornado in Python, there are both lightweight, so th
|
||||
2. Learn more about languages by studying their source code, it's not hard to read and understand them because they are both lightweight frameworks.
|
||||
3. It's quite easy to make secondary development of these frameworks for specific purposes.
|
||||
|
||||
Those reasons are my original intention of implementing Beego, and used two chapters in my book to introduce and design this lightweight web framework in GO.
|
||||
Those reasons are my original intention of implementing Beego, and used two chapters in my book to introduce and design this lightweight web framework in Go.
|
||||
|
||||
Then I started to design logic execution of Beego. Because Go and Python have somewhat similar, I referenced some ideas from tornado to design Beego. As you can see, there is no different between Beego and tornado in RESTful processing; they both use GET, POST or some other methods to implement RESTful. I took some ideas from [https://github.com/drone/routes](https://github.com/drone/routes) at the beginning of designing routes. It uses regular expression in route rules processing, which is an excellent idea that to make up for the default Mux router function in Go. However, I have to design my own interface in order to implement RESTful and use inherited ideas in Python.
|
||||
|
||||
|
45
docs/zh/HotUpdate.md
Normal file
45
docs/zh/HotUpdate.md
Normal file
@ -0,0 +1,45 @@
|
||||
## 热升级是什么?
|
||||
|
||||
热升级是什么呢?了解nginx的同学都知道,nginx是支持热升级的,可以用老进程服务先前链接的链接,使用新进程服务新的链接,即在不停止服务的情况下完成系统的升级与运行参数修改。那么热升级和热编译是不同的概念,热编译是通过监控文件的变化重新编译,然后重启进程,例如bee start就是这样的工具
|
||||
|
||||
|
||||
## 热升级有必要吗?
|
||||
|
||||
很多人认为HTTP的应用有必要支持热升级吗?那么我可以很负责的说非常有必要,不中断服务始终是我们所追求的目标,虽然很多人说可能服务器会坏掉等等,这个是属于高可用的设计范畴,不要搞混了,这个是可预知的问题,所以我们需要避免这样的升级带来的用户不可用。你还在为以前升级搞到凌晨升级而烦恼嘛?那么现在就赶紧拥抱热升级吧。
|
||||
|
||||
|
||||
## beego如何支持热升级
|
||||
热升级的原理基本上就是:主进程fork一个进程,然后子进程exec相应的程序。那么这个过程中发生了什么呢?我们知道进程fork之后会把主进程的所有句柄、数据和堆栈继承过来、但是里面所有的句柄存在一个叫做CloseOnExec,也就是执行exec的时候,copy的所有的句柄都被关闭了,除非特别申明,而我们期望的是子进程能够复用主进程的net.Listener的句柄。一个进程一旦调用exec类函数,它本身就"死亡"了,系统把代码段替换成新的程序的代码,废弃原有的数据段和堆栈段,并为新程序分配新的数据段与堆栈段,唯一留下的,就是进程号,也就是说,对系统而言,还是同一个进程,不过已经是另一个程序了。
|
||||
|
||||
那么我们要做的第一步就是让子进程继承主进程的这个句柄,我们可以通过os.StartProcess的参数来附加Files,把需要继承的句柄写在里面。
|
||||
|
||||
第二步就是我们希望子进程能够从这个句柄启动监听,还好Go里面支持net.FileListener,直接从句柄来监听,但是我们需要子进程知道这个FD,所以在启动子进程的时候我们设置了一个环境变量设置这个FD。
|
||||
|
||||
第三步就是我们期望老的链接继续服务完,而新的链接采用新的进程,这里面有两个细节,第一就是老的链接继续服务,那么我们怎么知道有老链接存在?所以我们必须每次接收一个链接记录一下,这样我们就知道还存在没有服务完的链接,第二就是怎么让老进程停止接收数据,让新进程接收数据呢?大家都监听在同一个端口,理论上是随机来接收的,所以这里我们只要关闭老的链接的接收就行,这样就会使得在l.Accept的时候报错。
|
||||
|
||||
上面是我们需要解决的三个方面的问题,具体的实现大家可以看我实现的代码逻辑。
|
||||
|
||||
|
||||
## 如何演示热升级
|
||||
|
||||
1. 编写代码,在beego应用的控制器中Get方法实现大概如下:
|
||||
|
||||
func (this *MainController) Get() {
|
||||
a, _ := this.GetInt("sleep")
|
||||
time.Sleep(time.Duration(a) * time.Second)
|
||||
this.Ctx.WriteString("ospid:" + strconv.Itoa(os.Getpid()))
|
||||
}
|
||||
|
||||
2. 打开两个终端
|
||||
|
||||
一个终端输入:` ps -ef|grep 应用名`
|
||||
|
||||
一个终端输入请求:`curl "http://127.0.0.1:8080/?sleep=20"`
|
||||
|
||||
3. 热升级
|
||||
|
||||
`kill -HUP 进程ID`
|
||||
|
||||
4. 打开一个终端输入请求:`curl "http://127.0.0.1:8080/?sleep=0"`
|
||||
|
||||
我们可以看到这样的结果,第一个请求等待20s,但是处理他的是老的进程,热升级之后,第一个请求还在执行,最后会输出老的进程ID,而第二次请求,输出的是新的进程ID
|
@ -1,4 +1,5 @@
|
||||
# 安装入门
|
||||
|
||||
beego虽然是一个简单的框架,但是其中用到了很多第三方的包,所以在你安装beego的过程中Go会自动安装其他关联的包。
|
||||
|
||||
- 当然第一步你需要安装Go,如何安装Go请参考我的书[第一章](https://github.com/astaxie/build-web-application-with-golang/blob/master/ebook/01.1.md)
|
||||
@ -19,6 +20,7 @@ beego虽然是一个简单的框架,但是其中用到了很多第三方的包
|
||||
|
||||
> - session模块:github.com/astaxie/beego/session
|
||||
|
||||
|
||||
> - session模块中支持redis引擎:github.com/garyburd/redigo/redis
|
||||
|
||||
> - session模块中支持mysql引擎:github.com/go-sql-driver/mysql
|
||||
|
@ -1,4 +1,5 @@
|
||||
# 快速入门
|
||||
|
||||
你对beego一无所知?没关系,这篇文档会很好的详细介绍beego的各个方面,看这个文档之前首先确认你已经安装了beego,如果你没有安装的话,请看这篇[安装指南](Install.md)
|
||||
|
||||
**导航**
|
||||
@ -23,7 +24,9 @@
|
||||
- [第三方应用集成](#-19)
|
||||
- [部署编译应用](#-20)
|
||||
|
||||
|
||||
## 最小应用
|
||||
|
||||
一个最小最简单的应用如下代码所示:
|
||||
|
||||
package main
|
||||
@ -68,6 +71,7 @@
|
||||
|
||||
停止服务的话,请按`ctrl+c`
|
||||
|
||||
|
||||
## 新建项目
|
||||
|
||||
通过如下命令创建beego项目,首先进入gopath目录
|
||||
@ -90,6 +94,7 @@
|
||||
└── views
|
||||
└── index.tpl
|
||||
|
||||
|
||||
## 开发模式
|
||||
|
||||
通过bee创建的项目,beego默认情况下是开发模式。
|
||||
@ -115,6 +120,7 @@
|
||||
|
||||

|
||||
|
||||
|
||||
## 路由设置
|
||||
|
||||
路由的主要功能是实现从请求地址到实现方法,beego中封装了`Controller`,所以路由是从路径到`ControllerInterface`的过程,`ControllerInterface`的方法有如下:
|
||||
@ -173,7 +179,9 @@
|
||||
this.Ctx.Params[":path"]
|
||||
this.Ctx.Params[":ext"]
|
||||
|
||||
|
||||
## 静态文件
|
||||
|
||||
Go语言内部其实已经提供了`http.ServeFile`,通过这个函数可以实现静态文件的服务。beego针对这个功能进行了一层封装,通过下面的方式进行静态文件注册:
|
||||
|
||||
beego.SetStaticPath("/static","public")
|
||||
@ -189,7 +197,9 @@ beego支持多个目录的静态文件注册,用户可以注册如下的静态
|
||||
|
||||
设置了如上的静态目录之后,用户访问`/images/login/login.png`,那么就会访问应用对应的目录下面的`images/login/login.png`文件。如果是访问`/static/img/logo.png`,那么就访问`public/img/logo.png`文件。
|
||||
|
||||
|
||||
## 过滤和中间件
|
||||
|
||||
beego支持自定义过滤中间件,例如安全验证,强制跳转等
|
||||
|
||||
如下例子所示,验证用户名是否是admin,应用于全部的请求:
|
||||
@ -215,7 +225,9 @@ beego支持自定义过滤中间件,例如安全验证,强制跳转等
|
||||
dosomething()
|
||||
})
|
||||
|
||||
|
||||
## 控制器设计
|
||||
|
||||
基于beego的Controller设计,只需要匿名组合`beego.Controller`就可以了,如下所示:
|
||||
|
||||
type xxxController struct {
|
||||
@ -279,7 +291,7 @@ beego支持自定义过滤中间件,例如安全验证,强制跳转等
|
||||
}
|
||||
|
||||
func (this *AddController) Get() {
|
||||
this.Data["content"] ="value"
|
||||
this.Data["content"] = "value"
|
||||
this.Layout = "admin/layout.html"
|
||||
this.TplNames = "admin/add.tpl"
|
||||
}
|
||||
@ -303,13 +315,20 @@ beego支持自定义过滤中间件,例如安全验证,强制跳转等
|
||||
this.Ctx.Redirect(302, "/admin/index")
|
||||
}
|
||||
|
||||
|
||||
## 模板处理
|
||||
|
||||
|
||||
### 模板目录
|
||||
|
||||
beego中默认的模板目录是`views`,用户可以把你的模板文件放到该目录下,beego会自动在该目录下的所有模板文件进行解析并缓存,开发模式下会每次重新解析,不做缓存。当然用户可以通过如下的方式改变模板的目录:
|
||||
|
||||
beego.ViewsPath = "/myviewpath"
|
||||
|
||||
|
||||
### 自动渲染
|
||||
beego中用户无需手动的调用渲染输出模板,beego会自动的在调用玩相应的method方法之后调用Render函数,当然如果你的应用是不需要模板输出的,那么你可以在配置文件或者在main.go中设置关闭自动渲染。
|
||||
|
||||
beego中用户无需手动的调用渲染输出模板,beego会自动的在调用完相应的method方法之后调用Render函数,当然如果你的应用是不需要模板输出的,那么你可以在配置文件或者在main.go中设置关闭自动渲染。
|
||||
|
||||
配置文件配置如下:
|
||||
|
||||
@ -319,12 +338,16 @@ main.go文件中设置如下:
|
||||
|
||||
beego.AutoRender = false
|
||||
|
||||
|
||||
### 模板数据
|
||||
|
||||
模板中的数据是通过在Controller中`this.Data`获取的,所以如果你想在模板中获取内容`{{.Content}}`,那么你需要在Controller中如下设置:
|
||||
|
||||
this.Data["Context"] = "value"
|
||||
|
||||
|
||||
### 模板名称
|
||||
|
||||
beego采用了Go语言内置的模板引擎,所有模板的语法和Go的一模一样,至于如何写模板文件,详细的请参考[模板教程](https://github.com/astaxie/build-web-application-with-golang/blob/master/ebook/07.4.md)。
|
||||
|
||||
用户通过在Controller的对应方法中设置相应的模板名称,beego会自动的在viewpath目录下查询该文件并渲染,例如下面的设置,beego会在admin下面找add.tpl文件进行渲染:
|
||||
@ -341,7 +364,9 @@ beego采用了Go语言内置的模板引擎,所有模板的语法和Go的一
|
||||
|
||||
也就是你对应的Controller名字+请求方法名.模板后缀,也就是如果你的Controller名是`AddController`,请求方法是`POST`,默认的文件后缀是`tpl`,那么就会默认请求`/viewpath/AddController/POST.tpl`文件。
|
||||
|
||||
|
||||
### layout设计
|
||||
|
||||
beego支持layout设计,例如你在管理系统中,其实整个的管理界面是固定的,只会变化中间的部分,那么你可以通过如下的设置:
|
||||
|
||||
this.Layout = "admin/layout.html"
|
||||
@ -359,7 +384,9 @@ beego就会首先解析TplNames指定的文件,获取内容赋值给LayoutCont
|
||||
处理逻辑
|
||||
{{template "footer.html"}}
|
||||
|
||||
|
||||
### 模板函数
|
||||
|
||||
beego支持用户定义模板函数,但是必须在`beego.Run()`调用之前,设置如下:
|
||||
|
||||
func hello(in string)(out string){
|
||||
@ -378,32 +405,42 @@ beego支持用户定义模板函数,但是必须在`beego.Run()`调用之前
|
||||
* markdown
|
||||
|
||||
实现了把markdown文本转化为html信息,使用方法{{markdown .Content}}
|
||||
|
||||
* dateformat
|
||||
|
||||
实现了时间的格式化,返回字符串,使用方法{{dateformat .Time "2006-01-02T15:04:05Z07:00"}}
|
||||
|
||||
* date
|
||||
|
||||
实现了类似PHP的date函数,可以很方便的根据字符串返回时间,使用方法{{date .T "Y-m-d H:i:s"}}
|
||||
|
||||
* compare
|
||||
|
||||
实现了比较两个对象的比较,如果相同返回true,否者false,使用方法{{compare .A .B}}
|
||||
|
||||
* substr
|
||||
|
||||
实现了字符串的截取,支持中文截取的完美截取,使用方法{{substr .Str 0 30}}
|
||||
|
||||
* html2str
|
||||
|
||||
实现了把html转化为字符串,剔除一些script、css之类的元素,返回纯文本信息,使用方法{{html2str .Htmlinfo}}
|
||||
|
||||
* str2html
|
||||
|
||||
实现了把相应的字符串当作HTML来输出,不转义,使用方法{{str2html .Strhtml}}
|
||||
|
||||
* htmlquote
|
||||
|
||||
实现了基本的html字符转义,使用方法{{htmlquote .quote}}
|
||||
|
||||
* htmlunquote
|
||||
|
||||
实现了基本的反转移字符,使用方法{{htmlunquote .unquote}}
|
||||
|
||||
|
||||
## request处理
|
||||
|
||||
我们经常需要获取用户传递的数据,包括Get、POST等方式的请求,beego里面会自动解析这些数据,你可以通过如下方式获取数据
|
||||
|
||||
- GetString(key string) string
|
||||
@ -429,7 +466,9 @@ beego支持用户定义模板函数,但是必须在`beego.Run()`调用之前
|
||||
|
||||
更多其他的request的信息,用户可以通过`this.Ctx.Request`获取信息,关于该对象的属性和方法参考手册[Request](http://golang.org/pkg/net/http/#Request)
|
||||
|
||||
|
||||
### 文件上传
|
||||
|
||||
在beego中你可以很容易的处理文件上传,就是别忘记在你的form表单中增加这个属性`enctype="multipart/form-data"`,否者你的浏览器不会传输你的上传文件。
|
||||
|
||||
文件上传之后一般是放在系统的内存里面,如果文件的size大于设置的缓存内存大小,那么就放在临时文件中,默认的缓存内存是64M,你可以通过如下来调整这个缓存内存大小:
|
||||
@ -456,7 +495,9 @@ beego提供了两个很方便的方法来处理文件上传:
|
||||
this.SaveToFile("the_file","/var/www/uploads/uploaded_file.txt"")
|
||||
}
|
||||
|
||||
|
||||
### JSON和XML输出
|
||||
|
||||
beego当初设计的时候就考虑了API功能的设计,而我们在设计API的时候经常是输出JSON或者XML数据,那么beego提供了这样的方式直接输出:
|
||||
|
||||
JSON数据直接输出,设置`content-type`为`application/json`:
|
||||
@ -475,7 +516,9 @@ XML数据直接输出,设置`content-type`为`application/xml`:
|
||||
this.ServeXml()
|
||||
}
|
||||
|
||||
|
||||
## 跳转和错误
|
||||
|
||||
我们在做Web开发的时候,经常会遇到页面调整和错误处理,beego这这方面也进行了考虑,通过`Redirect`方法来进行跳转:
|
||||
|
||||
func (this *AddController) Get() {
|
||||
@ -535,7 +578,9 @@ beego更加人性化的还有一个设计就是支持用户自定义字符串错
|
||||
|
||||
一旦在入口注册该错误处理代码,那么你可以在任何你的逻辑中遇到数据库错误调用`this.Abort("dbError")`来进行异常页面处理。
|
||||
|
||||
|
||||
## response处理
|
||||
|
||||
response可能会有集中情况:
|
||||
|
||||
1. 模板输出
|
||||
@ -552,7 +597,9 @@ response可能会有集中情况:
|
||||
|
||||
this.Ctx.WriteString("ok")
|
||||
|
||||
|
||||
## Sessions
|
||||
|
||||
beego内置了session模块,目前session模块支持的后端引擎包括memory、file、mysql、redis四中,用户也可以根据相应的interface实现自己的引擎。
|
||||
|
||||
beego中使用session相当方便,只要在main入口函数中设置如下:
|
||||
@ -637,7 +684,9 @@ sess对象具有如下方法:
|
||||
beego.SessionProvider = "redis"
|
||||
beego.SessionSavePath = "127.0.0.1:6379"
|
||||
|
||||
|
||||
## Cache设置
|
||||
|
||||
beego内置了一个cache模块,实现了类似memcache的功能,缓存数据在内存中,主要的使用方法如下:
|
||||
|
||||
var (
|
||||
@ -681,7 +730,9 @@ beego内置了一个cache模块,实现了类似memcache的功能,缓存数
|
||||
- Delete(name string) (ok bool, err error)
|
||||
- IsExist(name string) bool
|
||||
|
||||
|
||||
## 安全的Map
|
||||
|
||||
我们知道在Go语言里面map是非线程安全的,详细的[atomic_maps](http://golang.org/doc/faq#atomic_maps)。但是我们在平常的业务中经常需要用到线程安全的map,特别是在goroutine的情况下,所以beego内置了一个简单的线程安全的map:
|
||||
|
||||
bm := NewBeeMap()
|
||||
@ -708,20 +759,39 @@ beego内置了一个cache模块,实现了类似memcache的功能,缓存数
|
||||
- Check(k interface{}) bool
|
||||
- Delete(k interface{})
|
||||
|
||||
|
||||
## 日志处理
|
||||
|
||||
beego默认有一个初始化的BeeLogger对象输出内容到stdout中,你可以通过如下的方式设置自己的输出:
|
||||
|
||||
beego.SetLogger(*log.Logger)
|
||||
|
||||
只要你的输出符合`*log.Logger`就可以,例如输出到文件:
|
||||
现在beego支持文件方式输出到,而且支持文件的自动化logrotate,在main函数入口处初始化如下:
|
||||
|
||||
fd,err := os.OpenFile("/var/log/beeapp/beeapp.log", os.O_RDWR|os.O_APPEND, 0644)
|
||||
filew := beego.NewFileWriter("tmp/log.log", true)
|
||||
err := filew.StartLogger()
|
||||
if err != nil {
|
||||
beego.Critical("openfile beeapp.log:", err)
|
||||
return
|
||||
beego.Critical("NewFileWriter err", err)
|
||||
}
|
||||
lg := log.New(fd, "", log.Ldate|log.Ltime)
|
||||
beego.SetLogger(lg)
|
||||
|
||||
这样就默认开始在当前目录的tmp/log.log文件中开始记录日志,默认支持文件的logrotate,第二个参数为true表示开启,false表示关闭,开启的rotate的规则如下:
|
||||
|
||||
1. 1000000行日志就自动分割
|
||||
2. 文件的大小为256M就自动分割
|
||||
3. 每天进行分割
|
||||
4. 日志默认保存7天
|
||||
|
||||
一天之中分割不能多余999个,每个分割的文件名是`定义的文件名.日期.三位数字`
|
||||
|
||||
用户可以通过如下函数修改相应的日志切割规则:
|
||||
|
||||
- func (w *FileLogWriter) SetRotateDaily(daily bool) *FileLogWriter
|
||||
- func (w *FileLogWriter) SetRotateLines(maxlines int) *FileLogWriter
|
||||
- func (w *FileLogWriter) SetRotateMaxDay(maxday int64) *FileLogWriter
|
||||
- func (w *FileLogWriter) SetRotateSize(maxsize int) *FileLogWriter
|
||||
|
||||
但是这些函数调用必须在调用`StartLogger`之前。
|
||||
|
||||
### 不同级别的log日志函数
|
||||
|
||||
* Trace(v ...interface{})
|
||||
@ -741,56 +811,68 @@ beego默认有一个初始化的BeeLogger对象输出内容到stdout中,你可
|
||||
|
||||
这样的话就不会输出小于这个level的日志,日志的排序如下:
|
||||
|
||||
LevelTrace、LevelDebug、LevelInfo、LevelWarning、 LevelError、LevelCritical
|
||||
LevelTrace、LevelDebug、LevelInfo、LevelWarning、LevelError、LevelCritical
|
||||
|
||||
用户可以根据不同的级别输出不同的错误信息,如下例子所示:
|
||||
|
||||
|
||||
### Examples of log messages
|
||||
|
||||
- Trace
|
||||
|
||||
* "Entered parse function validation block"
|
||||
* "Validation: entered second 'if'"
|
||||
* "Dictionary 'Dict' is empty. Using default value"
|
||||
|
||||
- Debug
|
||||
|
||||
* "Web page requested: http://somesite.com Params='...'"
|
||||
* "Response generated. Response size: 10000. Sending."
|
||||
* "New file received. Type:PNG Size:20000"
|
||||
|
||||
- Info
|
||||
|
||||
* "Web server restarted"
|
||||
* "Hourly statistics: Requested pages: 12345 Errors: 123 ..."
|
||||
* "Service paused. Waiting for 'resume' call"
|
||||
|
||||
- Warn
|
||||
|
||||
* "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"
|
||||
|
||||
- Error
|
||||
|
||||
* "Internal error. Cannot process request #12345 Error:...."
|
||||
* "Cannot perform login: credentials DB not responding"
|
||||
|
||||
- Critical
|
||||
|
||||
* "Critical panic received: .... Shutting down"
|
||||
* "Fatal error: ... App is shutting down to prevent data corruption or loss"
|
||||
|
||||
|
||||
### Example
|
||||
|
||||
func internalCalculationFunc(x, y int) (result int, err error) {
|
||||
beego.Debug("calculating z. x:",x," y:",y)
|
||||
beego.Debug("calculating z. x:", x, " y:", y)
|
||||
z := y
|
||||
switch {
|
||||
case x == 3 :
|
||||
case x == 3:
|
||||
beego.Trace("x == 3")
|
||||
panic("Failure.")
|
||||
case y == 1 :
|
||||
case y == 1:
|
||||
beego.Trace("y == 1")
|
||||
return 0, errors.New("Error!")
|
||||
case y == 2 :
|
||||
case y == 2:
|
||||
beego.Trace("y == 2")
|
||||
z = x
|
||||
default :
|
||||
default:
|
||||
beego.Trace("default")
|
||||
z += x
|
||||
}
|
||||
retVal := z-3
|
||||
retVal := z - 3
|
||||
beego.Debug("Returning ", retVal)
|
||||
|
||||
return retVal, nil
|
||||
@ -800,18 +882,18 @@ LevelTrace、LevelDebug、LevelInfo、LevelWarning、 LevelError、LevelCritical
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
beego.Error("Unexpected error occurred: ", r)
|
||||
outputs <- outputData{result : 0, error : true}
|
||||
outputs <- outputData{result: 0, error: true}
|
||||
}
|
||||
}()
|
||||
beego.Info("Received input signal. x:",input.x," y:", input.y)
|
||||
beego.Info("Received input signal. x:", input.x, " y:", input.y)
|
||||
|
||||
res, err := internalCalculationFunc(input.x, input.y)
|
||||
if err != nil {
|
||||
beego.Warn("Error in calculation:", err.Error())
|
||||
}
|
||||
|
||||
beego.Info("Returning result: ",res," error: ",err)
|
||||
outputs <- outputData{result : res, error : err != nil}
|
||||
beego.Info("Returning result: ", res, " error: ", err)
|
||||
outputs <- outputData{result: res, error: err != nil}
|
||||
}
|
||||
|
||||
func main() {
|
||||
@ -828,16 +910,18 @@ LevelTrace、LevelDebug、LevelInfo、LevelWarning、 LevelError、LevelCritical
|
||||
|
||||
for {
|
||||
select {
|
||||
case input := <- inputs:
|
||||
case input := <-inputs:
|
||||
processInput(input)
|
||||
case <- criticalChan:
|
||||
case <-criticalChan:
|
||||
beego.Critical("Caught value from criticalChan: Go shut down.")
|
||||
panic("Shut down due to critical fault.")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
## 配置管理
|
||||
|
||||
beego支持解析ini文件, beego默认会解析当前应用下的`conf/app.conf`文件
|
||||
|
||||
通过这个文件你可以初始化很多beego的默认参数
|
||||
@ -874,7 +958,9 @@ AppConfig支持如下方法
|
||||
- Float(key string) (float64, error)
|
||||
- String(key string) string
|
||||
|
||||
|
||||
## 系统默认参数
|
||||
|
||||
beego中带有很多可配置的参数,我们来一一认识一下它们,这样有利于我们在接下来的beego开发中可以充分的发挥他们的作用:
|
||||
|
||||
* BeeApp
|
||||
@ -959,7 +1045,12 @@ beego中带有很多可配置的参数,我们来一一认识一下它们,这
|
||||
|
||||
是否开启gzip支持,默认为false不支持gzip,一旦开启了gzip,那么在模板输出的内容会进行gzip或者zlib压缩,根据用户的Accept-Encoding来判断。
|
||||
|
||||
* DirectoryIndex
|
||||
|
||||
是否开启静态目录的列表显示,默认不显示目录,返回403错误
|
||||
|
||||
## 第三方应用集成
|
||||
|
||||
beego支持第三方应用的集成,用户可以自定义`http.Handler`,用户可以通过如下方式进行注册路由:
|
||||
|
||||
beego.RouterHandler("/chat/:info(.*)", sockjshandler)
|
||||
@ -1013,7 +1104,9 @@ sockjshandler实现了接口`http.Handler`。
|
||||
|
||||
通过上面的代码很简单的实现了一个多人的聊天室。上面这个只是一个sockjs的例子,我想通过大家自定义`http.Handler`,可以有很多种方式来进行扩展beego应用。
|
||||
|
||||
|
||||
## 部署编译应用
|
||||
|
||||
Go语言的应用最后编译之后是一个二进制文件,你只需要copy这个应用到服务器上,运行起来就行。beego由于带有几个静态文件、配置文件、模板文件三个目录,所以用户部署的时候需要同时copy这三个目录到相应的部署应用之下,下面以我实际的应用部署为例:
|
||||
|
||||
$ mkdir /opt/app/beepkg
|
||||
|
@ -34,6 +34,7 @@ beego是一个类似tornado的Go应用框架,采用了RESTFul的方式来实
|
||||
beego.Run()
|
||||
}
|
||||
|
||||
|
||||
# beego 指南
|
||||
|
||||
* [为什么设计beego](Why.md)
|
||||
@ -41,9 +42,11 @@ beego是一个类似tornado的Go应用框架,采用了RESTFul的方式来实
|
||||
* [快速入门](Quickstart.md)
|
||||
* [一步一步开发应用](Tutorial.md)
|
||||
* [beego案例](Application.md)
|
||||
* [热升级](HotUpdate.md)
|
||||
|
||||
|
||||
# API接口
|
||||
|
||||
API对于我们平时开发应用非常有用,用于查询一些开发的函数,godoc做的非常好了
|
||||
|
||||
[godoc](http://godoc.org/github.com/astaxie/beego)
|
||||
[Go Walker](http://gowalker.org/github.com/astaxie/beego)
|
||||
|
@ -1,13 +1,19 @@
|
||||
# 一步一步跟我写博客
|
||||
|
||||
|
||||
## 创建项目
|
||||
|
||||
|
||||
## 数据库结构设计
|
||||
|
||||
|
||||
## 控制器设计
|
||||
|
||||
|
||||
## 模板设计
|
||||
|
||||
|
||||
## 用户登陆退出
|
||||
|
||||
|
||||
## 数据库操作
|
@ -74,7 +74,7 @@ var errtpl = `
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
||||
<title>Page Not Found</title>
|
||||
<title>{{.Title}}</title>
|
||||
<style type="text/css">
|
||||
* {
|
||||
margin:0;
|
||||
|
182
log.go
182
log.go
@ -1,13 +1,166 @@
|
||||
package beego
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
//--------------------
|
||||
// LOG LEVEL
|
||||
//--------------------
|
||||
type FileLogWriter struct {
|
||||
*log.Logger
|
||||
// The opened file
|
||||
filename string
|
||||
|
||||
maxlines int
|
||||
maxlines_curlines int
|
||||
|
||||
// Rotate at size
|
||||
maxsize int
|
||||
maxsize_cursize int
|
||||
|
||||
// Rotate daily
|
||||
daily bool
|
||||
maxday int64
|
||||
daily_opendate int
|
||||
|
||||
rotate bool
|
||||
|
||||
startLock sync.Mutex //only one log can writer to the file
|
||||
}
|
||||
|
||||
func NewFileWriter(fname string, rotate bool) *FileLogWriter {
|
||||
w := &FileLogWriter{
|
||||
filename: fname,
|
||||
maxlines: 1000000,
|
||||
maxsize: 1 << 28, //256 MB
|
||||
daily: true,
|
||||
maxday: 7,
|
||||
rotate: rotate,
|
||||
}
|
||||
return w
|
||||
}
|
||||
|
||||
// Set rotate at linecount (chainable). Must be called before call StartLogger
|
||||
func (w *FileLogWriter) SetRotateLines(maxlines int) *FileLogWriter {
|
||||
w.maxlines = maxlines
|
||||
return w
|
||||
}
|
||||
|
||||
// Set rotate at size (chainable). Must be called before call StartLogger
|
||||
func (w *FileLogWriter) SetRotateSize(maxsize int) *FileLogWriter {
|
||||
w.maxsize = maxsize
|
||||
return w
|
||||
}
|
||||
|
||||
// Set rotate daily (chainable). Must be called before call StartLogger
|
||||
func (w *FileLogWriter) SetRotateDaily(daily bool) *FileLogWriter {
|
||||
w.daily = daily
|
||||
return w
|
||||
}
|
||||
|
||||
// Set rotate daily's log keep for maxday,other will delete
|
||||
func (w *FileLogWriter) SetRotateMaxDay(maxday int64) *FileLogWriter {
|
||||
w.maxday = maxday
|
||||
return w
|
||||
}
|
||||
|
||||
func (w *FileLogWriter) StartLogger() error {
|
||||
if err := w.DoRotate(false); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *FileLogWriter) docheck(size int) {
|
||||
w.startLock.Lock()
|
||||
defer w.startLock.Unlock()
|
||||
if (w.maxlines > 0 && w.maxlines_curlines >= w.maxlines) ||
|
||||
(w.maxsize > 0 && w.maxsize_cursize >= w.maxsize) ||
|
||||
(w.daily && time.Now().Day() != w.daily_opendate) {
|
||||
if err := w.DoRotate(true); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "FileLogWriter(%q): %s\n", w.filename, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
w.maxlines_curlines++
|
||||
w.maxsize_cursize += size
|
||||
}
|
||||
|
||||
func (w *FileLogWriter) Printf(format string, v ...interface{}) {
|
||||
// Perform the write
|
||||
str := fmt.Sprintf(format, v...)
|
||||
n := 24 + len(str) // 24 stand for the length "2013/06/23 21:00:22 [T] "
|
||||
|
||||
w.docheck(n)
|
||||
w.Logger.Printf(format, v...)
|
||||
}
|
||||
|
||||
func (w *FileLogWriter) DoRotate(rotate bool) error {
|
||||
if rotate {
|
||||
_, err := os.Lstat(w.filename)
|
||||
if err == nil { // file exists
|
||||
// Find the next available number
|
||||
num := 1
|
||||
fname := ""
|
||||
for ; err == nil && num <= 999; num++ {
|
||||
fname = w.filename + fmt.Sprintf(".%s.%03d", time.Now().Format("2006-01-02"), num)
|
||||
_, err = os.Lstat(fname)
|
||||
}
|
||||
// return error if the last file checked still existed
|
||||
if err == nil {
|
||||
return fmt.Errorf("Rotate: Cannot find free log number to rename %s\n", w.filename)
|
||||
}
|
||||
|
||||
// Rename the file to its newfound home
|
||||
err = os.Rename(w.filename, fname)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Rotate: %s\n", err)
|
||||
}
|
||||
go w.deleteOldLog()
|
||||
}
|
||||
}
|
||||
|
||||
// Open the log file
|
||||
fd, err := os.OpenFile(w.filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0660)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
w.Logger = log.New(fd, "", log.Ldate|log.Ltime)
|
||||
finfo, err := fd.Stat()
|
||||
if err != nil {
|
||||
return fmt.Errorf("get stat err: %s\n", err)
|
||||
}
|
||||
w.maxsize_cursize = int(finfo.Size())
|
||||
w.daily_opendate = time.Now().Day()
|
||||
if finfo.Size() > 0 {
|
||||
content, err := ioutil.ReadFile(w.filename)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
w.maxlines_curlines = len(strings.Split(string(content), "\n"))
|
||||
|
||||
} else {
|
||||
w.maxlines_curlines = 0
|
||||
}
|
||||
BeeLogger = w
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *FileLogWriter) deleteOldLog() {
|
||||
dir := path.Dir(w.filename)
|
||||
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
|
||||
if !info.IsDir() && info.ModTime().Unix() < (time.Now().Unix()-60*60*24*w.maxday) {
|
||||
os.Remove(path)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// Log levels to control the logging output.
|
||||
const (
|
||||
@ -34,8 +187,29 @@ func SetLevel(l int) {
|
||||
level = l
|
||||
}
|
||||
|
||||
type IBeeLogger interface {
|
||||
Fatal(v ...interface{})
|
||||
Fatalf(format string, v ...interface{})
|
||||
Fatalln(v ...interface{})
|
||||
Flags() int
|
||||
Output(calldepth int, s string) error
|
||||
Panic(v ...interface{})
|
||||
Panicf(format string, v ...interface{})
|
||||
Panicln(v ...interface{})
|
||||
Prefix() string
|
||||
Print(v ...interface{})
|
||||
Printf(format string, v ...interface{})
|
||||
Println(v ...interface{})
|
||||
SetFlags(flag int)
|
||||
SetPrefix(prefix string)
|
||||
}
|
||||
|
||||
// logger references the used application logger.
|
||||
var BeeLogger = log.New(os.Stdout, "", log.Ldate|log.Ltime)
|
||||
var BeeLogger IBeeLogger
|
||||
|
||||
func init() {
|
||||
BeeLogger = log.New(os.Stdout, "", log.Ldate|log.Ltime)
|
||||
}
|
||||
|
||||
// SetLogger sets a new logger.
|
||||
func SetLogger(l *log.Logger) {
|
||||
|
152
reload.go
Normal file
152
reload.go
Normal file
@ -0,0 +1,152 @@
|
||||
// Zero-downtime restarts in Go.
|
||||
package beego
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"os/exec"
|
||||
"os/signal"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"sync"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
const (
|
||||
FDKey = "BEEGO_HOT_FD"
|
||||
)
|
||||
|
||||
// Export an error equivalent to net.errClosing for use with Accept during
|
||||
// a graceful exit.
|
||||
var ErrClosing = errors.New("use of closed network connection")
|
||||
var ErrInitStart = errors.New("init from")
|
||||
|
||||
// Allows for us to notice when the connection is closed.
|
||||
type conn struct {
|
||||
net.Conn
|
||||
wg *sync.WaitGroup
|
||||
}
|
||||
|
||||
func (c conn) Close() error {
|
||||
err := c.Conn.Close()
|
||||
c.wg.Done()
|
||||
return err
|
||||
}
|
||||
|
||||
type stoppableListener struct {
|
||||
net.Listener
|
||||
count int64
|
||||
stopped bool
|
||||
wg sync.WaitGroup
|
||||
}
|
||||
|
||||
var theStoppable *stoppableListener
|
||||
|
||||
func newStoppable(l net.Listener) (sl *stoppableListener) {
|
||||
sl = &stoppableListener{Listener: l}
|
||||
|
||||
// this goroutine monitors the channel. Can't do this in
|
||||
// Accept (below) because once it enters sl.Listener.Accept()
|
||||
// it blocks. We unblock it by closing the fd it is trying to
|
||||
// accept(2) on.
|
||||
go func() {
|
||||
WaitSignal(l)
|
||||
sl.stopped = true
|
||||
sl.Listener.Close()
|
||||
}()
|
||||
return
|
||||
}
|
||||
|
||||
func (sl *stoppableListener) Accept() (c net.Conn, err error) {
|
||||
c, err = sl.Listener.Accept()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
sl.wg.Add(1)
|
||||
// Wrap the returned connection, so that we can observe when
|
||||
// it is closed.
|
||||
c = conn{Conn: c, wg: &sl.wg}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func WaitSignal(l net.Listener) error {
|
||||
ch := make(chan os.Signal, 1)
|
||||
signal.Notify(ch, syscall.SIGTERM, syscall.SIGHUP)
|
||||
for {
|
||||
sig := <-ch
|
||||
log.Println(sig.String())
|
||||
switch sig {
|
||||
|
||||
case syscall.SIGTERM:
|
||||
return nil
|
||||
case syscall.SIGHUP:
|
||||
err := Restart(l)
|
||||
if nil != err {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return nil // It'll never get here.
|
||||
}
|
||||
|
||||
func CloseSelf() error {
|
||||
ppid := os.Getpid()
|
||||
if ppid == 1 { // init provided sockets, for example systemd
|
||||
return nil
|
||||
}
|
||||
p, err := os.FindProcess(ppid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return p.Kill()
|
||||
}
|
||||
|
||||
// Re-exec this image without dropping the listener passed to this function.
|
||||
func Restart(l net.Listener) error {
|
||||
argv0, err := exec.LookPath(os.Args[0])
|
||||
if nil != err {
|
||||
return err
|
||||
}
|
||||
wd, err := os.Getwd()
|
||||
if nil != err {
|
||||
return err
|
||||
}
|
||||
v := reflect.ValueOf(l).Elem().FieldByName("fd").Elem()
|
||||
fd := uintptr(v.FieldByName("sysfd").Int())
|
||||
allFiles := append([]*os.File{os.Stdin, os.Stdout, os.Stderr},
|
||||
os.NewFile(fd, string(v.FieldByName("sysfile").String())))
|
||||
|
||||
p, err := os.StartProcess(argv0, os.Args, &os.ProcAttr{
|
||||
Dir: wd,
|
||||
Env: append(os.Environ(), fmt.Sprintf("%s=%d", FDKey, fd)),
|
||||
Files: allFiles,
|
||||
})
|
||||
if nil != err {
|
||||
return err
|
||||
}
|
||||
log.Printf("spawned child %d\n", p.Pid)
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetInitListner(tcpaddr *net.TCPAddr) (l net.Listener, err error) {
|
||||
countStr := os.Getenv(FDKey)
|
||||
if countStr == "" {
|
||||
return net.ListenTCP("tcp", tcpaddr)
|
||||
} else {
|
||||
count, err := strconv.Atoi(countStr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
f := os.NewFile(uintptr(count), "listen socket")
|
||||
l, err = net.FileListener(f)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return l, nil
|
||||
}
|
||||
}
|
106
router.go
106
router.go
@ -4,12 +4,18 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
sc *Controller = &Controller{}
|
||||
)
|
||||
|
||||
type controllerInfo struct {
|
||||
pattern string
|
||||
regex *regexp.Regexp
|
||||
@ -183,6 +189,85 @@ func (p *ControllerRegistor) FilterPrefixPath(path string, filter http.HandlerFu
|
||||
})
|
||||
}
|
||||
|
||||
func StructMap(vc reflect.Value, r *http.Request) error {
|
||||
for k, t := range r.Form {
|
||||
v := t[0]
|
||||
names := strings.Split(k, ".")
|
||||
var value reflect.Value = vc
|
||||
for i, name := range names {
|
||||
name = strings.Title(name)
|
||||
if i == 0 {
|
||||
if reflect.ValueOf(sc).Elem().FieldByName(name).IsValid() {
|
||||
Trace("Controller's property should not be changed by mapper.")
|
||||
break
|
||||
}
|
||||
}
|
||||
if value.Kind() != reflect.Struct {
|
||||
Trace(fmt.Sprintf("arg error, value kind is %v", value.Kind()))
|
||||
break
|
||||
}
|
||||
|
||||
if i != len(names)-1 {
|
||||
value = value.FieldByName(name)
|
||||
if !value.IsValid() {
|
||||
Trace(fmt.Sprintf("(%v value is not valid %v)", name, value))
|
||||
break
|
||||
}
|
||||
} else {
|
||||
tv := value.FieldByName(name)
|
||||
if !tv.IsValid() {
|
||||
Trace(fmt.Sprintf("struct %v has no field named %v", value, name))
|
||||
break
|
||||
}
|
||||
if !tv.CanSet() {
|
||||
Trace("can not set " + k)
|
||||
break
|
||||
}
|
||||
var l interface{}
|
||||
switch k := tv.Kind(); k {
|
||||
case reflect.String:
|
||||
l = v
|
||||
case reflect.Bool:
|
||||
l = (v == "true")
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32:
|
||||
x, err := strconv.Atoi(v)
|
||||
if err != nil {
|
||||
Trace("arg " + v + " as int: " + err.Error())
|
||||
break
|
||||
}
|
||||
l = x
|
||||
case reflect.Int64:
|
||||
x, err := strconv.ParseInt(v, 10, 64)
|
||||
if err != nil {
|
||||
Trace("arg " + v + " as int: " + err.Error())
|
||||
break
|
||||
}
|
||||
l = x
|
||||
case reflect.Float32, reflect.Float64:
|
||||
x, err := strconv.ParseFloat(v, 64)
|
||||
if err != nil {
|
||||
Trace("arg " + v + " as float64: " + err.Error())
|
||||
break
|
||||
}
|
||||
l = x
|
||||
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||
x, err := strconv.ParseUint(v, 10, 64)
|
||||
if err != nil {
|
||||
Trace("arg " + v + " as int: " + err.Error())
|
||||
break
|
||||
}
|
||||
l = x
|
||||
case reflect.Struct:
|
||||
Trace("can not set an struct")
|
||||
}
|
||||
|
||||
tv.Set(reflect.ValueOf(l))
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AutoRoute
|
||||
func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
||||
defer func() {
|
||||
@ -214,8 +299,10 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
w := &responseWriter{writer: rw}
|
||||
|
||||
w.Header().Set("Server", "beegoServer")
|
||||
var runrouter *controllerInfo
|
||||
var findrouter bool
|
||||
|
||||
@ -231,6 +318,22 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
|
||||
}
|
||||
if strings.HasPrefix(r.URL.Path, prefix) {
|
||||
file := staticDir + r.URL.Path[len(prefix):]
|
||||
finfo, err := os.Stat(file)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
//if the request is dir and DirectoryIndex is false then
|
||||
if finfo.IsDir() && !DirectoryIndex {
|
||||
if h, ok := ErrorMaps["403"]; ok {
|
||||
h(w, r)
|
||||
return
|
||||
} else {
|
||||
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||
w.WriteHeader(403)
|
||||
fmt.Fprintln(w, "403 Forbidden")
|
||||
return
|
||||
}
|
||||
}
|
||||
http.ServeFile(w, r, file)
|
||||
w.started = true
|
||||
return
|
||||
@ -347,10 +450,13 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
|
||||
//Invoke the request handler
|
||||
vc := reflect.New(runrouter.controllerType)
|
||||
|
||||
StructMap(vc.Elem(), r)
|
||||
|
||||
//call the controller init function
|
||||
init := vc.MethodByName("Init")
|
||||
in := make([]reflect.Value, 2)
|
||||
ct := &Context{ResponseWriter: w, Request: r, Params: params}
|
||||
|
||||
in[0] = reflect.ValueOf(ct)
|
||||
in[1] = reflect.ValueOf(runrouter.controllerType.Name())
|
||||
init.Call(in)
|
||||
|
@ -1,60 +1,60 @@
|
||||
sessionmanager
|
||||
session
|
||||
==============
|
||||
|
||||
sessionmanager is a golang session manager. It can use session for many providers.Just like the `database/sql` and `database/sql/driver`.
|
||||
session is a Go session manager. It can use many session providers. Just like the `database/sql` and `database/sql/driver`.
|
||||
|
||||
##How to install
|
||||
## How to install?
|
||||
|
||||
go get github.com/astaxie/beego/session
|
||||
|
||||
|
||||
##how many providers support
|
||||
Now this sessionmanager support memory/file/redis/mysql
|
||||
## What providers are supported?
|
||||
|
||||
As of now this session manager support memory, file, Redis and MySQL.
|
||||
|
||||
|
||||
## How to use it?
|
||||
|
||||
##How do we use it?
|
||||
|
||||
first you must import it
|
||||
|
||||
First you must import it
|
||||
|
||||
import (
|
||||
"github.com/astaxie/beego/session"
|
||||
)
|
||||
|
||||
then in you web app init the globalsession manager
|
||||
Then in you web app init the global session manager
|
||||
|
||||
var globalSessions *session.Manager
|
||||
|
||||
use memory as providers:
|
||||
* Use **memory** as provider:
|
||||
|
||||
func init() {
|
||||
globalSessions, _ = session.NewManager("memory", "gosessionid", 3600,"")
|
||||
go globalSessions.GC()
|
||||
}
|
||||
|
||||
use mysql as providers,the last param is the DNS, learn more from [mysql](https://github.com/Go-SQL-Driver/MySQL#dsn-data-source-name):
|
||||
* Use **file** as provider, the last param is the path where you want file to be stored:
|
||||
|
||||
func init() {
|
||||
globalSessions, _ = session.NewManager("mysql", "gosessionid", 3600,"username:password@protocol(address)/dbname?param=value")
|
||||
globalSessions, _ = session.NewManager("file", "gosessionid", 3600, "./tmp")
|
||||
go globalSessions.GC()
|
||||
}
|
||||
|
||||
use file as providers,the last param is the path where to store the file:
|
||||
* Use **Redis** as provider, the last param is the Redis conn address:
|
||||
|
||||
func init() {
|
||||
globalSessions, _ = session.NewManager("file", "gosessionid", 3600,"./tmp")
|
||||
globalSessions, _ = session.NewManager("redis", "gosessionid", 3600, "127.0.0.1:6379")
|
||||
go globalSessions.GC()
|
||||
}
|
||||
|
||||
use redis as providers,the last param is the redis's conn address:
|
||||
* Use **MySQL** as provider, the last param is the DSN, learn more from [mysql](https://github.com/Go-SQL-Driver/MySQL#dsn-data-source-name):
|
||||
|
||||
func init() {
|
||||
globalSessions, _ = session.NewManager("redis", "gosessionid", 3600,"127.0.0.1:6379")
|
||||
globalSessions, _ = session.NewManager(
|
||||
"mysql", "gosessionid", 3600, "username:password@protocol(address)/dbname?param=value")
|
||||
go globalSessions.GC()
|
||||
}
|
||||
|
||||
at last in the handlerfunc you can use it like this
|
||||
Finally in the handlerfunc you can use it like this
|
||||
|
||||
func login(w http.ResponseWriter, r *http.Request) {
|
||||
sess := globalSessions.SessionStart(w, r)
|
||||
@ -72,17 +72,19 @@ at last in the handlerfunc you can use it like this
|
||||
}
|
||||
|
||||
|
||||
## How to write own provider?
|
||||
|
||||
##How to write own provider
|
||||
When we develop a web app, maybe you want to write a provider because you must meet the requirements.
|
||||
When you develop a web app, maybe you want to write own provider because you must meet the requirements.
|
||||
|
||||
Write a provider is so easy. You only define two struct type(Session and Provider),which satisfy the interface definition.Maybe The memory provider is a good example for you.
|
||||
Writing a provider is easy. You only need to define two struct types
|
||||
(Session and Provider), which satisfy the interface definition.
|
||||
Maybe you will find the **memory** provider as good example.
|
||||
|
||||
type SessionStore interface {
|
||||
Set(key, value interface{}) error //set session value
|
||||
Get(key interface{}) interface{} //get session value
|
||||
Delete(key interface{}) error //delete session value
|
||||
SessionID() string //back current sessionID
|
||||
Set(key, value interface{}) error // set session value
|
||||
Get(key interface{}) interface{} // get session value
|
||||
Delete(key interface{}) error // delete session value
|
||||
SessionID() string // return current sessionID
|
||||
SessionRelease() // release the resource
|
||||
}
|
||||
|
||||
@ -93,6 +95,7 @@ Write a provider is so easy. You only define two struct type(Session and Provide
|
||||
SessionGC()
|
||||
}
|
||||
|
||||
##LICENSE
|
||||
|
||||
## LICENSE
|
||||
|
||||
BSD License http://creativecommons.org/licenses/BSD/
|
@ -32,7 +32,6 @@ func (fs *FileSessionStore) Set(key, value interface{}) error {
|
||||
func (fs *FileSessionStore) Get(key interface{}) interface{} {
|
||||
fs.lock.RLock()
|
||||
defer fs.lock.RUnlock()
|
||||
fs.updatecontent()
|
||||
if v, ok := fs.values[key]; ok {
|
||||
return v
|
||||
} else {
|
||||
@ -58,13 +57,13 @@ func (fs *FileSessionStore) SessionRelease() {
|
||||
}
|
||||
|
||||
func (fs *FileSessionStore) updatecontent() {
|
||||
if len(fs.values) > 0 {
|
||||
b, err := encodeGob(fs.values)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
fs.f.Truncate(0)
|
||||
fs.f.Seek(0, 0)
|
||||
fs.f.Write(b)
|
||||
}
|
||||
}
|
||||
|
||||
type FileProvider struct {
|
||||
@ -107,7 +106,7 @@ func (fp *FileProvider) SessionRead(sid string) (SessionStore, error) {
|
||||
}
|
||||
}
|
||||
f.Close()
|
||||
f, err = os.Create(path.Join(fp.savePath, string(sid[0]), string(sid[1]), sid))
|
||||
f, err = os.OpenFile(path.Join(fp.savePath, string(sid[0]), string(sid[1]), sid), os.O_WRONLY|os.O_CREATE, 0777)
|
||||
ss := &FileSessionStore{f: f, sid: sid, values: kv}
|
||||
return ss, nil
|
||||
}
|
||||
|
@ -6,18 +6,24 @@ import (
|
||||
|
||||
var redispder = &RedisProvider{}
|
||||
|
||||
var MAX_POOL_SIZE = 20
|
||||
|
||||
var redisPool chan redis.Conn
|
||||
|
||||
type RedisSessionStore struct {
|
||||
c redis.Conn
|
||||
sid string
|
||||
}
|
||||
|
||||
func (rs *RedisSessionStore) Set(key, value interface{}) error {
|
||||
//_, err := rs.c.Do("HSET", rs.sid, key, value)
|
||||
_, err := rs.c.Do("HSET", rs.sid, key, value)
|
||||
return err
|
||||
}
|
||||
|
||||
func (rs *RedisSessionStore) Get(key interface{}) interface{} {
|
||||
v, err := rs.c.Do("GET", rs.sid, key)
|
||||
//v, err := rs.c.Do("GET", rs.sid, key)
|
||||
v, err := redis.String(rs.c.Do("HGET", rs.sid, key))
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
@ -25,6 +31,7 @@ func (rs *RedisSessionStore) Get(key interface{}) interface{} {
|
||||
}
|
||||
|
||||
func (rs *RedisSessionStore) Delete(key interface{}) error {
|
||||
//_, err := rs.c.Do("HDEL", rs.sid, key)
|
||||
_, err := rs.c.Do("HDEL", rs.sid, key)
|
||||
return err
|
||||
}
|
||||
@ -43,11 +50,37 @@ type RedisProvider struct {
|
||||
}
|
||||
|
||||
func (rp *RedisProvider) connectInit() redis.Conn {
|
||||
c, err := redis.Dial("tcp", rp.savePath)
|
||||
/*c, err := redis.Dial("tcp", rp.savePath)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return c
|
||||
return c*/
|
||||
//if redisPool == nil {
|
||||
redisPool = make(chan redis.Conn, MAX_POOL_SIZE)
|
||||
//}
|
||||
if len(redisPool) == 0 {
|
||||
go func() {
|
||||
for i := 0; i < MAX_POOL_SIZE/2; i++ {
|
||||
c, err := redis.Dial("tcp", rp.savePath)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
putRedis(c)
|
||||
}
|
||||
}()
|
||||
}
|
||||
return <-redisPool
|
||||
}
|
||||
|
||||
func putRedis(conn redis.Conn) {
|
||||
if redisPool == nil {
|
||||
redisPool = make(chan redis.Conn, MAX_POOL_SIZE)
|
||||
}
|
||||
if len(redisPool) >= MAX_POOL_SIZE {
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
redisPool <- conn
|
||||
}
|
||||
|
||||
func (rp *RedisProvider) SessionInit(maxlifetime int64, savePath string) error {
|
||||
@ -58,8 +91,10 @@ func (rp *RedisProvider) SessionInit(maxlifetime int64, savePath string) error {
|
||||
|
||||
func (rp *RedisProvider) SessionRead(sid string) (SessionStore, error) {
|
||||
c := rp.connectInit()
|
||||
if str, err := redis.String(c.Do("GET", sid)); err != nil || str == "" {
|
||||
c.Do("SET", sid, sid, rp.maxlifetime)
|
||||
//if str, err := redis.String(c.Do("GET", sid)); err != nil || str == "" {
|
||||
if str, err := redis.String(c.Do("HGET", sid, sid)); err != nil || str == "" {
|
||||
//c.Do("SET", sid, sid, rp.maxlifetime)
|
||||
c.Do("HSET", sid, sid, rp.maxlifetime)
|
||||
}
|
||||
rs := &RedisSessionStore{c: c, sid: sid}
|
||||
return rs, nil
|
||||
|
Reference in New Issue
Block a user