1
0
mirror of https://github.com/astaxie/beego.git synced 2025-06-15 12:00:38 +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

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