mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 07:50:54 +00:00
modify beego
This commit is contained in:
parent
625dec0a16
commit
4263723691
254
beego.go
254
beego.go
@ -1,127 +1,127 @@
|
|||||||
package beego
|
package beego
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
BeeApp *App
|
BeeApp *App
|
||||||
AppName string
|
AppName string
|
||||||
AppPath string
|
AppPath string
|
||||||
StaticDir map[string]string
|
StaticDir map[string]string
|
||||||
HttpAddr string
|
HttpAddr string
|
||||||
HttpPort int
|
HttpPort int
|
||||||
RecoverPanic bool
|
RecoverPanic bool
|
||||||
AutoRender bool
|
AutoRender bool
|
||||||
ViewsPath string
|
ViewsPath string
|
||||||
RunMode string //"dev" or "prod"
|
RunMode string //"dev" or "prod"
|
||||||
AppConfig *Config
|
AppConfig *Config
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
BeeApp = NewApp()
|
BeeApp = NewApp()
|
||||||
AppPath, _ = os.Getwd()
|
AppPath, _ = os.Getwd()
|
||||||
StaticDir = make(map[string]string)
|
StaticDir = make(map[string]string)
|
||||||
var err error
|
var err error
|
||||||
AppConfig, err = LoadConfig(path.Join(AppPath, "conf", "app.conf"))
|
AppConfig, err = LoadConfig(path.Join(AppPath, "conf", "app.conf"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
//Trace("open Config err:", err)
|
//Trace("open Config err:", err)
|
||||||
HttpAddr = ""
|
HttpAddr = ""
|
||||||
HttpPort = 8080
|
HttpPort = 8080
|
||||||
AppName = "beego"
|
AppName = "beego"
|
||||||
RunMode = "prod"
|
RunMode = "prod" //default runmod
|
||||||
AutoRender = true
|
AutoRender = true
|
||||||
RecoverPanic = true
|
RecoverPanic = true
|
||||||
ViewsPath = "views"
|
ViewsPath = "views"
|
||||||
} else {
|
} else {
|
||||||
HttpAddr = AppConfig.String("httpaddr")
|
HttpAddr = AppConfig.String("httpaddr")
|
||||||
if v, err := AppConfig.Int("httpport"); err != nil {
|
if v, err := AppConfig.Int("httpport"); err != nil {
|
||||||
HttpPort = 8080
|
HttpPort = 8080
|
||||||
} else {
|
} else {
|
||||||
HttpPort = v
|
HttpPort = v
|
||||||
}
|
}
|
||||||
AppName = AppConfig.String("appname")
|
AppName = AppConfig.String("appname")
|
||||||
if runmode := AppConfig.String("runmode"); runmode != "" {
|
if runmode := AppConfig.String("runmode"); runmode != "" {
|
||||||
RunMode = runmode
|
RunMode = runmode
|
||||||
} else {
|
} else {
|
||||||
RunMode = "prod"
|
RunMode = "prod"
|
||||||
}
|
}
|
||||||
if ar, err := AppConfig.Bool("autorender"); err != nil {
|
if ar, err := AppConfig.Bool("autorender"); err != nil {
|
||||||
AutoRender = true
|
AutoRender = true
|
||||||
} else {
|
} else {
|
||||||
AutoRender = ar
|
AutoRender = ar
|
||||||
}
|
}
|
||||||
if ar, err := AppConfig.Bool("autorecover"); err != nil {
|
if ar, err := AppConfig.Bool("autorecover"); err != nil {
|
||||||
RecoverPanic = true
|
RecoverPanic = true
|
||||||
} else {
|
} else {
|
||||||
RecoverPanic = ar
|
RecoverPanic = ar
|
||||||
}
|
}
|
||||||
if views := AppConfig.String("viewspath"); views == "" {
|
if views := AppConfig.String("viewspath"); views == "" {
|
||||||
ViewsPath = "views"
|
ViewsPath = "views"
|
||||||
} else {
|
} else {
|
||||||
ViewsPath = views
|
ViewsPath = views
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
StaticDir["/static"] = "static"
|
StaticDir["/static"] = "static"
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type App struct {
|
type App struct {
|
||||||
Handlers *ControllerRegistor
|
Handlers *ControllerRegistor
|
||||||
}
|
}
|
||||||
|
|
||||||
// New returns a new PatternServeMux.
|
// New returns a new PatternServeMux.
|
||||||
func NewApp() *App {
|
func NewApp() *App {
|
||||||
cr := NewControllerRegistor()
|
cr := NewControllerRegistor()
|
||||||
app := &App{Handlers: cr}
|
app := &App{Handlers: cr}
|
||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *App) Run() {
|
func (app *App) Run() {
|
||||||
addr := fmt.Sprintf("%s:%d", HttpAddr, HttpPort)
|
addr := fmt.Sprintf("%s:%d", HttpAddr, HttpPort)
|
||||||
err := http.ListenAndServe(addr, app.Handlers)
|
err := http.ListenAndServe(addr, app.Handlers)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
BeeLogger.Fatal("ListenAndServe: ", err)
|
BeeLogger.Fatal("ListenAndServe: ", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *App) RegisterController(path string, c ControllerInterface) *App {
|
func (app *App) RegisterController(path string, c ControllerInterface) *App {
|
||||||
app.Handlers.Add(path, c)
|
app.Handlers.Add(path, c)
|
||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *App) Filter(filter http.HandlerFunc) *App {
|
func (app *App) Filter(filter http.HandlerFunc) *App {
|
||||||
app.Handlers.Filter(filter)
|
app.Handlers.Filter(filter)
|
||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *App) FilterParam(param string, filter http.HandlerFunc) *App {
|
func (app *App) FilterParam(param string, filter http.HandlerFunc) *App {
|
||||||
app.Handlers.FilterParam(param, filter)
|
app.Handlers.FilterParam(param, filter)
|
||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *App) FilterPrefixPath(path string, filter http.HandlerFunc) *App {
|
func (app *App) FilterPrefixPath(path string, filter http.HandlerFunc) *App {
|
||||||
app.Handlers.FilterParam(path, filter)
|
app.Handlers.FilterParam(path, filter)
|
||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *App) SetViewsPath(path string) *App {
|
func (app *App) SetViewsPath(path string) *App {
|
||||||
ViewsPath = path
|
ViewsPath = path
|
||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *App) SetStaticPath(url string, path string) *App {
|
func (app *App) SetStaticPath(url string, path string) *App {
|
||||||
StaticDir[url] = path
|
StaticDir[url] = path
|
||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *App) ErrorLog(ctx *Context) {
|
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)
|
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) {
|
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)
|
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)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user