2014-04-12 05:18:18 +00:00
|
|
|
// Beego (http://beego.me/)
|
|
|
|
// @description beego is an open-source, high-performance web framework for the Go programming language.
|
|
|
|
// @link http://github.com/astaxie/beego for the canonical source repository
|
|
|
|
// @license http://github.com/astaxie/beego/blob/master/LICENSE
|
|
|
|
// @authors astaxie
|
|
|
|
|
2013-09-09 16:00:11 +00:00
|
|
|
package beego
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/http/fcgi"
|
|
|
|
"time"
|
2013-12-03 13:37:39 +00:00
|
|
|
|
|
|
|
"github.com/astaxie/beego/context"
|
2013-09-09 16:00:11 +00:00
|
|
|
)
|
|
|
|
|
2013-12-20 14:35:16 +00:00
|
|
|
// FilterFunc defines filter function type.
|
2013-09-09 16:00:11 +00:00
|
|
|
type FilterFunc func(*context.Context)
|
|
|
|
|
2013-12-20 11:20:13 +00:00
|
|
|
// App defines beego application with a new PatternServeMux.
|
2013-09-09 16:00:11 +00:00
|
|
|
type App struct {
|
|
|
|
Handlers *ControllerRegistor
|
|
|
|
}
|
|
|
|
|
2013-12-20 11:20:13 +00:00
|
|
|
// NewApp returns a new beego application.
|
2013-09-09 16:00:11 +00:00
|
|
|
func NewApp() *App {
|
|
|
|
cr := NewControllerRegistor()
|
|
|
|
app := &App{Handlers: cr}
|
|
|
|
return app
|
|
|
|
}
|
|
|
|
|
2013-12-20 11:20:13 +00:00
|
|
|
// Run beego application.
|
2013-09-09 16:00:11 +00:00
|
|
|
func (app *App) Run() {
|
|
|
|
addr := HttpAddr
|
|
|
|
|
|
|
|
if HttpPort != 0 {
|
|
|
|
addr = fmt.Sprintf("%s:%d", HttpAddr, HttpPort)
|
|
|
|
}
|
2013-11-26 07:30:59 +00:00
|
|
|
|
2013-12-05 03:27:29 +00:00
|
|
|
BeeLogger.Info("Running on %s", addr)
|
2013-11-26 07:30:59 +00:00
|
|
|
|
2013-09-09 16:00:11 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
l net.Listener
|
|
|
|
)
|
2013-11-26 07:30:59 +00:00
|
|
|
|
2013-09-09 16:00:11 +00:00
|
|
|
if UseFcgi {
|
|
|
|
if HttpPort == 0 {
|
|
|
|
l, err = net.Listen("unix", addr)
|
|
|
|
} else {
|
|
|
|
l, err = net.Listen("tcp", addr)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2013-09-09 16:17:49 +00:00
|
|
|
BeeLogger.Critical("Listen: ", err)
|
2013-09-09 16:00:11 +00:00
|
|
|
}
|
|
|
|
err = fcgi.Serve(l, app.Handlers)
|
|
|
|
} else {
|
|
|
|
if EnableHotUpdate {
|
|
|
|
server := &http.Server{
|
|
|
|
Handler: app.Handlers,
|
|
|
|
ReadTimeout: time.Duration(HttpServerTimeOut) * time.Second,
|
|
|
|
WriteTimeout: time.Duration(HttpServerTimeOut) * time.Second,
|
|
|
|
}
|
|
|
|
laddr, err := net.ResolveTCPAddr("tcp", addr)
|
|
|
|
if nil != err {
|
2013-09-09 16:17:49 +00:00
|
|
|
BeeLogger.Critical("ResolveTCPAddr:", err)
|
2013-09-09 16:00:11 +00:00
|
|
|
}
|
2013-12-21 05:19:24 +00:00
|
|
|
l, err = GetInitListener(laddr)
|
2013-09-09 16:00:11 +00:00
|
|
|
theStoppable = newStoppable(l)
|
|
|
|
err = server.Serve(theStoppable)
|
|
|
|
theStoppable.wg.Wait()
|
|
|
|
CloseSelf()
|
|
|
|
} else {
|
|
|
|
s := &http.Server{
|
|
|
|
Addr: addr,
|
|
|
|
Handler: app.Handlers,
|
|
|
|
ReadTimeout: time.Duration(HttpServerTimeOut) * time.Second,
|
|
|
|
WriteTimeout: time.Duration(HttpServerTimeOut) * time.Second,
|
|
|
|
}
|
|
|
|
if HttpTLS {
|
|
|
|
err = s.ListenAndServeTLS(HttpCertFile, HttpKeyFile)
|
|
|
|
} else {
|
|
|
|
err = s.ListenAndServe()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-11-26 07:30:59 +00:00
|
|
|
|
2013-09-09 16:00:11 +00:00
|
|
|
if err != nil {
|
2013-09-09 16:17:49 +00:00
|
|
|
BeeLogger.Critical("ListenAndServe: ", err)
|
2013-11-13 13:09:47 +00:00
|
|
|
time.Sleep(100 * time.Microsecond)
|
2013-09-09 16:00:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-20 11:20:13 +00:00
|
|
|
// Router adds a url-patterned controller handler.
|
|
|
|
// The path argument supports regex rules and specific placeholders.
|
|
|
|
// The c argument needs a controller handler implemented beego.ControllerInterface.
|
|
|
|
// The mapping methods argument only need one string to define custom router rules.
|
|
|
|
// usage:
|
2013-12-21 05:19:24 +00:00
|
|
|
// simple router
|
|
|
|
// beego.Router("/admin", &admin.UserController{})
|
|
|
|
// beego.Router("/admin/index", &admin.ArticleController{})
|
2013-12-20 11:20:13 +00:00
|
|
|
//
|
2013-12-21 05:19:24 +00:00
|
|
|
// regex router
|
2013-12-20 11:20:13 +00:00
|
|
|
//
|
2013-12-21 05:19:24 +00:00
|
|
|
// beego.Router(“/api/:id([0-9]+)“, &controllers.RController{})
|
2013-12-20 11:20:13 +00:00
|
|
|
//
|
|
|
|
// custom rules
|
2013-12-21 05:19:24 +00:00
|
|
|
// beego.Router("/api/list",&RestController{},"*:ListFood")
|
|
|
|
// beego.Router("/api/create",&RestController{},"post:CreateFood")
|
|
|
|
// beego.Router("/api/update",&RestController{},"put:UpdateFood")
|
|
|
|
// beego.Router("/api/delete",&RestController{},"delete:DeleteFood")
|
2013-09-09 16:00:11 +00:00
|
|
|
func (app *App) Router(path string, c ControllerInterface, mappingMethods ...string) *App {
|
|
|
|
app.Handlers.Add(path, c, mappingMethods...)
|
|
|
|
return app
|
|
|
|
}
|
|
|
|
|
2013-12-20 11:20:13 +00:00
|
|
|
// AutoRouter adds beego-defined controller handler.
|
|
|
|
// if beego.AddAuto(&MainContorlller{}) and MainController has methods List and Page,
|
|
|
|
// visit the url /main/list to exec List function or /main/page to exec Page function.
|
2013-09-09 16:00:11 +00:00
|
|
|
func (app *App) AutoRouter(c ControllerInterface) *App {
|
|
|
|
app.Handlers.AddAuto(c)
|
|
|
|
return app
|
|
|
|
}
|
|
|
|
|
2014-01-01 09:57:57 +00:00
|
|
|
// AutoRouterWithPrefix adds beego-defined controller handler with prefix.
|
|
|
|
// if beego.AutoPrefix("/admin",&MainContorlller{}) and MainController has methods List and Page,
|
|
|
|
// visit the url /admin/main/list to exec List function or /admin/main/page to exec Page function.
|
|
|
|
func (app *App) AutoRouterWithPrefix(prefix string, c ControllerInterface) *App {
|
|
|
|
app.Handlers.AddAutoPrefix(prefix, c)
|
|
|
|
return app
|
|
|
|
}
|
|
|
|
|
beego: support more router
//design model
beego.Get(router, beego.FilterFunc)
beego.Post(router, beego.FilterFunc)
beego.Put(router, beego.FilterFunc)
beego.Head(router, beego.FilterFunc)
beego.Options(router, beego.FilterFunc)
beego.Delete(router, beego.FilterFunc)
beego.Handler(router, http.Handler)
//example
beego.Get("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("Get userlist"))
})
beego.Post("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("add userlist"))
})
beego.Delete("/user/:id", func(ctx *context.Context) {
ctx.Output.Body([]byte([]byte(ctx.Input.Param(":id")))
})
import (
"http"
"github.com/gorilla/rpc"
"github.com/gorilla/rpc/json"
)
func init() {
s := rpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
s.RegisterService(new(HelloService), "")
beego.Handler("/rpc", s)
}
2014-05-16 02:18:15 +00:00
|
|
|
// add router for Get method
|
|
|
|
func (app *App) Get(rootpath string, f FilterFunc) *App {
|
|
|
|
app.Handlers.Get(rootpath, f)
|
|
|
|
return app
|
|
|
|
}
|
|
|
|
|
|
|
|
// add router for Post method
|
|
|
|
func (app *App) Post(rootpath string, f FilterFunc) *App {
|
|
|
|
app.Handlers.Post(rootpath, f)
|
|
|
|
return app
|
|
|
|
}
|
|
|
|
|
|
|
|
// add router for Put method
|
|
|
|
func (app *App) Put(rootpath string, f FilterFunc) *App {
|
|
|
|
app.Handlers.Put(rootpath, f)
|
|
|
|
return app
|
|
|
|
}
|
|
|
|
|
|
|
|
// add router for Delete method
|
|
|
|
func (app *App) Delete(rootpath string, f FilterFunc) *App {
|
|
|
|
app.Handlers.Delete(rootpath, f)
|
|
|
|
return app
|
|
|
|
}
|
|
|
|
|
|
|
|
// add router for Options method
|
|
|
|
func (app *App) Options(rootpath string, f FilterFunc) *App {
|
|
|
|
app.Handlers.Options(rootpath, f)
|
|
|
|
return app
|
|
|
|
}
|
|
|
|
|
|
|
|
// add router for Head method
|
|
|
|
func (app *App) Head(rootpath string, f FilterFunc) *App {
|
|
|
|
app.Handlers.Head(rootpath, f)
|
|
|
|
return app
|
|
|
|
}
|
|
|
|
|
|
|
|
// add router for Patch method
|
|
|
|
func (app *App) Patch(rootpath string, f FilterFunc) *App {
|
|
|
|
app.Handlers.Patch(rootpath, f)
|
|
|
|
return app
|
|
|
|
}
|
|
|
|
|
|
|
|
// add router for Patch method
|
|
|
|
func (app *App) Any(rootpath string, f FilterFunc) *App {
|
|
|
|
app.Handlers.Any(rootpath, f)
|
|
|
|
return app
|
|
|
|
}
|
|
|
|
|
|
|
|
// add router for http.Handler
|
|
|
|
func (app *App) Handler(rootpath string, h http.Handler) *App {
|
|
|
|
app.Handlers.Handler(rootpath, h)
|
|
|
|
return app
|
|
|
|
}
|
|
|
|
|
2013-12-21 05:19:24 +00:00
|
|
|
// UrlFor creates a url with another registered controller handler with params.
|
2013-12-20 11:20:13 +00:00
|
|
|
// The endpoint is formed as path.controller.name to defined the controller method which will run.
|
|
|
|
// The values need key-pair data to assign into controller method.
|
2013-11-10 15:05:07 +00:00
|
|
|
func (app *App) UrlFor(endpoint string, values ...string) string {
|
|
|
|
return app.Handlers.UrlFor(endpoint, values...)
|
|
|
|
}
|
2013-12-20 11:20:13 +00:00
|
|
|
|
2013-12-20 11:36:54 +00:00
|
|
|
// [Deprecated] use InsertFilter.
|
2013-12-20 11:20:13 +00:00
|
|
|
// Filter adds a FilterFunc under pattern condition and named action.
|
|
|
|
// The actions contains BeforeRouter,AfterStatic,BeforeExec,AfterExec and FinishRouter.
|
2013-09-09 16:00:11 +00:00
|
|
|
func (app *App) Filter(pattern, action string, filter FilterFunc) *App {
|
|
|
|
app.Handlers.AddFilter(pattern, action, filter)
|
|
|
|
return app
|
|
|
|
}
|
|
|
|
|
2013-12-20 11:20:13 +00:00
|
|
|
// InsertFilter adds a FilterFunc with pattern condition and action constant.
|
|
|
|
// The pos means action constant including
|
|
|
|
// beego.BeforeRouter, beego.AfterStatic, beego.BeforeExec, beego.AfterExec and beego.FinishRouter.
|
2013-11-25 07:59:40 +00:00
|
|
|
func (app *App) InsertFilter(pattern string, pos int, filter FilterFunc) *App {
|
|
|
|
app.Handlers.InsertFilter(pattern, pos, filter)
|
|
|
|
return app
|
|
|
|
}
|
|
|
|
|
2013-12-20 11:20:13 +00:00
|
|
|
// SetViewsPath sets view directory path in beego application.
|
|
|
|
// it returns beego application self.
|
2013-09-09 16:00:11 +00:00
|
|
|
func (app *App) SetViewsPath(path string) *App {
|
|
|
|
ViewsPath = path
|
|
|
|
return app
|
|
|
|
}
|
|
|
|
|
2013-12-20 11:20:13 +00:00
|
|
|
// SetStaticPath sets static directory path and proper url pattern in beego application.
|
|
|
|
// if beego.SetStaticPath("static","public"), visit /static/* to load static file in folder "public".
|
|
|
|
// it returns beego application self.
|
2013-09-09 16:00:11 +00:00
|
|
|
func (app *App) SetStaticPath(url string, path string) *App {
|
|
|
|
StaticDir[url] = path
|
|
|
|
return app
|
|
|
|
}
|
|
|
|
|
2013-12-20 11:20:13 +00:00
|
|
|
// DelStaticPath removes the static folder setting in this url pattern in beego application.
|
|
|
|
// it returns beego application self.
|
2013-09-09 16:00:11 +00:00
|
|
|
func (app *App) DelStaticPath(url string) *App {
|
|
|
|
delete(StaticDir, url)
|
|
|
|
return app
|
|
|
|
}
|