mirror of
https://github.com/kataras/iris.git
synced 2025-12-23 12:57:05 +00:00
add examples for read using custom decoder per type, read using custom decoder via iris#UnmarshalerFunc and to complete it add an example for the context#ReadXML.
Former-commit-id: 536b1780f12d0b9d9ce9aa976a0f95f18634ec2d
This commit is contained in:
@@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"log"
|
||||
|
||||
"github.com/kataras/iris/_examples/http_responsewriter/herotemplate/template"
|
||||
|
||||
@@ -13,11 +12,15 @@ import (
|
||||
// $ go run app.go
|
||||
//
|
||||
// Read more at https://github.com/shiyanhui/hero/hero
|
||||
|
||||
func main() {
|
||||
|
||||
app := iris.New()
|
||||
|
||||
app.Get("/users", func(ctx iris.Context) {
|
||||
ctx.Gzip(true)
|
||||
ctx.ContentType("text/html")
|
||||
|
||||
var userList = []string{
|
||||
"Alice",
|
||||
"Bob",
|
||||
@@ -25,30 +28,27 @@ func main() {
|
||||
}
|
||||
|
||||
// Had better use buffer sync.Pool.
|
||||
// Hero exports GetBuffer and PutBuffer for this.
|
||||
// Hero(github.com/shiyanhui/hero/hero) exports GetBuffer and PutBuffer for this.
|
||||
//
|
||||
// buffer := hero.GetBuffer()
|
||||
// defer hero.PutBuffer(buffer)
|
||||
buffer := new(bytes.Buffer)
|
||||
template.UserList(userList, buffer)
|
||||
|
||||
if _, err := ctx.Write(buffer.Bytes()); err != nil {
|
||||
log.Printf("ERR: %s\n", err)
|
||||
}
|
||||
})
|
||||
|
||||
app.Get("/users2", func(ctx iris.Context) {
|
||||
var userList = []string{
|
||||
"Alice",
|
||||
"Bob",
|
||||
"Tom",
|
||||
}
|
||||
// buffer := new(bytes.Buffer)
|
||||
// template.UserList(userList, buffer)
|
||||
// ctx.Write(buffer.Bytes())
|
||||
|
||||
// using an io.Writer for automatic buffer management (i.e. hero built-in buffer pool),
|
||||
// iris context implements the io.Writer by its ResponseWriter
|
||||
// which is an enhanced version of the standard http.ResponseWriter
|
||||
// but still 100% compatible.
|
||||
template.UserListToWriter(userList, ctx)
|
||||
// but still 100% compatible, GzipResponseWriter too:
|
||||
// _, err := template.UserListToWriter(userList, ctx.GzipResponseWriter())
|
||||
buffer := new(bytes.Buffer)
|
||||
template.UserList(userList, buffer)
|
||||
|
||||
_, err := ctx.Write(buffer.Bytes())
|
||||
if err != nil {
|
||||
ctx.StatusCode(iris.StatusInternalServerError)
|
||||
ctx.WriteString(err.Error())
|
||||
}
|
||||
})
|
||||
|
||||
app.Run(iris.Addr(":8080"))
|
||||
|
||||
Reference in New Issue
Block a user