1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-10 20:30:54 +00:00
Beego/app.go

105 lines
2.4 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-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() {
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
}
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
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
}