mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 13:00:54 +00:00
add custom middleware options for beego.Run()
This commit is contained in:
parent
520753415f
commit
e91afb1938
10
app.go
10
app.go
@ -51,8 +51,10 @@ func NewApp() *App {
|
|||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type MiddleWare func(http.Handler) http.Handler
|
||||||
|
|
||||||
// Run beego application.
|
// Run beego application.
|
||||||
func (app *App) Run() {
|
func (app *App) Run(mws ...MiddleWare) {
|
||||||
addr := BConfig.Listen.HTTPAddr
|
addr := BConfig.Listen.HTTPAddr
|
||||||
|
|
||||||
if BConfig.Listen.HTTPPort != 0 {
|
if BConfig.Listen.HTTPPort != 0 {
|
||||||
@ -94,6 +96,12 @@ func (app *App) Run() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
app.Server.Handler = app.Handlers
|
app.Server.Handler = app.Handlers
|
||||||
|
for i:=len(mws)-1;i>=0;i-- {
|
||||||
|
if mws[i] == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
app.Server.Handler = mws[i](app.Server.Handler)
|
||||||
|
}
|
||||||
app.Server.ReadTimeout = time.Duration(BConfig.Listen.ServerTimeOut) * time.Second
|
app.Server.ReadTimeout = time.Duration(BConfig.Listen.ServerTimeOut) * time.Second
|
||||||
app.Server.WriteTimeout = time.Duration(BConfig.Listen.ServerTimeOut) * time.Second
|
app.Server.WriteTimeout = time.Duration(BConfig.Listen.ServerTimeOut) * time.Second
|
||||||
app.Server.ErrorLog = logs.GetLogger("HTTP")
|
app.Server.ErrorLog = logs.GetLogger("HTTP")
|
||||||
|
14
beego.go
14
beego.go
@ -67,6 +67,20 @@ func Run(params ...string) {
|
|||||||
BeeApp.Run()
|
BeeApp.Run()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func RunWithMiddleWares(addr string, mws ...MiddleWare) {
|
||||||
|
initBeforeHTTPRun()
|
||||||
|
|
||||||
|
strs := strings.Split(addr, ":")
|
||||||
|
if len(strs) > 0 && strs[0] != "" {
|
||||||
|
BConfig.Listen.HTTPAddr = strs[0]
|
||||||
|
}
|
||||||
|
if len(strs) > 1 && strs[1] != "" {
|
||||||
|
BConfig.Listen.HTTPPort, _ = strconv.Atoi(strs[1])
|
||||||
|
}
|
||||||
|
|
||||||
|
BeeApp.Run(mws...)
|
||||||
|
}
|
||||||
|
|
||||||
func initBeforeHTTPRun() {
|
func initBeforeHTTPRun() {
|
||||||
//init hooks
|
//init hooks
|
||||||
AddAPPStartHook(
|
AddAPPStartHook(
|
||||||
|
Loading…
Reference in New Issue
Block a user