New rendering pipeline

This commit is contained in:
Manu Mtz-Almeida 2015-05-18 16:09:15 +02:00
parent 947b53d4a2
commit 21b5154fd7

View File

@ -324,15 +324,18 @@ func (c *Context) Header(key, value string) {
} }
} }
func (c *Context) Render(code int, r render.Render) { func (c *Context) Return(code int, r render.Render) {
w := c.Writer c.Writer.WriteHeader(code)
w.WriteHeader(code) c.Render(r)
if err := r.Write(w); err != nil { c.Abort()
}
func (c *Context) Render(r render.Render) {
if err := r.Write(c.Writer); err != nil {
debugPrintError(err) debugPrintError(err)
c.ErrorTyped(err, ErrorTypeInternal, nil) c.ErrorTyped(err, ErrorTypeInternal, nil)
c.AbortWithStatus(500) c.AbortWithStatus(500)
} }
//c.Abort()
} }
// Renders the HTTP template specified by its file name. // Renders the HTTP template specified by its file name.
@ -340,28 +343,28 @@ func (c *Context) Render(code int, r render.Render) {
// See http://golang.org/doc/articles/wiki/ // See http://golang.org/doc/articles/wiki/
func (c *Context) HTML(code int, name string, obj interface{}) { func (c *Context) HTML(code int, name string, obj interface{}) {
instance := c.Engine.HTMLRender.Instance(name, obj) instance := c.Engine.HTMLRender.Instance(name, obj)
c.Render(code, instance) c.Return(code, instance)
} }
func (c *Context) IndentedJSON(code int, obj interface{}) { func (c *Context) IndentedJSON(code int, obj interface{}) {
c.Render(code, render.IndentedJSON{Data: obj}) c.Return(code, render.IndentedJSON{Data: obj})
} }
// Serializes the given struct as JSON into the response body in a fast and efficient way. // Serializes the given struct as JSON into the response body in a fast and efficient way.
// It also sets the Content-Type as "application/json". // It also sets the Content-Type as "application/json".
func (c *Context) JSON(code int, obj interface{}) { func (c *Context) JSON(code int, obj interface{}) {
c.Render(code, render.JSON{Data: obj}) c.Return(code, render.JSON{Data: obj})
} }
// Serializes the given struct as XML into the response body in a fast and efficient way. // Serializes the given struct as XML into the response body in a fast and efficient way.
// It also sets the Content-Type as "application/xml". // It also sets the Content-Type as "application/xml".
func (c *Context) XML(code int, obj interface{}) { func (c *Context) XML(code int, obj interface{}) {
c.Render(code, render.XML{Data: obj}) c.Return(code, render.XML{Data: obj})
} }
// Writes the given string into the response body and sets the Content-Type to "text/plain". // Writes the given string into the response body and sets the Content-Type to "text/plain".
func (c *Context) String(code int, format string, values ...interface{}) { func (c *Context) String(code int, format string, values ...interface{}) {
c.Render(code, render.String{ c.Return(code, render.String{
Format: format, Format: format,
Data: values}, Data: values},
) )
@ -369,7 +372,7 @@ func (c *Context) String(code int, format string, values ...interface{}) {
// Returns a HTTP redirect to the specific location. // Returns a HTTP redirect to the specific location.
func (c *Context) Redirect(code int, location string) { func (c *Context) Redirect(code int, location string) {
c.Render(-1, render.Redirect{ c.Render(render.Redirect{
Code: code, Code: code,
Location: location, Location: location,
Request: c.Request, Request: c.Request,
@ -378,7 +381,7 @@ func (c *Context) Redirect(code int, location string) {
// Writes some data into the body stream and updates the HTTP code. // Writes some data into the body stream and updates the HTTP code.
func (c *Context) Data(code int, contentType string, data []byte) { func (c *Context) Data(code int, contentType string, data []byte) {
c.Render(code, render.Data{ c.Return(code, render.Data{
ContentType: contentType, ContentType: contentType,
Data: data, Data: data,
}) })
@ -386,14 +389,14 @@ func (c *Context) Data(code int, contentType string, data []byte) {
// Writes the specified file into the body stream // Writes the specified file into the body stream
func (c *Context) File(filepath string) { func (c *Context) File(filepath string) {
c.Render(-1, render.File{ c.Return(-1, render.File{
Path: filepath, Path: filepath,
Request: c.Request, Request: c.Request,
}) })
} }
func (c *Context) SSEvent(name string, message interface{}) { func (c *Context) SSEvent(name string, message interface{}) {
c.Render(-1, sse.Event{ c.Render(sse.Event{
Event: name, Event: name,
Data: message, Data: message,
}) })