1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-26 05:01:28 +00:00
Beego/httpResponse/response.go
2017-05-17 20:38:59 +03:00

53 lines
1.1 KiB
Go

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