1
0
mirror of https://github.com/astaxie/beego.git synced 2025-06-11 04:20:40 +00:00

move response

This commit is contained in:
Eyal Post
2017-05-18 09:05:49 +03:00
parent e32a18203b
commit 3e51823c0f

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)
}