1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 16:10:55 +00:00

simplify filter.go hooks.go

This commit is contained in:
fud 2015-12-22 10:02:59 +08:00
parent 2ddda59605
commit cd514803a4
2 changed files with 30 additions and 24 deletions

View File

@ -33,12 +33,11 @@ type FilterRouter struct {
// If the request is matched, the values of the URL parameters defined // If the request is matched, the values of the URL parameters defined
// by the filter pattern are also returned. // by the filter pattern are also returned.
func (f *FilterRouter) ValidRouter(url string, ctx *context.Context) bool { func (f *FilterRouter) ValidRouter(url string, ctx *context.Context) bool {
isok := f.tree.Match(url, ctx) isOk := f.tree.Match(url, ctx)
if isok == nil { if isOk != nil {
return false if b, ok := isOk.(bool); ok {
return b
} }
if isok, ok := isok.(bool); ok {
return isok
} }
return false return false
} }

View File

@ -1,11 +1,10 @@
package beego package beego
import ( import (
"encoding/json"
"mime" "mime"
"path/filepath"
"strconv"
"net/http" "net/http"
"path/filepath"
"github.com/astaxie/beego/session" "github.com/astaxie/beego/session"
) )
@ -20,8 +19,7 @@ func registerMime() error {
// register default error http handlers, 404,401,403,500 and 503. // register default error http handlers, 404,401,403,500 and 503.
func registerDefaultErrorHandler() error { func registerDefaultErrorHandler() error {
m := map[string]func(http.ResponseWriter, *http.Request){
for e, h := range map[string]func(http.ResponseWriter, *http.Request){
"401": unauthorized, "401": unauthorized,
"402": paymentRequired, "402": paymentRequired,
"403": forbidden, "403": forbidden,
@ -32,7 +30,8 @@ func registerDefaultErrorHandler() error {
"502": badGateway, "502": badGateway,
"503": serviceUnavailable, "503": serviceUnavailable,
"504": gatewayTimeout, "504": gatewayTimeout,
} { }
for e, h := range m {
if _, ok := ErrorMaps[e]; !ok { if _, ok := ErrorMaps[e]; !ok {
ErrorHandler(e, h) ErrorHandler(e, h)
} }
@ -45,18 +44,24 @@ func registerSession() error {
var err error var err error
sessionConfig := AppConfig.String("sessionConfig") sessionConfig := AppConfig.String("sessionConfig")
if sessionConfig == "" { if sessionConfig == "" {
sessionConfig = `{"cookieName":"` + BConfig.WebConfig.Session.SessionName + `",` + conf := map[string]interface{}{
`"gclifetime":` + strconv.FormatInt(BConfig.WebConfig.Session.SessionGCMaxLifetime, 10) + `,` + "cookieName": BConfig.WebConfig.Session.SessionName,
`"providerConfig":"` + filepath.ToSlash(BConfig.WebConfig.Session.SessionProviderConfig) + `",` + "gclifetime": BConfig.WebConfig.Session.SessionGCMaxLifetime,
`"secure":` + strconv.FormatBool(BConfig.Listen.HTTPSEnable) + `,` + "providerConfig": filepath.ToSlash(BConfig.WebConfig.Session.SessionProviderConfig),
`"enableSetCookie":` + strconv.FormatBool(BConfig.WebConfig.Session.SessionAutoSetCookie) + `,` + "secure": BConfig.Listen.HTTPSEnable,
`"domain":"` + BConfig.WebConfig.Session.SessionDomain + `",` + "enableSetCookie": BConfig.WebConfig.Session.SessionAutoSetCookie,
`"cookieLifeTime":` + strconv.Itoa(BConfig.WebConfig.Session.SessionCookieLifeTime) + `}` "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 { if err != nil {
return err return err
} }
sessionConfig = string(confBytes)
}
if GlobalSessions, err = session.NewManager(BConfig.WebConfig.Session.SessionProvider, sessionConfig); err != nil {
return err
}
go GlobalSessions.GC() go GlobalSessions.GC()
} }
return nil return nil
@ -64,10 +69,12 @@ func registerSession() error {
func registerTemplate() error { func registerTemplate() error {
if BConfig.WebConfig.AutoRender { if BConfig.WebConfig.AutoRender {
err := BuildTemplate(BConfig.WebConfig.ViewsPath) if err := BuildTemplate(BConfig.WebConfig.ViewsPath); err != nil {
if err != nil && BConfig.RunMode == "dev" { if BConfig.RunMode == "dev" {
Warn(err) Warn(err)
} }
return err
}
} }
return nil return nil
} }