1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-27 07:41:27 +00:00
Beego/server/web/hooks.go

109 lines
2.9 KiB
Go
Raw Normal View History

package web
2020-07-22 14:50:08 +00:00
import (
context2 "context"
2020-07-22 14:50:08 +00:00
"encoding/json"
"mime"
"net/http"
"path/filepath"
"github.com/astaxie/beego/core/logs"
"github.com/astaxie/beego/server/web/context"
"github.com/astaxie/beego/server/web/session"
2020-07-22 14:50:08 +00:00
)
// register MIME type with content type
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 {
m := map[string]func(http.ResponseWriter, *http.Request){
"401": unauthorized,
"402": paymentRequired,
"403": forbidden,
"404": notFound,
"405": methodNotAllowed,
"500": internalServerError,
"501": notImplemented,
"502": badGateway,
"503": serviceUnavailable,
"504": gatewayTimeout,
"417": invalidxsrf,
"422": missingxsrf,
"413": payloadTooLarge,
}
for e, h := range m {
if _, ok := ErrorMaps[e]; !ok {
ErrorHandler(e, h)
}
}
return nil
}
func registerSession() error {
if BConfig.WebConfig.Session.SessionOn {
var err error
sessionConfig, err := AppConfig.String(nil, "sessionConfig")
2020-07-22 14:50:08 +00:00
conf := new(session.ManagerConfig)
2020-08-28 17:17:43 +00:00
if sessionConfig == "" || err != nil {
2020-07-22 14:50:08 +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.DisableHTTPOnly = BConfig.WebConfig.Session.SessionDisableHTTPOnly
conf.Domain = BConfig.WebConfig.Session.SessionDomain
conf.EnableSidInHTTPHeader = BConfig.WebConfig.Session.SessionEnableSidInHTTPHeader
conf.SessionNameInHTTPHeader = BConfig.WebConfig.Session.SessionNameInHTTPHeader
conf.EnableSidInURLQuery = BConfig.WebConfig.Session.SessionEnableSidInURLQuery
} else {
if err = json.Unmarshal([]byte(sessionConfig), conf); err != nil {
return err
}
}
if GlobalSessions, err = session.NewManager(BConfig.WebConfig.Session.SessionProvider, conf); err != nil {
return err
}
go GlobalSessions.GC()
}
return nil
}
func registerTemplate() error {
defer lockViewPaths()
if err := AddViewPath(BConfig.WebConfig.ViewsPath); err != nil {
if BConfig.RunMode == DEV {
logs.Warn(err)
}
return err
}
return nil
}
func registerGzip() error {
if BConfig.EnableGzip {
context.InitGzip(
AppConfig.DefaultInt(context2.Background(), "gzipMinLength", -1),
AppConfig.DefaultInt(context2.Background(), "gzipCompressLevel", -1),
AppConfig.DefaultStrings(context2.Background(), "includedMethods", []string{"GET"}),
2020-07-22 14:50:08 +00:00
)
}
return nil
}
2020-08-06 01:39:12 +00:00
func registerCommentRouter() error {
if BConfig.RunMode == DEV {
if err := parserPkg(filepath.Join(WorkPath, BConfig.WebConfig.CommentRouterPath)); err != nil {
return err
}
}
return nil
2020-08-10 15:04:57 +00:00
}