2017-12-14 05:11:37 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
|
2020-06-08 04:16:55 +02:00
|
|
|
"github.com/kataras/iris/v12/_examples/view/herotemplate/template"
|
2017-12-14 05:11:37 +01:00
|
|
|
|
2019-10-25 00:27:02 +02:00
|
|
|
"github.com/kataras/iris/v12"
|
2017-12-14 05:11:37 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// $ 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) {
|
2020-07-20 12:36:39 +02:00
|
|
|
ctx.CompressWriter(true)
|
2018-03-08 04:21:16 +01:00
|
|
|
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
|
|
|
|
2020-07-10 22:21:09 +02:00
|
|
|
// iris context implements the io.Writer:
|
|
|
|
// _, err := template.UserListToWriter(userList, ctx)
|
|
|
|
// OR:
|
2018-03-08 04:21:16 +01:00
|
|
|
buffer := new(bytes.Buffer)
|
|
|
|
template.UserList(userList, buffer)
|
|
|
|
|
|
|
|
_, err := ctx.Write(buffer.Bytes())
|
|
|
|
if err != nil {
|
2020-05-17 23:25:38 +02:00
|
|
|
ctx.StopWithError(iris.StatusInternalServerError, err)
|
|
|
|
return
|
2018-03-08 04:21:16 +01:00
|
|
|
}
|
2017-12-14 05:11:37 +01:00
|
|
|
})
|
|
|
|
|
2020-03-05 21:41:27 +01:00
|
|
|
app.Listen(":8080")
|
2017-12-14 05:11:37 +01:00
|
|
|
}
|