iris/_examples/view/herotemplate/app.go
Gerasimos (Makis) Maropoulos 0f113dfcda (#1554) Add support for all common compressions (write and read)
- Remove the context.Context interface and export the *context, the iris.Context now points to the pointer\nSupport compression and rate limiting in the FileServer\nBit of code organisation


Former-commit-id: ad1c61bf968059510c6be9e7f2cceec7da70ba17
2020-07-10 23:21:09 +03:00

53 lines
1.1 KiB
Go

package main
import (
"bytes"
"github.com/kataras/iris/v12/_examples/view/herotemplate/template"
"github.com/kataras/iris/v12"
)
// $ go get -u github.com/shiyanhui/hero/hero
// $ 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.Compress(true)
ctx.ContentType("text/html")
userList := []string{
"Alice",
"Bob",
"Tom",
}
// Had better use buffer sync.Pool.
// 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)
// ctx.Write(buffer.Bytes())
// iris context implements the io.Writer:
// _, err := template.UserListToWriter(userList, ctx)
// OR:
buffer := new(bytes.Buffer)
template.UserList(userList, buffer)
_, err := ctx.Write(buffer.Bytes())
if err != nil {
ctx.StopWithError(iris.StatusInternalServerError, err)
return
}
})
app.Listen(":8080")
}