1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-05 05:00:54 +00:00
Beego/hooks.go

99 lines
2.3 KiB
Go
Raw Normal View History

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"
"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")
if sessionConfig == "" {
2015-12-22 02:02:59 +00:00
conf := map[string]interface{}{
"cookieName": BConfig.WebConfig.Session.SessionName,
"gclifetime": BConfig.WebConfig.Session.SessionGCMaxLifetime,
"providerConfig": filepath.ToSlash(BConfig.WebConfig.Session.SessionProviderConfig),
2016-01-12 13:55:02 +00:00
"secure": BConfig.Listen.EnableHTTPS,
2015-12-22 02:02:59 +00:00
"enableSetCookie": BConfig.WebConfig.Session.SessionAutoSetCookie,
"domain": BConfig.WebConfig.Session.SessionDomain,
"cookieLifeTime": BConfig.WebConfig.Session.SessionCookieLifeTime,
}
confBytes, err := json.Marshal(conf)
if err != nil {
return err
}
sessionConfig = string(confBytes)
2015-09-07 11:18:04 +00:00
}
2015-12-22 02:02:59 +00:00
if GlobalSessions, err = session.NewManager(BConfig.WebConfig.Session.SessionProvider, sessionConfig); 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 {
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 {
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
}