Beego/app.go

126 lines
3.1 KiB
Go
Raw Normal View History

2014-08-18 08:41:43 +00:00
// Copyright 2014 beego Author. All Rights Reserved.
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2013-09-09 16:00:11 +00:00
package beego
import (
"fmt"
"net"
"net/http"
"net/http/fcgi"
"time"
)
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-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 {
2014-10-13 11:47:44 +00:00
if UseStdIo {
err = fcgi.Serve(nil, app.Handlers) // standard I/O
if err == nil {
BeeLogger.Info("Use FCGI via standard I/O")
} else {
BeeLogger.Info("Cannot use FCGI via standard I/O", err)
}
2013-09-09 16:00:11 +00:00
} else {
2014-10-13 11:47:44 +00:00
if HttpPort == 0 {
l, err = net.Listen("unix", addr)
} else {
l, err = net.Listen("tcp", addr)
}
if err != nil {
BeeLogger.Critical("Listen: ", err)
}
err = fcgi.Serve(l, app.Handlers)
2013-09-09 16:00:11 +00:00
}
} 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() {
2014-08-01 09:03:28 +00:00
time.Sleep(20 * time.Microsecond)
2014-05-20 08:41:39 +00:00
if HttpsPort != 0 {
app.Server.Addr = fmt.Sprintf("%s:%d", HttpAddr, HttpsPort)
2014-05-13 15:19:50 +00:00
}
BeeLogger.Info("https server Running on %s", app.Server.Addr)
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() {
2014-08-01 09:03:28 +00:00
app.Server.Addr = addr
BeeLogger.Info("http server Running on %s", app.Server.Addr)
if ListenTCP4 && HttpAddr == "" {
ln, err := net.Listen("tcp4", app.Server.Addr)
if err != nil {
BeeLogger.Critical("ListenAndServe: ", err)
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
}
2014-05-20 08:41:39 +00:00
}
}()
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
}