Skip to content

Commit

Permalink
Merge pull request beego#2894 from skOak/develop
Browse files Browse the repository at this point in the history
add custom middleware options for beego.Run()
  • Loading branch information
astaxie authored Oct 23, 2017
2 parents b169ea4 + e91afb1 commit 7dc8991
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
10 changes: 9 additions & 1 deletion app.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ func NewApp() *App {
return app
}

type MiddleWare func(http.Handler) http.Handler

// Run beego application.
func (app *App) Run() {
func (app *App) Run(mws ...MiddleWare) {
addr := BConfig.Listen.HTTPAddr

if BConfig.Listen.HTTPPort != 0 {
Expand Down Expand Up @@ -94,6 +96,12 @@ func (app *App) Run() {
}

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.WriteTimeout = time.Duration(BConfig.Listen.ServerTimeOut) * time.Second
app.Server.ErrorLog = logs.GetLogger("HTTP")
Expand Down
14 changes: 14 additions & 0 deletions beego.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ func Run(params ...string) {
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() {
//init hooks
AddAPPStartHook(
Expand Down

0 comments on commit 7dc8991

Please sign in to comment.