diff --git a/docs.go b/docs.go
index 0e0f13f7..72532876 100644
--- a/docs.go
+++ b/docs.go
@@ -15,8 +15,6 @@
package beego
import (
- "encoding/json"
-
"github.com/astaxie/beego/context"
)
@@ -33,14 +31,8 @@ func serverDocs(ctx *context.Context) {
}
}
if obj != nil {
- bt, err := json.Marshal(obj)
- if err != nil {
- ctx.Output.SetStatus(504)
- return
- }
- ctx.Output.Header("Content-Type", "application/json;charset=UTF-8")
ctx.Output.Header("Access-Control-Allow-Origin", "*")
- ctx.Output.Body(bt)
+ ctx.Output.JSON(obj, false, false)
return
}
ctx.Output.SetStatus(404)
diff --git a/error.go b/error.go
index 0010c814..af57b7c7 100644
--- a/error.go
+++ b/error.go
@@ -82,16 +82,17 @@ var tpl = `
`
// render default application error page with error and stack string.
-func showErr(err interface{}, ctx *context.Context, Stack string) {
+func showErr(err interface{}, ctx *context.Context, stack string) {
t, _ := template.New("beegoerrortemp").Parse(tpl)
- data := make(map[string]string)
- data["AppError"] = BConfig.AppName + ":" + fmt.Sprint(err)
- data["RequestMethod"] = ctx.Input.Method()
- data["RequestURL"] = ctx.Input.URI()
- data["RemoteAddr"] = ctx.Input.IP()
- data["Stack"] = Stack
- data["BeegoVersion"] = VERSION
- data["GoVersion"] = runtime.Version()
+ data := map[string]string{
+ "AppError": fmt.Sprintf("%s:%v", BConfig.AppName, err),
+ "RequestMethod": ctx.Input.Method(),
+ "RequestURL": ctx.Input.URI(),
+ "RemoteAddr": ctx.Input.IP(),
+ "Stack": stack,
+ "BeegoVersion": VERSION,
+ "GoVersion": runtime.Version(),
+ }
ctx.ResponseWriter.WriteHeader(500)
t.Execute(ctx.ResponseWriter, data)
}
@@ -210,38 +211,42 @@ var ErrorMaps = make(map[string]*errorInfo, 10)
// show 401 unauthorized error.
func unauthorized(rw http.ResponseWriter, r *http.Request) {
t, _ := template.New("beegoerrortemp").Parse(errtpl)
- data := make(map[string]interface{})
- data["Title"] = "Unauthorized"
+ data := map[string]interface{}{
+ "Title": http.StatusText(401),
+ "BeegoVersion": VERSION,
+ }
data["Content"] = template.HTML("
The page you have requested can't be authorized." +
"
Perhaps you are here because:" +
"
" +
"
The credentials you supplied are incorrect" +
"
There are errors in the website address" +
"
")
- data["BeegoVersion"] = VERSION
t.Execute(rw, data)
}
// show 402 Payment Required
func paymentRequired(rw http.ResponseWriter, r *http.Request) {
t, _ := template.New("beegoerrortemp").Parse(errtpl)
- data := make(map[string]interface{})
- data["Title"] = "Payment Required"
+ data := map[string]interface{}{
+ "Title": http.StatusText(402),
+ "BeegoVersion": VERSION,
+ }
data["Content"] = template.HTML("
The page you have requested Payment Required." +
"
Perhaps you are here because:" +
"
" +
"
The credentials you supplied are incorrect" +
"
There are errors in the website address" +
"
")
- data["BeegoVersion"] = VERSION
t.Execute(rw, data)
}
// show 403 forbidden error.
func forbidden(rw http.ResponseWriter, r *http.Request) {
t, _ := template.New("beegoerrortemp").Parse(errtpl)
- data := make(map[string]interface{})
- data["Title"] = "Forbidden"
+ data := map[string]interface{}{
+ "Title": http.StatusText(403),
+ "BeegoVersion": VERSION,
+ }
data["Content"] = template.HTML("
The page you have requested is forbidden." +
"
Perhaps you are here because:" +
"
" +
@@ -249,15 +254,16 @@ func forbidden(rw http.ResponseWriter, r *http.Request) {
"
The site may be disabled" +
"
You need to log in" +
"
")
- data["BeegoVersion"] = VERSION
t.Execute(rw, data)
}
// show 404 notfound error.
func notFound(rw http.ResponseWriter, r *http.Request) {
t, _ := template.New("beegoerrortemp").Parse(errtpl)
- data := make(map[string]interface{})
- data["Title"] = "Page Not Found"
+ data := map[string]interface{}{
+ "Title": http.StatusText(404),
+ "BeegoVersion": VERSION,
+ }
data["Content"] = template.HTML("
The page you have requested has flown the coop." +
"
Perhaps you are here because:" +
"
" +
@@ -266,92 +272,97 @@ func notFound(rw http.ResponseWriter, r *http.Request) {
"
You were looking for your puppy and got lost" +
"
You like 404 pages" +
"
")
- data["BeegoVersion"] = VERSION
t.Execute(rw, data)
}
// show 405 Method Not Allowed
func methodNotAllowed(rw http.ResponseWriter, r *http.Request) {
t, _ := template.New("beegoerrortemp").Parse(errtpl)
- data := make(map[string]interface{})
- data["Title"] = "Method Not Allowed"
+ data := map[string]interface{}{
+ "Title": http.StatusText(405),
+ "BeegoVersion": VERSION,
+ }
data["Content"] = template.HTML("
The method you have requested Not Allowed." +
"
Perhaps you are here because:" +
"
" +
"
The method specified in the Request-Line is not allowed for the resource identified by the Request-URI" +
"
The response MUST include an Allow header containing a list of valid methods for the requested resource." +
"
")
- data["BeegoVersion"] = VERSION
t.Execute(rw, data)
}
// show 500 internal server error.
func internalServerError(rw http.ResponseWriter, r *http.Request) {
t, _ := template.New("beegoerrortemp").Parse(errtpl)
- data := make(map[string]interface{})
- data["Title"] = "Internal Server Error"
+ data := map[string]interface{}{
+ "Title": http.StatusText(500),
+ "BeegoVersion": VERSION,
+ }
data["Content"] = template.HTML("
The page you have requested is down right now." +
"
" +
"
Please try again later and report the error to the website administrator" +
"
")
- data["BeegoVersion"] = VERSION
t.Execute(rw, data)
}
// show 501 Not Implemented.
func notImplemented(rw http.ResponseWriter, r *http.Request) {
t, _ := template.New("beegoerrortemp").Parse(errtpl)
- data := make(map[string]interface{})
- data["Title"] = "Not Implemented"
+ data := map[string]interface{}{
+ "Title": http.StatusText(504),
+ "BeegoVersion": VERSION,
+ }
data["Content"] = template.HTML("
The page you have requested is Not Implemented." +
"
" +
"
Please try again later and report the error to the website administrator" +
"
")
- data["BeegoVersion"] = VERSION
t.Execute(rw, data)
}
// show 502 Bad Gateway.
func badGateway(rw http.ResponseWriter, r *http.Request) {
t, _ := template.New("beegoerrortemp").Parse(errtpl)
- data := make(map[string]interface{})
- data["Title"] = "Bad Gateway"
+ data := map[string]interface{}{
+ "Title": http.StatusText(502),
+ "BeegoVersion": VERSION,
+ }
data["Content"] = template.HTML("
The page you have requested is down right now." +
"
" +
"
The server, while acting as a gateway or proxy, received an invalid response from the upstream server it accessed in attempting to fulfill the request." +
"
Please try again later and report the error to the website administrator" +
"
")
- data["BeegoVersion"] = VERSION
t.Execute(rw, data)
}
// show 503 service unavailable error.
func serviceUnavailable(rw http.ResponseWriter, r *http.Request) {
t, _ := template.New("beegoerrortemp").Parse(errtpl)
- data := make(map[string]interface{})
- data["Title"] = "Service Unavailable"
+ data := map[string]interface{}{
+ "Title": http.StatusText(503),
+ "BeegoVersion": VERSION,
+ }
data["Content"] = template.HTML("
The page you have requested is unavailable." +
"
Perhaps you are here because:" +
"
" +
"
The page is overloaded" +
"
Please try again later." +
"
")
- data["BeegoVersion"] = VERSION
t.Execute(rw, data)
}
// show 504 Gateway Timeout.
func gatewayTimeout(rw http.ResponseWriter, r *http.Request) {
t, _ := template.New("beegoerrortemp").Parse(errtpl)
- data := make(map[string]interface{})
- data["Title"] = "Gateway Timeout"
+ data := map[string]interface{}{
+ "Title": http.StatusText(504),
+ "BeegoVersion": VERSION,
+ }
data["Content"] = template.HTML("
The page you have requested is unavailable." +
"
Perhaps you are here because:" +
"
" +
"
The server, while acting as a gateway or proxy, did not receive a timely response from the upstream server specified by the URI." +
"
Please try again later." +
"
")
- data["BeegoVersion"] = VERSION
t.Execute(rw, data)
}
@@ -360,11 +371,11 @@ func gatewayTimeout(rw http.ResponseWriter, r *http.Request) {
// beego.ErrorHandler("404",NotFound)
// beego.ErrorHandler("500",InternalServerError)
func ErrorHandler(code string, h http.HandlerFunc) *App {
- errinfo := &errorInfo{}
- errinfo.errorType = errorTypeHandler
- errinfo.handler = h
- errinfo.method = code
- ErrorMaps[code] = errinfo
+ ErrorMaps[code] = &errorInfo{
+ errorType: errorTypeHandler,
+ handler: h,
+ method: code,
+ }
return BeeApp
}
@@ -378,12 +389,12 @@ func ErrorController(c ControllerInterface) *App {
for i := 0; i < rt.NumMethod(); i++ {
methodName := rt.Method(i).Name
if !utils.InSlice(methodName, exceptMethod) && strings.HasPrefix(methodName, "Error") {
- errinfo := &errorInfo{}
- errinfo.errorType = errorTypeController
- errinfo.controllerType = ct
- errinfo.method = methodName
errName := strings.TrimPrefix(methodName, "Error")
- ErrorMaps[errName] = errinfo
+ ErrorMaps[errName] = &errorInfo{
+ errorType: errorTypeController,
+ controllerType: ct,
+ method: methodName,
+ }
}
}
return BeeApp
@@ -432,9 +443,8 @@ func executeError(err *errorInfo, ctx *context.Context, code int) {
execController.URLMapping()
- var in []reflect.Value
method := vc.MethodByName(err.method)
- method.Call(in)
+ method.Call([]reflect.Value{})
//render template
if BConfig.WebConfig.AutoRender {
diff --git a/filter.go b/filter.go
index cefd74a1..863223f7 100644
--- a/filter.go
+++ b/filter.go
@@ -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
- }
- if isok, ok := isok.(bool); ok {
- return isok
+ isOk := f.tree.Match(url, ctx)
+ if isOk != nil {
+ if b, ok := isOk.(bool); ok {
+ return b
+ }
}
return false
}
diff --git a/hooks.go b/hooks.go
index 61b80ad3..0588206f 100644
--- a/hooks.go
+++ b/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,16 +44,22 @@ 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,
+ }
+ confBytes, err := json.Marshal(conf)
+ if err != nil {
+ return err
+ }
+ sessionConfig = string(confBytes)
}
- GlobalSessions, err = session.NewManager(BConfig.WebConfig.Session.SessionProvider, sessionConfig)
- if err != nil {
+ if GlobalSessions, err = session.NewManager(BConfig.WebConfig.Session.SessionProvider, sessionConfig); err != nil {
return err
}
go GlobalSessions.GC()
@@ -64,9 +69,11 @@ func registerSession() error {
func registerTemplate() error {
if BConfig.WebConfig.AutoRender {
- err := BuildTemplate(BConfig.WebConfig.ViewsPath)
- if err != nil && BConfig.RunMode == "dev" {
- Warn(err)
+ if err := BuildTemplate(BConfig.WebConfig.ViewsPath); err != nil {
+ if BConfig.RunMode == "dev" {
+ Warn(err)
+ }
+ return err
}
}
return nil