1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-25 20:51:29 +00:00

Refactor a bit to consolidate packages

This commit is contained in:
Eyal Post 2017-05-17 20:38:59 +03:00
parent d54cd4fa5f
commit 828cbbdf5d
7 changed files with 94 additions and 101 deletions

View File

@ -171,6 +171,22 @@ func (ctx *Context) CheckXSRFCookie() bool {
return true return true
} }
// RenderMethodResult renders the return value of a controller method to the output
func (ctx *Context) RenderMethodResult(result interface{}) {
if result != nil {
renderer, ok := result.(Renderer)
if !ok {
err, ok := result.(error)
if ok {
renderer = errorRenderer(err)
} else {
renderer = jsonRenderer(result)
}
}
renderer.Render(ctx)
}
}
//Response is a wrapper for the http.ResponseWriter //Response is a wrapper for the http.ResponseWriter
//started set to true if response was written to then don't execute other handler //started set to true if response was written to then don't execute other handler
type Response struct { type Response struct {

View File

@ -168,6 +168,19 @@ func sanitizeValue(v string) string {
return cookieValueSanitizer.Replace(v) return cookieValueSanitizer.Replace(v)
} }
func jsonRenderer(value interface{}) Renderer {
return rendererFunc(func(ctx *Context) {
ctx.Output.JSON(value, false, false)
})
}
func errorRenderer(err error) Renderer {
return rendererFunc(func(ctx *Context) {
ctx.Output.SetStatus(500)
ctx.WriteString(err.Error())
})
}
// JSON writes json to response body. // JSON writes json to response body.
// if coding is true, it converts utf-8 to \u0000 type. // if coding is true, it converts utf-8 to \u0000 type.
func (output *BeegoOutput) JSON(data interface{}, hasIndent bool, coding bool) error { func (output *BeegoOutput) JSON(data interface{}, hasIndent bool, coding bool) error {

12
context/renderer.go Normal file
View File

@ -0,0 +1,12 @@
package context
// Renderer defines an http response renderer
type Renderer interface {
Render(ctx *Context)
}
type rendererFunc func(ctx *Context)
func (f rendererFunc) Render(ctx *Context) {
f(ctx)
}

View File

@ -1,46 +0,0 @@
package response
import (
"strconv"
"net/http"
beecontext "github.com/astaxie/beego/context"
)
const (
NotFound StatusCode = http.StatusNotFound
BadRequest StatusCode = http.StatusBadRequest
)
// Renderer defines an http response renderer
type Renderer interface {
Render(ctx *beecontext.Context)
}
type rendererFunc func(ctx *beecontext.Context)
func (f rendererFunc) Render(ctx *beecontext.Context) {
f(ctx)
}
// StatusCode sets the http response status code
type StatusCode int
func (s StatusCode) Error() string {
return strconv.Itoa(int(s))
}
// Render sets the http status code
func (s StatusCode) Render(ctx *beecontext.Context) {
ctx.Output.SetStatus(int(s))
}
type statusCodeWithRender struct {
statusCode int
rendererFunc
}
func (s statusCodeWithRender) Error() string {
return strconv.Itoa(s.statusCode)
}

View File

@ -1,53 +0,0 @@
package response
import (
beecontext "github.com/astaxie/beego/context"
)
// JSON renders value to the response as JSON
func JSON(value interface{}, encoding ...bool) Renderer {
return rendererFunc(func(ctx *beecontext.Context) {
var (
hasIndent = true
hasEncoding = false
)
//TODO: need access to BConfig :(
// if BConfig.RunMode == PROD {
// hasIndent = false
// }
if len(encoding) > 0 && encoding[0] {
hasEncoding = true
}
ctx.Output.JSON(value, hasIndent, hasEncoding)
})
}
func errorRenderer(err error) Renderer {
return rendererFunc(func(ctx *beecontext.Context) {
ctx.Output.SetStatus(500)
ctx.WriteString(err.Error())
})
}
// Redirect renders http 302 with a URL
func Redirect(localurl string) error {
return statusCodeWithRender{302, func(ctx *beecontext.Context) {
ctx.Redirect(302, localurl)
}}
}
// RenderMethodResult renders the return value of a controller method to the output
func RenderMethodResult(result interface{}, ctx *beecontext.Context) {
if result != nil {
renderer, ok := result.(Renderer)
if !ok {
err, ok := result.(error)
if ok {
renderer = errorRenderer(err)
} else {
renderer = JSON(result)
}
}
renderer.Render(ctx)
}
}

52
httpResponse/response.go Normal file
View File

@ -0,0 +1,52 @@
package httpResponse
import (
"strconv"
"net/http"
beecontext "github.com/astaxie/beego/context"
)
const (
//BadRequest indicates http error 400
BadRequest StatusCode = http.StatusBadRequest
//NotFound indicates http error 404
NotFound StatusCode = http.StatusNotFound
)
// Redirect renders http 302 with a URL
func Redirect(localurl string) error {
return statusCodeWithRender{302, func(ctx *beecontext.Context) {
ctx.Redirect(302, localurl)
}}
}
// StatusCode sets the http response status code
type StatusCode int
func (s StatusCode) Error() string {
return strconv.Itoa(int(s))
}
// Render sets the http status code
func (s StatusCode) Render(ctx *beecontext.Context) {
ctx.Output.SetStatus(int(s))
}
type statusCodeWithRender struct {
statusCode int
f func(ctx *beecontext.Context)
}
//assert that statusCodeWithRender implements Renderer interface
var _r beecontext.Renderer = (*statusCodeWithRender)(nil)
func (s statusCodeWithRender) Error() string {
return strconv.Itoa(s.statusCode)
}
func (s statusCodeWithRender) Render(ctx *beecontext.Context) {
s.f(ctx)
}

View File

@ -28,7 +28,6 @@ import (
beecontext "github.com/astaxie/beego/context" beecontext "github.com/astaxie/beego/context"
"github.com/astaxie/beego/context/param" "github.com/astaxie/beego/context/param"
"github.com/astaxie/beego/context/response"
"github.com/astaxie/beego/logs" "github.com/astaxie/beego/logs"
"github.com/astaxie/beego/toolbox" "github.com/astaxie/beego/toolbox"
"github.com/astaxie/beego/utils" "github.com/astaxie/beego/utils"
@ -905,7 +904,7 @@ func (p *ControllerRegister) handleParamResponse(context *beecontext.Context, ex
result := results[i] result := results[i]
if result.Kind() != reflect.Interface || !result.IsNil() { if result.Kind() != reflect.Interface || !result.IsNil() {
resultValue := result.Interface() resultValue := result.Interface()
response.RenderMethodResult(resultValue, context) context.RenderMethodResult(resultValue)
} }
} }
if !context.ResponseWriter.Started && context.Output.Status == 0 { if !context.ResponseWriter.Started && context.Output.Status == 0 {