2015-09-07 11:18:04 +00:00
|
|
|
package beego
|
|
|
|
|
|
|
|
import (
|
2015-12-22 02:02:59 +00:00
|
|
|
"encoding/json"
|
2015-09-07 11:18:04 +00:00
|
|
|
"mime"
|
2015-09-17 02:31:53 +00:00
|
|
|
"net/http"
|
2015-12-22 02:02:59 +00:00
|
|
|
"path/filepath"
|
2015-09-17 02:31:53 +00:00
|
|
|
|
2016-03-17 11:09:38 +00:00
|
|
|
"github.com/astaxie/beego/context"
|
2016-03-24 09:39:29 +00:00
|
|
|
"github.com/astaxie/beego/logs"
|
2015-09-07 11:18:04 +00:00
|
|
|
"github.com/astaxie/beego/session"
|
|
|
|
)
|
|
|
|
|
|
|
|
//
|
|
|
|
func registerMime() error {
|
|
|
|
for k, v := range mimemaps {
|
|
|
|
mime.AddExtensionType(k, v)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// register default error http handlers, 404,401,403,500 and 503.
|
|
|
|
func registerDefaultErrorHandler() error {
|
2015-12-22 02:02:59 +00:00
|
|
|
m := map[string]func(http.ResponseWriter, *http.Request){
|
2015-09-17 02:31:53 +00:00
|
|
|
"401": unauthorized,
|
|
|
|
"402": paymentRequired,
|
|
|
|
"403": forbidden,
|
|
|
|
"404": notFound,
|
|
|
|
"405": methodNotAllowed,
|
|
|
|
"500": internalServerError,
|
|
|
|
"501": notImplemented,
|
|
|
|
"502": badGateway,
|
|
|
|
"503": serviceUnavailable,
|
|
|
|
"504": gatewayTimeout,
|
2015-12-22 02:02:59 +00:00
|
|
|
}
|
|
|
|
for e, h := range m {
|
2015-09-17 02:36:29 +00:00
|
|
|
if _, ok := ErrorMaps[e]; !ok {
|
|
|
|
ErrorHandler(e, h)
|
|
|
|
}
|
2015-09-07 11:18:04 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func registerSession() error {
|
2015-12-09 15:35:04 +00:00
|
|
|
if BConfig.WebConfig.Session.SessionOn {
|
2015-09-07 11:18:04 +00:00
|
|
|
var err error
|
|
|
|
sessionConfig := AppConfig.String("sessionConfig")
|
2016-08-13 13:07:27 +00:00
|
|
|
conf := new(session.ManagerConfig)
|
2015-09-07 11:18:04 +00:00
|
|
|
if sessionConfig == "" {
|
2016-08-13 13:07:27 +00:00
|
|
|
conf.CookieName = BConfig.WebConfig.Session.SessionName
|
|
|
|
conf.EnableSetCookie = BConfig.WebConfig.Session.SessionAutoSetCookie
|
|
|
|
conf.Gclifetime = BConfig.WebConfig.Session.SessionGCMaxLifetime
|
|
|
|
conf.Secure = BConfig.Listen.EnableHTTPS
|
|
|
|
conf.CookieLifeTime = BConfig.WebConfig.Session.SessionCookieLifeTime
|
|
|
|
conf.ProviderConfig = filepath.ToSlash(BConfig.WebConfig.Session.SessionProviderConfig)
|
|
|
|
conf.Domain = BConfig.WebConfig.Session.SessionDomain
|
|
|
|
conf.EnableSidInHttpHeader = BConfig.WebConfig.Session.EnableSidInHttpHeader
|
|
|
|
conf.SessionNameInHttpHeader = BConfig.WebConfig.Session.SessionNameInHttpHeader
|
|
|
|
conf.EnableSidInUrlQuery = BConfig.WebConfig.Session.EnableSidInUrlQuery
|
|
|
|
} else {
|
|
|
|
if err = json.Unmarshal([]byte(sessionConfig), conf); err != nil {
|
2015-12-22 02:02:59 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-09-07 11:18:04 +00:00
|
|
|
}
|
2016-08-13 13:07:27 +00:00
|
|
|
if GlobalSessions, err = session.NewManager(BConfig.WebConfig.Session.SessionProvider, conf); err != nil {
|
2015-09-07 11:18:04 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
go GlobalSessions.GC()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func registerTemplate() error {
|
2016-01-25 13:08:29 +00:00
|
|
|
if err := BuildTemplate(BConfig.WebConfig.ViewsPath); err != nil {
|
|
|
|
if BConfig.RunMode == DEV {
|
2016-03-24 09:39:29 +00:00
|
|
|
logs.Warn(err)
|
2015-09-07 11:18:04 +00:00
|
|
|
}
|
2016-01-25 13:08:29 +00:00
|
|
|
return err
|
2015-09-07 11:18:04 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-07 11:27:53 +00:00
|
|
|
func registerAdmin() error {
|
2016-01-12 13:55:02 +00:00
|
|
|
if BConfig.Listen.EnableAdmin {
|
2015-09-07 11:27:53 +00:00
|
|
|
go beeAdminApp.Run()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2016-03-17 11:09:38 +00:00
|
|
|
|
|
|
|
func registerGzip() error {
|
|
|
|
if BConfig.EnableGzip {
|
2016-03-22 08:42:42 +00:00
|
|
|
context.InitGzip(
|
|
|
|
AppConfig.DefaultInt("gzipMinLength", -1),
|
|
|
|
AppConfig.DefaultInt("gzipCompressLevel", -1),
|
|
|
|
AppConfig.DefaultStrings("includedMethods", []string{"GET"}),
|
|
|
|
)
|
2016-03-17 11:09:38 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|