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

107 lines
2.2 KiB
Go
Raw Normal View History

2015-09-07 11:18:04 +00:00
package beego
import (
"mime"
"path/filepath"
"strconv"
"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 {
if _, ok := ErrorMaps["401"]; !ok {
2015-09-08 13:41:38 +00:00
ErrorHandler("401", unauthorized)
2015-09-07 11:18:04 +00:00
}
if _, ok := ErrorMaps["402"]; !ok {
2015-09-08 13:41:38 +00:00
ErrorHandler("402", paymentRequired)
2015-09-07 11:18:04 +00:00
}
if _, ok := ErrorMaps["403"]; !ok {
2015-09-08 13:41:38 +00:00
ErrorHandler("403", forbidden)
2015-09-07 11:18:04 +00:00
}
if _, ok := ErrorMaps["404"]; !ok {
2015-09-08 13:41:38 +00:00
ErrorHandler("404", notFound)
2015-09-07 11:18:04 +00:00
}
if _, ok := ErrorMaps["405"]; !ok {
2015-09-08 13:41:38 +00:00
ErrorHandler("405", methodNotAllowed)
2015-09-07 11:18:04 +00:00
}
if _, ok := ErrorMaps["500"]; !ok {
2015-09-08 13:41:38 +00:00
ErrorHandler("500", internalServerError)
2015-09-07 11:18:04 +00:00
}
if _, ok := ErrorMaps["501"]; !ok {
2015-09-08 13:41:38 +00:00
ErrorHandler("501", notImplemented)
2015-09-07 11:18:04 +00:00
}
if _, ok := ErrorMaps["502"]; !ok {
2015-09-08 13:41:38 +00:00
ErrorHandler("502", badGateway)
2015-09-07 11:18:04 +00:00
}
if _, ok := ErrorMaps["503"]; !ok {
2015-09-08 13:41:38 +00:00
ErrorHandler("503", serviceUnavailable)
2015-09-07 11:18:04 +00:00
}
if _, ok := ErrorMaps["504"]; !ok {
2015-09-08 13:41:38 +00:00
ErrorHandler("504", gatewayTimeout)
2015-09-07 11:18:04 +00:00
}
return nil
}
func registerSession() error {
if SessionOn {
var err error
sessionConfig := AppConfig.String("sessionConfig")
if sessionConfig == "" {
sessionConfig = `{"cookieName":"` + SessionName + `",` +
`"gclifetime":` + strconv.FormatInt(SessionGCMaxLifetime, 10) + `,` +
`"providerConfig":"` + filepath.ToSlash(SessionProviderConfig) + `",` +
`"secure":` + strconv.FormatBool(EnableHTTPTLS) + `,` +
2015-09-07 11:18:04 +00:00
`"enableSetCookie":` + strconv.FormatBool(SessionAutoSetCookie) + `,` +
`"domain":"` + SessionDomain + `",` +
`"cookieLifeTime":` + strconv.Itoa(SessionCookieLifeTime) + `}`
}
2015-09-07 13:38:53 +00:00
GlobalSessions, err = session.NewManager(SessionProvider, sessionConfig)
2015-09-07 11:18:04 +00:00
if err != nil {
return err
}
go GlobalSessions.GC()
}
return nil
}
func registerTemplate() error {
if AutoRender {
err := BuildTemplate(ViewsPath)
if err != nil && RunMode == "dev" {
Warn(err)
}
}
return nil
}
func registerDocs() error {
if EnableDocs {
Get("/docs", serverDocs)
Get("/docs/*", serverDocs)
}
return nil
}
2015-09-07 11:27:53 +00:00
func registerAdmin() error {
if EnableAdmin {
go beeAdminApp.Run()
}
return nil
}