2017-12-14 05:11:37 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
|
2017-12-25 19:05:32 +01:00
|
|
|
"github.com/kataras/iris/_examples/http_responsewriter/herotemplate/template"
|
2017-12-14 05:11:37 +01:00
|
|
|
|
|
|
|
"github.com/kataras/iris"
|
|
|
|
)
|
|
|
|
|
|
|
|
// $ go get -u github.com/shiyanhui/hero/hero
|
|
|
|
// $ go run app.go
|
|
|
|
//
|
|
|
|
// Read more at https://github.com/shiyanhui/hero/hero
|
2018-03-08 04:21:16 +01:00
|
|
|
|
2017-12-14 05:11:37 +01:00
|
|
|
func main() {
|
|
|
|
app := iris.New()
|
|
|
|
|
|
|
|
app.Get("/users", func(ctx iris.Context) {
|
2018-03-08 04:21:16 +01:00
|
|
|
ctx.Gzip(true)
|
|
|
|
ctx.ContentType("text/html")
|
|
|
|
|
2019-08-17 09:06:20 +02:00
|
|
|
userList := []string{
|
2017-12-14 05:11:37 +01:00
|
|
|
"Alice",
|
|
|
|
"Bob",
|
|
|
|
"Tom",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Had better use buffer sync.Pool.
|
2018-03-08 04:21:16 +01:00
|
|
|
// Hero(github.com/shiyanhui/hero/hero) exports GetBuffer and PutBuffer for this.
|
2017-12-14 05:11:37 +01:00
|
|
|
//
|
|
|
|
// buffer := hero.GetBuffer()
|
|
|
|
// defer hero.PutBuffer(buffer)
|
2018-03-08 04:21:16 +01:00
|
|
|
// buffer := new(bytes.Buffer)
|
|
|
|
// template.UserList(userList, buffer)
|
|
|
|
// ctx.Write(buffer.Bytes())
|
2017-12-14 05:11:37 +01:00
|
|
|
|
|
|
|
// using an io.Writer for automatic buffer management (i.e. hero built-in buffer pool),
|
|
|
|
// iris context implements the io.Writer by its ResponseWriter
|
2017-12-14 22:04:42 +01:00
|
|
|
// which is an enhanced version of the standard http.ResponseWriter
|
2018-03-08 04:21:16 +01:00
|
|
|
// 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())
|
|
|
|
}
|
2017-12-14 05:11:37 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
app.Run(iris.Addr(":8080"))
|
|
|
|
}
|