1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-14 06:30:55 +00:00
Beego/hooks.go

89 lines
2.1 KiB
Go
Raw Normal View History

2015-09-07 11:18:04 +00:00
package beego
import (
"mime"
"path/filepath"
"strconv"
2015-09-17 02:31:53 +00:00
"net/http"
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-09-17 02:31:53 +00:00
for e, h := range 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,
} {
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-09 15:35:04 +00:00
sessionConfig = `{"cookieName":"` + BConfig.WebConfig.Session.SessionName + `",` +
`"gclifetime":` + strconv.FormatInt(BConfig.WebConfig.Session.SessionGCMaxLifetime, 10) + `,` +
`"providerConfig":"` + filepath.ToSlash(BConfig.WebConfig.Session.SessionProviderConfig) + `",` +
`"secure":` + strconv.FormatBool(BConfig.Listen.HTTPSEnable) + `,` +
`"enableSetCookie":` + strconv.FormatBool(BConfig.WebConfig.Session.SessionAutoSetCookie) + `,` +
`"domain":"` + BConfig.WebConfig.Session.SessionDomain + `",` +
`"cookieLifeTime":` + strconv.Itoa(BConfig.WebConfig.Session.SessionCookieLifeTime) + `}`
2015-09-07 11:18:04 +00:00
}
2015-12-09 15:35:04 +00:00
GlobalSessions, err = session.NewManager(BConfig.WebConfig.Session.SessionProvider, sessionConfig)
2015-09-07 11:18:04 +00:00
if err != nil {
return err
}
go GlobalSessions.GC()
}
return nil
}
func registerTemplate() error {
2015-12-09 15:35:04 +00:00
if BConfig.WebConfig.AutoRender {
err := BuildTemplate(BConfig.WebConfig.ViewsPath)
if err != nil && BConfig.RunMode == "dev" {
2015-09-07 11:18:04 +00:00
Warn(err)
}
}
return nil
}
func registerDocs() error {
2015-12-09 15:35:04 +00:00
if BConfig.WebConfig.EnableDocs {
2015-09-07 11:18:04 +00:00
Get("/docs", serverDocs)
Get("/docs/*", serverDocs)
}
return nil
}
2015-09-07 11:27:53 +00:00
func registerAdmin() error {
2015-12-09 15:35:04 +00:00
if BConfig.Listen.AdminEnable {
2015-09-07 11:27:53 +00:00
go beeAdminApp.Run()
}
return nil
}