1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-25 22:31:28 +00:00
Beego/controller.go

171 lines
3.8 KiB
Go
Raw Normal View History

2012-12-19 08:20:55 +00:00
package beego
import (
"bytes"
"encoding/json"
"encoding/xml"
2013-01-01 15:11:15 +00:00
"github.com/astaxie/session"
2012-12-19 08:20:55 +00:00
"html/template"
"io/ioutil"
"net/http"
"net/url"
"path"
"strconv"
)
type Controller struct {
Ctx *Context
Data map[interface{}]interface{}
ChildName string
TplNames string
Layout string
TplExt string
}
type ControllerInterface interface {
Init(ct *Context, cn string)
Prepare()
Get()
Post()
Delete()
Put()
Head()
Patch()
Options()
Finish()
Render() error
}
func (c *Controller) Init(ctx *Context, cn string) {
c.Data = make(map[interface{}]interface{})
c.Layout = ""
c.TplNames = ""
c.ChildName = cn
c.Ctx = ctx
c.TplExt = "tpl"
}
func (c *Controller) Prepare() {
}
func (c *Controller) Finish() {
}
func (c *Controller) Get() {
http.Error(c.Ctx.ResponseWriter, "Method Not Allowed", 405)
}
func (c *Controller) Post() {
http.Error(c.Ctx.ResponseWriter, "Method Not Allowed", 405)
}
func (c *Controller) Delete() {
http.Error(c.Ctx.ResponseWriter, "Method Not Allowed", 405)
}
func (c *Controller) Put() {
http.Error(c.Ctx.ResponseWriter, "Method Not Allowed", 405)
}
func (c *Controller) Head() {
http.Error(c.Ctx.ResponseWriter, "Method Not Allowed", 405)
}
func (c *Controller) Patch() {
http.Error(c.Ctx.ResponseWriter, "Method Not Allowed", 405)
}
func (c *Controller) Options() {
http.Error(c.Ctx.ResponseWriter, "Method Not Allowed", 405)
}
func (c *Controller) Render() error {
2013-03-14 13:17:37 +00:00
rb, err := c.RenderBytes()
if err != nil {
return err
} else {
c.Ctx.SetHeader("Content-Length", strconv.Itoa(len(rb)), true)
c.Ctx.ContentType("text/html")
c.Ctx.ResponseWriter.Write(rb)
return nil
}
return nil
}
func (c *Controller) RenderBytes() ([]byte, error) {
2012-12-19 08:20:55 +00:00
//if the controller has set layout, then first get the tplname's content set the content to the layout
if c.Layout != "" {
if c.TplNames == "" {
c.TplNames = c.ChildName + "/" + c.Ctx.Request.Method + "." + c.TplExt
}
_, file := path.Split(c.TplNames)
2013-03-13 16:14:09 +00:00
subdir := path.Dir(c.TplNames)
2012-12-19 08:20:55 +00:00
newbytes := bytes.NewBufferString("")
2013-03-13 16:14:09 +00:00
BeeTemplates[subdir].ExecuteTemplate(newbytes, file, c.Data)
2012-12-19 08:20:55 +00:00
tplcontent, _ := ioutil.ReadAll(newbytes)
c.Data["LayoutContent"] = template.HTML(string(tplcontent))
_, file = path.Split(c.Layout)
2013-03-14 13:17:37 +00:00
ibytes := bytes.NewBufferString("")
err := BeeTemplates[subdir].ExecuteTemplate(ibytes, file, c.Data)
2012-12-19 08:20:55 +00:00
if err != nil {
Trace("template Execute err:", err)
}
2013-03-14 13:17:37 +00:00
icontent, _ := ioutil.ReadAll(ibytes)
return icontent, nil
2012-12-19 08:20:55 +00:00
} else {
if c.TplNames == "" {
c.TplNames = c.ChildName + "/" + c.Ctx.Request.Method + "." + c.TplExt
}
_, file := path.Split(c.TplNames)
2013-03-13 16:14:09 +00:00
subdir := path.Dir(c.TplNames)
2013-03-14 13:17:37 +00:00
ibytes := bytes.NewBufferString("")
err := BeeTemplates[subdir].ExecuteTemplate(ibytes, file, c.Data)
2012-12-19 08:20:55 +00:00
if err != nil {
Trace("template Execute err:", err)
}
2013-03-14 13:17:37 +00:00
icontent, _ := ioutil.ReadAll(ibytes)
return icontent, nil
2012-12-19 08:20:55 +00:00
}
2013-03-14 13:17:37 +00:00
return []byte{}, nil
2012-12-19 08:20:55 +00:00
}
func (c *Controller) Redirect(url string, code int) {
c.Ctx.Redirect(code, url)
}
func (c *Controller) ServeJson() {
2013-01-06 03:25:05 +00:00
content, err := json.MarshalIndent(c.Data["json"], "", " ")
2012-12-19 08:20:55 +00:00
if err != nil {
http.Error(c.Ctx.ResponseWriter, err.Error(), http.StatusInternalServerError)
return
}
c.Ctx.SetHeader("Content-Length", strconv.Itoa(len(content)), true)
c.Ctx.ContentType("json")
c.Ctx.ResponseWriter.Write(content)
}
func (c *Controller) ServeXml() {
2013-01-06 03:25:05 +00:00
content, err := xml.Marshal(c.Data["xml"])
2012-12-19 08:20:55 +00:00
if err != nil {
http.Error(c.Ctx.ResponseWriter, err.Error(), http.StatusInternalServerError)
return
}
c.Ctx.SetHeader("Content-Length", strconv.Itoa(len(content)), true)
c.Ctx.ContentType("xml")
c.Ctx.ResponseWriter.Write(content)
}
func (c *Controller) Input() url.Values {
c.Ctx.Request.ParseForm()
return c.Ctx.Request.Form
}
2013-01-01 15:11:15 +00:00
func (c *Controller) StartSession() (sess session.Session) {
sess = GlobalSessions.SessionStart(c.Ctx.ResponseWriter, c.Ctx.Request)
return
}