1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-10 20:30:54 +00:00
Beego/beego.go

226 lines
5.4 KiB
Go
Raw Normal View History

2012-12-17 05:53:03 +00:00
package beego
import (
"fmt"
2013-01-01 15:11:15 +00:00
"github.com/astaxie/session"
_ "github.com/astaxie/session/providers/memory"
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-01-01 15:11:15 +00:00
"strconv"
2012-12-17 05:53:03 +00:00
)
2013-03-13 16:14:09 +00:00
const VERSION = "0.0.3"
2012-12-17 05:53:03 +00:00
var (
2013-01-06 02:15:08 +00:00
BeeApp *App
AppName string
AppPath string
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-01-01 15:11:15 +00:00
//related to session
SessionOn bool // wheather auto start session,default is false
SessionProvider string // default session provider memory
SessionName string // sessionName cookie's name
SessionGCMaxLifetime int64 // session's gc maxlifetime
2013-01-10 12:22:00 +00:00
UseFcgi bool
2013-01-01 15:11:15 +00:00
GlobalSessions *session.Manager //GlobalSessions
2012-12-17 05:53:03 +00:00
)
func init() {
BeeApp = NewApp()
AppPath, _ = os.Getwd()
StaticDir = make(map[string]string)
2013-01-06 02:15:08 +00:00
TemplateCache = make(map[string]*template.Template)
2012-12-17 05:53:03 +00:00
var err error
AppConfig, err = LoadConfig(path.Join(AppPath, "conf", "app.conf"))
if err != nil {
//Trace("open Config err:", err)
HttpAddr = ""
HttpPort = 8080
AppName = "beego"
RunMode = "prod" //default runmod
AutoRender = true
RecoverPanic = true
2012-12-27 14:27:26 +00:00
PprofOn = false
2012-12-17 05:53:03 +00:00
ViewsPath = "views"
2013-01-01 15:11:15 +00:00
SessionOn = false
SessionProvider = "memory"
SessionName = "beegosessionID"
SessionGCMaxLifetime = 3600
2013-01-10 12:22:00 +00:00
UseFcgi = false
2012-12-17 05:53:03 +00:00
} else {
HttpAddr = AppConfig.String("httpaddr")
if v, err := AppConfig.Int("httpport"); err != nil {
HttpPort = 8080
} else {
HttpPort = v
}
AppName = AppConfig.String("appname")
if runmode := AppConfig.String("runmode"); runmode != "" {
RunMode = runmode
} else {
RunMode = "prod"
}
if ar, err := AppConfig.Bool("autorender"); err != nil {
AutoRender = true
} else {
AutoRender = ar
}
if ar, err := AppConfig.Bool("autorecover"); err != nil {
RecoverPanic = true
} else {
RecoverPanic = ar
}
2012-12-27 14:27:26 +00:00
if ar, err := AppConfig.Bool("pprofon"); err != nil {
PprofOn = false
} else {
PprofOn = ar
}
2012-12-17 05:53:03 +00:00
if views := AppConfig.String("viewspath"); views == "" {
ViewsPath = "views"
} else {
ViewsPath = views
}
2013-01-01 15:11:15 +00:00
if ar, err := AppConfig.Bool("sessionon"); err != nil {
SessionOn = false
} else {
SessionOn = ar
}
if ar := AppConfig.String("sessionprovider"); ar == "" {
SessionProvider = "memory"
} else {
SessionProvider = ar
}
if ar := AppConfig.String("sessionname"); ar == "" {
SessionName = "beegosessionID"
} else {
SessionName = ar
}
2013-01-01 16:06:44 +00:00
if ar, err := AppConfig.Int("sessiongcmaxlifetime"); err != nil && ar != 0 {
2013-01-01 15:11:15 +00:00
int64val, _ := strconv.ParseInt(strconv.Itoa(ar), 10, 64)
SessionGCMaxLifetime = int64val
} else {
SessionGCMaxLifetime = 3600
}
2013-01-10 12:22:00 +00:00
if ar, err := AppConfig.Bool("usefcgi"); err != nil {
UseFcgi = false
} else {
UseFcgi = ar
}
2012-12-17 05:53:03 +00:00
}
StaticDir["/static"] = "static"
}
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-01-10 12:22:00 +00:00
var err error
if UseFcgi {
l, e := net.Listen("tcp", addr)
if e != nil {
BeeLogger.Fatal("Listen: ", e)
}
err = fcgi.Serve(l, app.Handlers)
} else {
err = http.ListenAndServe(addr, app.Handlers)
}
2012-12-17 05:53:03 +00:00
if err != nil {
BeeLogger.Fatal("ListenAndServe: ", err)
}
}
func (app *App) RegisterController(path string, c ControllerInterface) *App {
app.Handlers.Add(path, c)
return app
}
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
}
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) 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) {
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 14:15:21 +00:00
func RegisterController(path string, c ControllerInterface) *App {
BeeApp.RegisterController(path, c)
return BeeApp
}
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
}
func Run() {
2012-12-27 14:27:26 +00:00
if PprofOn {
BeeApp.RegisterController(`/debug/pprof`, &ProfController{})
2012-12-27 14:42:16 +00:00
BeeApp.RegisterController(`/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)
go GlobalSessions.GC()
}
2013-03-13 16:14:09 +00:00
BuildTemplate(ViewsPath)
2012-12-17 14:15:21 +00:00
BeeApp.Run()
}