1
0
mirror of https://github.com/astaxie/beego.git synced 2024-12-23 10:10:49 +00:00
Beego/beego.go

305 lines
7.4 KiB
Go
Raw Normal View History

2012-12-17 05:53:03 +00:00
package beego
import (
"fmt"
"github.com/astaxie/beego/session"
2013-01-06 02:15:08 +00:00
"html/template"
2013-01-10 12:22:00 +00:00
"net"
2012-12-17 05:53:03 +00:00
"net/http"
2013-01-10 12:22:00 +00:00
"net/http/fcgi"
2012-12-17 05:53:03 +00:00
"os"
"path"
2013-04-11 03:17:18 +00:00
"runtime"
2013-07-03 07:29:54 +00:00
"time"
2012-12-17 05:53:03 +00:00
)
2013-08-14 02:53:11 +00:00
const VERSION = "0.9.0"
2013-03-13 16:14:09 +00:00
2012-12-17 05:53:03 +00:00
var (
2013-01-06 02:15:08 +00:00
BeeApp *App
AppName string
AppPath string
2013-05-05 15:03:59 +00:00
AppConfigPath string
2013-01-06 02:15:08 +00:00
StaticDir map[string]string
TemplateCache map[string]*template.Template
HttpAddr string
HttpPort int
RecoverPanic bool
AutoRender bool
PprofOn bool
ViewsPath string
RunMode string //"dev" or "prod"
AppConfig *Config
2013-06-07 09:48:04 +00:00
//related to session
2013-05-05 15:03:59 +00:00
GlobalSessions *session.Manager //GlobalSessions
SessionOn bool // whether auto start session,default is false
2013-06-07 09:48:04 +00:00
SessionProvider string // default session provider memory mysql redis
2013-05-05 15:03:59 +00:00
SessionName string // sessionName cookie's name
SessionGCMaxLifetime int64 // session's gc maxlifetime
SessionSavePath string // session savepath if use mysql/redis/file this set to the connectinfo
2013-01-10 12:22:00 +00:00
UseFcgi bool
MaxMemory int64
2013-07-25 07:40:33 +00:00
EnableGzip bool // enable gzip
DirectoryIndex bool //enable DirectoryIndex default is false
EnableHotUpdate bool //enable HotUpdate default is false
2013-07-25 07:40:33 +00:00
HttpServerTimeOut int64 //set httpserver timeout
ErrorsShow bool //set weather show errors
XSRFKEY string //set XSRF
2013-08-06 15:21:52 +00:00
EnableXSRF bool
2013-08-07 03:22:23 +00:00
XSRFExpire int
2013-08-06 15:21:52 +00:00
CopyRequestBody bool //When in raw application, You want to the reqeustbody
2012-12-17 05:53:03 +00:00
)
func init() {
os.Chdir(path.Dir(os.Args[0]))
2012-12-17 05:53:03 +00:00
BeeApp = NewApp()
AppPath, _ = os.Getwd()
StaticDir = make(map[string]string)
2013-01-06 02:15:08 +00:00
TemplateCache = make(map[string]*template.Template)
2013-05-05 15:03:59 +00:00
HttpAddr = ""
HttpPort = 8080
AppName = "beego"
RunMode = "dev" //default runmod
AutoRender = true
RecoverPanic = true
PprofOn = false
ViewsPath = "views"
SessionOn = false
SessionProvider = "memory"
SessionName = "beegosessionID"
SessionGCMaxLifetime = 3600
SessionSavePath = ""
UseFcgi = false
MaxMemory = 1 << 26 //64MB
EnableGzip = false
2012-12-17 05:53:03 +00:00
StaticDir["/static"] = "static"
2013-05-05 15:03:59 +00:00
AppConfigPath = path.Join(AppPath, "conf", "app.conf")
2013-07-03 07:29:54 +00:00
HttpServerTimeOut = 0
2013-07-07 09:58:50 +00:00
ErrorsShow = true
2013-07-08 08:17:08 +00:00
XSRFKEY = "beegoxsrf"
2013-08-07 03:22:23 +00:00
XSRFExpire = 60
2013-05-07 15:06:51 +00:00
ParseConfig()
runtime.GOMAXPROCS(runtime.NumCPU())
2012-12-17 05:53:03 +00:00
}
type App struct {
Handlers *ControllerRegistor
}
// New returns a new PatternServeMux.
func NewApp() *App {
cr := NewControllerRegistor()
app := &App{Handlers: cr}
return app
}
func (app *App) Run() {
addr := fmt.Sprintf("%s:%d", HttpAddr, HttpPort)
2013-06-07 09:48:04 +00:00
var (
err error
l net.Listener
)
2013-01-10 12:22:00 +00:00
if UseFcgi {
2013-06-07 09:48:04 +00:00
l, err = net.Listen("tcp", addr)
if err != nil {
BeeLogger.Fatal("Listen: ", err)
2013-01-10 12:22:00 +00:00
}
err = fcgi.Serve(l, app.Handlers)
} else {
if EnableHotUpdate {
2013-07-03 08:58:15 +00:00
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 {
BeeLogger.Fatal("ResolveTCPAddr:", err)
}
l, err = GetInitListner(laddr)
theStoppable = newStoppable(l)
err = server.Serve(theStoppable)
theStoppable.wg.Wait()
CloseSelf()
} else {
2013-07-03 07:29:54 +00:00
s := &http.Server{
Addr: addr,
Handler: app.Handlers,
ReadTimeout: time.Duration(HttpServerTimeOut) * time.Second,
WriteTimeout: time.Duration(HttpServerTimeOut) * time.Second,
}
err = s.ListenAndServe()
2013-06-07 09:48:04 +00:00
}
2013-01-10 12:22:00 +00:00
}
2012-12-17 05:53:03 +00:00
if err != nil {
BeeLogger.Fatal("ListenAndServe: ", err)
}
}
2013-07-25 07:50:16 +00:00
func (app *App) Router(path string, c ControllerInterface, mappingMethods ...string) *App {
app.Handlers.Add(path, c, mappingMethods...)
2012-12-17 05:53:03 +00:00
return app
}
2013-07-27 02:25:14 +00:00
func (app *App) AutoRouter(c ControllerInterface) *App {
app.Handlers.AddAuto(c)
return app
}
2012-12-17 05:53:03 +00:00
func (app *App) Filter(filter http.HandlerFunc) *App {
app.Handlers.Filter(filter)
return app
}
func (app *App) FilterParam(param string, filter http.HandlerFunc) *App {
app.Handlers.FilterParam(param, filter)
return app
}
func (app *App) FilterPrefixPath(path string, filter http.HandlerFunc) *App {
app.Handlers.FilterPrefixPath(path, filter)
2012-12-17 05:53:03 +00:00
return app
}
2013-08-11 16:14:42 +00:00
func (app *App) FilterAfter(filter http.HandlerFunc) *App {
app.Handlers.FilterAfter(filter)
return app
}
func (app *App) FilterParamAfter(param string, filter http.HandlerFunc) *App {
app.Handlers.FilterParamAfter(param, filter)
return app
}
func (app *App) FilterPrefixPathAfter(path string, filter http.HandlerFunc) *App {
app.Handlers.FilterPrefixPathAfter(path, filter)
return app
}
2012-12-17 05:53:03 +00:00
func (app *App) SetViewsPath(path string) *App {
ViewsPath = path
return app
}
func (app *App) SetStaticPath(url string, path string) *App {
StaticDir[url] = path
return app
}
func (app *App) DelStaticPath(url string) *App {
delete(StaticDir, url)
return app
}
2012-12-17 05:53:03 +00:00
func (app *App) ErrorLog(ctx *Context) {
BeeLogger.Printf("[ERR] host: '%s', request: '%s %s', proto: '%s', ua: '%s', remote: '%s'\n", ctx.Request.Host, ctx.Request.Method, ctx.Request.URL.Path, ctx.Request.Proto, ctx.Request.UserAgent(), ctx.Request.RemoteAddr)
}
func (app *App) AccessLog(ctx *Context) {
2013-04-16 13:35:55 +00:00
BeeLogger.Printf("[ACC] host: '%s', request: '%s %s', proto: '%s', ua: '%s', remote: '%s'\n", ctx.Request.Host, ctx.Request.Method, ctx.Request.URL.Path, ctx.Request.Proto, ctx.Request.UserAgent(), ctx.Request.RemoteAddr)
2012-12-17 05:53:03 +00:00
}
2012-12-17 14:15:21 +00:00
func RegisterController(path string, c ControllerInterface) *App {
BeeApp.Router(path, c)
return BeeApp
}
2013-07-25 07:50:16 +00:00
func Router(rootpath string, c ControllerInterface, mappingMethods ...string) *App {
BeeApp.Router(rootpath, c, mappingMethods...)
2013-07-08 10:35:10 +00:00
return BeeApp
}
func RESTRouter(rootpath string, c ControllerInterface) *App {
Router(rootpath, c)
Router(path.Join(rootpath, ":objectId"), c)
2012-12-17 14:15:21 +00:00
return BeeApp
}
2013-07-27 02:25:14 +00:00
func AutoRouter(c ControllerInterface) *App {
BeeApp.AutoRouter(c)
return BeeApp
}
2013-04-07 16:28:32 +00:00
func RouterHandler(path string, c http.Handler) *App {
BeeApp.Handlers.AddHandler(path, c)
return BeeApp
}
2013-05-06 16:17:25 +00:00
func Errorhandler(err string, h http.HandlerFunc) *App {
ErrorMaps[err] = h
return BeeApp
}
2013-04-14 15:20:38 +00:00
func SetViewsPath(path string) *App {
BeeApp.SetViewsPath(path)
return BeeApp
}
func SetStaticPath(url string, path string) *App {
StaticDir[url] = path
return BeeApp
}
func DelStaticPath(url string) *App {
delete(StaticDir, url)
return BeeApp
}
2012-12-17 14:15:21 +00:00
func Filter(filter http.HandlerFunc) *App {
BeeApp.Filter(filter)
return BeeApp
}
func FilterParam(param string, filter http.HandlerFunc) *App {
BeeApp.FilterParam(param, filter)
return BeeApp
}
func FilterPrefixPath(path string, filter http.HandlerFunc) *App {
BeeApp.FilterPrefixPath(path, filter)
2012-12-17 14:15:21 +00:00
return BeeApp
}
2013-08-11 16:14:42 +00:00
func FilterAfter(filter http.HandlerFunc) *App {
BeeApp.FilterAfter(filter)
return BeeApp
}
func FilterParamAfter(param string, filter http.HandlerFunc) *App {
BeeApp.FilterParamAfter(param, filter)
return BeeApp
}
func FilterPrefixPathAfter(path string, filter http.HandlerFunc) *App {
BeeApp.FilterPrefixPathAfter(path, filter)
return BeeApp
}
2012-12-17 14:15:21 +00:00
func Run() {
2013-05-07 15:16:56 +00:00
if AppConfigPath != path.Join(AppPath, "conf", "app.conf") {
err := ParseConfig()
if err != nil {
if RunMode == "dev" {
Warn(err)
}
2013-05-05 15:03:59 +00:00
}
}
2012-12-27 14:27:26 +00:00
if PprofOn {
BeeApp.Router(`/debug/pprof`, &ProfController{})
BeeApp.Router(`/debug/pprof/:pp([\w]+)`, &ProfController{})
2012-12-27 14:27:26 +00:00
}
2013-01-01 15:11:15 +00:00
if SessionOn {
GlobalSessions, _ = session.NewManager(SessionProvider, SessionName, SessionGCMaxLifetime, SessionSavePath)
2013-01-01 15:11:15 +00:00
go GlobalSessions.GC()
}
2013-05-07 15:16:56 +00:00
err := BuildTemplate(ViewsPath)
2013-03-21 13:55:54 +00:00
if err != nil {
if RunMode == "dev" {
Warn(err)
}
2013-03-21 13:55:54 +00:00
}
2013-05-06 16:17:25 +00:00
registerErrorHander()
2012-12-17 14:15:21 +00:00
BeeApp.Run()
}