2013-08-21 09:59:31 +00:00
|
|
|
package context
|
2013-08-21 05:24:14 +00:00
|
|
|
|
|
|
|
import (
|
2013-11-26 03:05:49 +00:00
|
|
|
"net/http"
|
2013-12-03 13:37:39 +00:00
|
|
|
|
|
|
|
"github.com/astaxie/beego/middleware"
|
2013-08-21 05:24:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Context struct {
|
|
|
|
Input *BeegoInput
|
|
|
|
Output *BeegoOutput
|
|
|
|
Request *http.Request
|
|
|
|
ResponseWriter http.ResponseWriter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *Context) Redirect(status int, localurl string) {
|
|
|
|
ctx.Output.Header("Location", localurl)
|
|
|
|
ctx.Output.SetStatus(status)
|
2013-12-16 14:54:29 +00:00
|
|
|
ctx.Output.Body([]byte(" "))
|
2013-08-21 05:24:14 +00:00
|
|
|
}
|
|
|
|
|
2013-09-09 16:00:11 +00:00
|
|
|
func (ctx *Context) Abort(status int, body string) {
|
|
|
|
ctx.Output.SetStatus(status)
|
2013-12-16 14:54:29 +00:00
|
|
|
// first panic from ErrorMaps, is is user defined error functions.
|
|
|
|
if _, ok := middleware.ErrorMaps[body]; ok {
|
|
|
|
panic(body)
|
|
|
|
}
|
|
|
|
// second panic from HTTPExceptionMaps, it is system defined functions.
|
2013-11-26 03:05:49 +00:00
|
|
|
if e, ok := middleware.HTTPExceptionMaps[status]; ok {
|
|
|
|
if len(body) >= 1 {
|
|
|
|
e.Description = body
|
|
|
|
}
|
|
|
|
panic(e)
|
|
|
|
}
|
2013-12-16 14:54:29 +00:00
|
|
|
// last panic user string
|
|
|
|
panic(body)
|
2013-09-09 16:00:11 +00:00
|
|
|
}
|
|
|
|
|
2013-08-21 05:24:14 +00:00
|
|
|
func (ctx *Context) WriteString(content string) {
|
|
|
|
ctx.Output.Body([]byte(content))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *Context) GetCookie(key string) string {
|
|
|
|
return ctx.Input.Cookie(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *Context) SetCookie(name string, value string, others ...interface{}) {
|
|
|
|
ctx.Output.Cookie(name, value, others...)
|
|
|
|
}
|