2012-12-19 08:20:55 +00:00
|
|
|
package beego
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2013-04-18 15:13:53 +00:00
|
|
|
"compress/gzip"
|
|
|
|
"compress/zlib"
|
2012-12-19 08:20:55 +00:00
|
|
|
"encoding/json"
|
|
|
|
"encoding/xml"
|
2013-04-05 15:50:53 +00:00
|
|
|
"github.com/astaxie/beego/session"
|
2012-12-19 08:20:55 +00:00
|
|
|
"html/template"
|
2013-04-16 10:20:55 +00:00
|
|
|
"io"
|
2012-12-19 08:20:55 +00:00
|
|
|
"io/ioutil"
|
2013-04-09 15:33:48 +00:00
|
|
|
"mime/multipart"
|
2012-12-19 08:20:55 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2013-04-16 10:20:55 +00:00
|
|
|
"os"
|
2012-12-19 08:20:55 +00:00
|
|
|
"path"
|
|
|
|
"strconv"
|
2013-04-09 16:24:28 +00:00
|
|
|
"strings"
|
2012-12-19 08:20:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
2013-04-18 15:13:53 +00:00
|
|
|
c.Ctx.ResponseWriter.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
|
|
output_writer := c.Ctx.ResponseWriter.(io.Writer)
|
|
|
|
if EnableGzip == true && c.Ctx.Request.Header.Get("Accept-Encoding") != "" {
|
|
|
|
splitted := strings.SplitN(c.Ctx.Request.Header.Get("Accept-Encoding"), ",", -1)
|
|
|
|
encodings := make([]string, len(splitted))
|
|
|
|
|
|
|
|
for i, val := range splitted {
|
|
|
|
encodings[i] = strings.TrimSpace(val)
|
|
|
|
}
|
|
|
|
for _, val := range encodings {
|
|
|
|
if val == "gzip" {
|
|
|
|
c.Ctx.ResponseWriter.Header().Set("Content-Encoding", "gzip")
|
|
|
|
output_writer, _ = gzip.NewWriterLevel(c.Ctx.ResponseWriter, gzip.BestSpeed)
|
|
|
|
|
|
|
|
break
|
|
|
|
} else if val == "deflate" {
|
|
|
|
c.Ctx.ResponseWriter.Header().Set("Content-Encoding", "deflate")
|
|
|
|
output_writer, _ = zlib.NewWriterLevel(c.Ctx.ResponseWriter, zlib.BestSpeed)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
c.Ctx.SetHeader("Content-Length", strconv.Itoa(len(rb)), true)
|
|
|
|
}
|
|
|
|
output_writer.Write(rb)
|
|
|
|
switch output_writer.(type) {
|
|
|
|
case *gzip.Writer:
|
|
|
|
output_writer.(*gzip.Writer).Close()
|
|
|
|
case *zlib.Writer:
|
|
|
|
output_writer.(*zlib.Writer).Close()
|
|
|
|
case io.WriteCloser:
|
|
|
|
output_writer.(io.WriteCloser).Close()
|
|
|
|
}
|
2013-03-14 13:17:37 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-04-03 15:37:59 +00:00
|
|
|
func (c *Controller) RenderString() (string, error) {
|
|
|
|
b, e := c.RenderBytes()
|
|
|
|
return string(b), e
|
|
|
|
}
|
|
|
|
|
2013-03-14 13:17:37 +00:00
|
|
|
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
|
|
|
|
}
|
2013-04-02 15:57:15 +00:00
|
|
|
if RunMode == "dev" {
|
2013-04-03 15:37:59 +00:00
|
|
|
BuildTemplate(ViewsPath)
|
2013-04-02 15:57:15 +00:00
|
|
|
}
|
2013-04-03 15:37:59 +00:00
|
|
|
subdir := path.Dir(c.TplNames)
|
2012-12-19 08:20:55 +00:00
|
|
|
_, file := path.Split(c.TplNames)
|
|
|
|
newbytes := bytes.NewBufferString("")
|
2013-04-03 15:37:59 +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("")
|
2013-04-03 15:37:59 +00:00
|
|
|
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
|
|
|
|
}
|
2013-04-02 15:57:15 +00:00
|
|
|
if RunMode == "dev" {
|
2013-04-03 15:37:59 +00:00
|
|
|
BuildTemplate(ViewsPath)
|
2013-04-02 15:57:15 +00:00
|
|
|
}
|
2013-04-03 15:37:59 +00:00
|
|
|
subdir := path.Dir(c.TplNames)
|
2012-12-19 08:20:55 +00:00
|
|
|
_, file := path.Split(c.TplNames)
|
2013-03-14 13:17:37 +00:00
|
|
|
ibytes := bytes.NewBufferString("")
|
2013-04-03 15:37:59 +00:00
|
|
|
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 {
|
2013-04-09 13:49:17 +00:00
|
|
|
ct := c.Ctx.Request.Header.Get("Content-Type")
|
2013-04-09 16:24:28 +00:00
|
|
|
if strings.Contains(ct, "multipart/form-data") {
|
2013-04-09 13:49:17 +00:00
|
|
|
c.Ctx.Request.ParseMultipartForm(MaxMemory) //64MB
|
|
|
|
} else {
|
|
|
|
c.Ctx.Request.ParseForm()
|
|
|
|
}
|
2012-12-19 08:20:55 +00:00
|
|
|
return c.Ctx.Request.Form
|
|
|
|
}
|
2013-01-01 15:11:15 +00:00
|
|
|
|
2013-04-09 13:49:17 +00:00
|
|
|
func (c *Controller) GetString(key string) string {
|
|
|
|
return c.Input().Get(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Controller) GetInt(key string) (int64, error) {
|
|
|
|
return strconv.ParseInt(c.Input().Get(key), 10, 64)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Controller) GetBool(key string) (bool, error) {
|
|
|
|
return strconv.ParseBool(c.Input().Get(key))
|
|
|
|
}
|
|
|
|
|
2013-04-09 15:33:48 +00:00
|
|
|
func (c *Controller) GetFile(key string) (multipart.File, *multipart.FileHeader, error) {
|
|
|
|
return c.Ctx.Request.FormFile(key)
|
|
|
|
}
|
|
|
|
|
2013-04-16 10:20:55 +00:00
|
|
|
func (c *Controller) SaveToFile(fromfile, tofile string) error {
|
|
|
|
file, _, err := c.Ctx.Request.FormFile(fromfile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
f, err := os.OpenFile(tofile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
io.Copy(f, file)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-04-05 15:50:53 +00:00
|
|
|
func (c *Controller) StartSession() (sess session.SessionStore) {
|
2013-01-01 15:11:15 +00:00
|
|
|
sess = GlobalSessions.SessionStart(c.Ctx.ResponseWriter, c.Ctx.Request)
|
|
|
|
return
|
|
|
|
}
|
2013-04-05 15:50:53 +00:00
|
|
|
|
|
|
|
func (c *Controller) SetSession(name string, value interface{}) {
|
|
|
|
ss := c.StartSession()
|
|
|
|
defer ss.SessionRelease()
|
|
|
|
ss.Set(name, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Controller) GetSession(name string) interface{} {
|
|
|
|
ss := c.StartSession()
|
|
|
|
defer ss.SessionRelease()
|
|
|
|
return ss.Get(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Controller) DelSession(name string) {
|
|
|
|
ss := c.StartSession()
|
|
|
|
defer ss.SessionRelease()
|
|
|
|
ss.Delete(name)
|
|
|
|
}
|