mirror of
https://github.com/astaxie/beego.git
synced 2025-06-11 04:20:40 +00:00
move response
This commit is contained in:
52
context/httpResponse/response.go
Normal file
52
context/httpResponse/response.go
Normal 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)
|
||||
}
|
Reference in New Issue
Block a user