mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 10:10:54 +00:00
simplify filter.go hooks.go
This commit is contained in:
parent
2ddda59605
commit
cd514803a4
@ -33,12 +33,11 @@ type FilterRouter struct {
|
||||
// If the request is matched, the values of the URL parameters defined
|
||||
// by the filter pattern are also returned.
|
||||
func (f *FilterRouter) ValidRouter(url string, ctx *context.Context) bool {
|
||||
isok := f.tree.Match(url, ctx)
|
||||
if isok == nil {
|
||||
return false
|
||||
isOk := f.tree.Match(url, ctx)
|
||||
if isOk != nil {
|
||||
if b, ok := isOk.(bool); ok {
|
||||
return b
|
||||
}
|
||||
if isok, ok := isok.(bool); ok {
|
||||
return isok
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
39
hooks.go
39
hooks.go
@ -1,11 +1,10 @@
|
||||
package beego
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"mime"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/astaxie/beego/session"
|
||||
)
|
||||
@ -20,8 +19,7 @@ func registerMime() error {
|
||||
|
||||
// register default error http handlers, 404,401,403,500 and 503.
|
||||
func registerDefaultErrorHandler() error {
|
||||
|
||||
for e, h := range map[string]func(http.ResponseWriter, *http.Request){
|
||||
m := map[string]func(http.ResponseWriter, *http.Request){
|
||||
"401": unauthorized,
|
||||
"402": paymentRequired,
|
||||
"403": forbidden,
|
||||
@ -32,7 +30,8 @@ func registerDefaultErrorHandler() error {
|
||||
"502": badGateway,
|
||||
"503": serviceUnavailable,
|
||||
"504": gatewayTimeout,
|
||||
} {
|
||||
}
|
||||
for e, h := range m {
|
||||
if _, ok := ErrorMaps[e]; !ok {
|
||||
ErrorHandler(e, h)
|
||||
}
|
||||
@ -45,18 +44,24 @@ func registerSession() error {
|
||||
var err error
|
||||
sessionConfig := AppConfig.String("sessionConfig")
|
||||
if sessionConfig == "" {
|
||||
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) + `}`
|
||||
conf := map[string]interface{}{
|
||||
"cookieName": BConfig.WebConfig.Session.SessionName,
|
||||
"gclifetime": BConfig.WebConfig.Session.SessionGCMaxLifetime,
|
||||
"providerConfig": filepath.ToSlash(BConfig.WebConfig.Session.SessionProviderConfig),
|
||||
"secure": BConfig.Listen.HTTPSEnable,
|
||||
"enableSetCookie": BConfig.WebConfig.Session.SessionAutoSetCookie,
|
||||
"domain": BConfig.WebConfig.Session.SessionDomain,
|
||||
"cookieLifeTime": BConfig.WebConfig.Session.SessionCookieLifeTime,
|
||||
}
|
||||
GlobalSessions, err = session.NewManager(BConfig.WebConfig.Session.SessionProvider, sessionConfig)
|
||||
confBytes, err := json.Marshal(conf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sessionConfig = string(confBytes)
|
||||
}
|
||||
if GlobalSessions, err = session.NewManager(BConfig.WebConfig.Session.SessionProvider, sessionConfig); err != nil {
|
||||
return err
|
||||
}
|
||||
go GlobalSessions.GC()
|
||||
}
|
||||
return nil
|
||||
@ -64,10 +69,12 @@ func registerSession() error {
|
||||
|
||||
func registerTemplate() error {
|
||||
if BConfig.WebConfig.AutoRender {
|
||||
err := BuildTemplate(BConfig.WebConfig.ViewsPath)
|
||||
if err != nil && BConfig.RunMode == "dev" {
|
||||
if err := BuildTemplate(BConfig.WebConfig.ViewsPath); err != nil {
|
||||
if BConfig.RunMode == "dev" {
|
||||
Warn(err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user