1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 08:20:55 +00:00
add config ListenTCP4

when user want to listen on the TCP4, because now almost use the ipv4.
but default lister on the ipv6
This commit is contained in:
astaxie 2014-11-03 15:06:25 +08:00
parent 90cff5f042
commit 716962672f
3 changed files with 34 additions and 12 deletions

36
app.go
View File

@ -20,13 +20,8 @@ import (
"net/http" "net/http"
"net/http/fcgi" "net/http/fcgi"
"time" "time"
"github.com/astaxie/beego/context"
) )
// FilterFunc defines filter function type.
type FilterFunc func(*context.Context)
// App defines beego application with a new PatternServeMux. // App defines beego application with a new PatternServeMux.
type App struct { type App struct {
Handlers *ControllerRegistor Handlers *ControllerRegistor
@ -85,7 +80,7 @@ func (app *App) Run() {
if HttpsPort != 0 { if HttpsPort != 0 {
app.Server.Addr = fmt.Sprintf("%s:%d", HttpAddr, HttpsPort) app.Server.Addr = fmt.Sprintf("%s:%d", HttpAddr, HttpsPort)
} }
BeeLogger.Info("Running on %s", app.Server.Addr) BeeLogger.Info("https server Running on %s", app.Server.Addr)
err := app.Server.ListenAndServeTLS(HttpCertFile, HttpKeyFile) err := app.Server.ListenAndServeTLS(HttpCertFile, HttpKeyFile)
if err != nil { if err != nil {
BeeLogger.Critical("ListenAndServeTLS: ", err) BeeLogger.Critical("ListenAndServeTLS: ", err)
@ -98,12 +93,29 @@ func (app *App) Run() {
if EnableHttpListen { if EnableHttpListen {
go func() { go func() {
app.Server.Addr = addr app.Server.Addr = addr
BeeLogger.Info("Running on %s", app.Server.Addr) BeeLogger.Info("http server Running on %s", app.Server.Addr)
err := app.Server.ListenAndServe() if ListenTCP4 && HttpAddr == "" {
if err != nil { ln, err := net.Listen("tcp4", app.Server.Addr)
BeeLogger.Critical("ListenAndServe: ", err) if err != nil {
time.Sleep(100 * time.Microsecond) BeeLogger.Critical("ListenAndServe: ", err)
endRunning <- true time.Sleep(100 * time.Microsecond)
endRunning <- true
return
}
err = app.Server.Serve(ln)
if err != nil {
BeeLogger.Critical("ListenAndServe: ", err)
time.Sleep(100 * time.Microsecond)
endRunning <- true
return
}
} else {
err := app.Server.ListenAndServe()
if err != nil {
BeeLogger.Critical("ListenAndServe: ", err)
time.Sleep(100 * time.Microsecond)
endRunning <- true
}
} }
}() }()
} }

View File

@ -40,6 +40,7 @@ var (
EnableHttpListen bool EnableHttpListen bool
HttpAddr string HttpAddr string
HttpPort int HttpPort int
ListenTCP4 bool
EnableHttpTLS bool EnableHttpTLS bool
HttpsPort int HttpsPort int
HttpCertFile string HttpCertFile string
@ -309,6 +310,10 @@ func ParseConfig() (err error) {
HttpPort = v HttpPort = v
} }
if v, err := AppConfig.Bool("ListenTCP4"); err == nil {
ListenTCP4 = v
}
if v, err := AppConfig.Bool("EnableHttpListen"); err == nil { if v, err := AppConfig.Bool("EnableHttpListen"); err == nil {
EnableHttpListen = v EnableHttpListen = v
} }

View File

@ -14,6 +14,11 @@
package beego package beego
import "github.com/astaxie/beego/context"
// FilterFunc defines filter function type.
type FilterFunc func(*context.Context)
// FilterRouter defines filter operation before controller handler execution. // FilterRouter defines filter operation before controller handler execution.
// it can match patterned url and do filter function when action arrives. // it can match patterned url and do filter function when action arrives.
type FilterRouter struct { type FilterRouter struct {