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

105 lines
2.7 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"
)
// register MIME type with content type
2015-09-07 11:18:04 +00:00
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,
"417": invalidxsrf,
"422": missingxsrf,
2020-07-08 10:09:01 +00:00
"413": payloadTooLarge,
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)
2016-10-11 15:49:19 +00:00
conf.DisableHTTPOnly = BConfig.WebConfig.Session.SessionDisableHTTPOnly
2016-08-13 13:07:27 +00:00
conf.Domain = BConfig.WebConfig.Session.SessionDomain
2017-04-30 14:41:23 +00:00
conf.EnableSidInHTTPHeader = BConfig.WebConfig.Session.SessionEnableSidInHTTPHeader
conf.SessionNameInHTTPHeader = BConfig.WebConfig.Session.SessionNameInHTTPHeader
conf.EnableSidInURLQuery = BConfig.WebConfig.Session.SessionEnableSidInURLQuery
2016-08-13 13:07:27 +00:00
} 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 {
defer lockViewPaths()
2017-02-07 16:28:03 +00:00
if err := AddViewPath(BConfig.WebConfig.ViewsPath); err != nil {
2016-01-25 13:08:29 +00:00
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
}