1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-13 06:20:55 +00:00
Beego/app.go

95 lines
2.1 KiB
Go
Raw Normal View History

2014-04-12 05:18:18 +00:00
// Beego (http://beego.me/)
// @description beego is an open-source, high-performance web framework for the Go programming language.
// @link http://github.com/astaxie/beego for the canonical source repository
// @license http://github.com/astaxie/beego/blob/master/LICENSE
// @authors astaxie
2013-09-09 16:00:11 +00:00
package beego
import (
"fmt"
"net"
"net/http"
"net/http/fcgi"
"time"
2013-12-03 13:37:39 +00:00
"github.com/astaxie/beego/context"
2013-09-09 16:00:11 +00:00
)
2013-12-20 14:35:16 +00:00
// FilterFunc defines filter function type.
2013-09-09 16:00:11 +00:00
type FilterFunc func(*context.Context)
2013-12-20 11:20:13 +00:00
// App defines beego application with a new PatternServeMux.
2013-09-09 16:00:11 +00:00
type App struct {
Handlers *ControllerRegistor
Server *http.Server
2013-09-09 16:00:11 +00:00
}
2013-12-20 11:20:13 +00:00
// NewApp returns a new beego application.
2013-09-09 16:00:11 +00:00
func NewApp() *App {
2014-06-10 12:12:57 +00:00
cr := NewControllerRegister()
app := &App{Handlers: cr, Server: &http.Server{}}
2013-09-09 16:00:11 +00:00
return app
}
2013-12-20 11:20:13 +00:00
// Run beego application.
2013-09-09 16:00:11 +00:00
func (app *App) Run() {
addr := HttpAddr
if HttpPort != 0 {
addr = fmt.Sprintf("%s:%d", HttpAddr, HttpPort)
}
2013-12-05 03:27:29 +00:00
BeeLogger.Info("Running on %s", addr)
2013-09-09 16:00:11 +00:00
var (
err error
l net.Listener
)
2014-05-20 08:41:39 +00:00
endRunning := make(chan bool, 1)
2013-09-09 16:00:11 +00:00
if UseFcgi {
if HttpPort == 0 {
l, err = net.Listen("unix", addr)
} else {
l, err = net.Listen("tcp", addr)
}
if err != nil {
2013-09-09 16:17:49 +00:00
BeeLogger.Critical("Listen: ", err)
2013-09-09 16:00:11 +00:00
}
err = fcgi.Serve(l, app.Handlers)
} else {
app.Server.Addr = addr
app.Server.Handler = app.Handlers
app.Server.ReadTimeout = time.Duration(HttpServerTimeOut) * time.Second
app.Server.WriteTimeout = time.Duration(HttpServerTimeOut) * time.Second
2014-05-20 08:41:39 +00:00
if EnableHttpTLS {
go func() {
if HttpsPort != 0 {
app.Server.Addr = fmt.Sprintf("%s:%d", HttpAddr, HttpsPort)
2014-05-13 15:19:50 +00:00
}
err := app.Server.ListenAndServeTLS(HttpCertFile, HttpKeyFile)
2014-05-20 08:41:39 +00:00
if err != nil {
BeeLogger.Critical("ListenAndServeTLS: ", err)
time.Sleep(100 * time.Microsecond)
endRunning <- true
}
}()
}
2014-05-20 07:30:17 +00:00
2014-05-20 08:41:39 +00:00
if EnableHttpListen {
go func() {
err := app.Server.ListenAndServe()
2014-05-20 08:41:39 +00:00
if err != nil {
BeeLogger.Critical("ListenAndServe: ", err)
time.Sleep(100 * time.Microsecond)
endRunning <- true
}
}()
2013-09-09 16:00:11 +00:00
}
}
2014-05-20 07:30:17 +00:00
<-endRunning
2013-09-09 16:00:11 +00:00
}