mirror of
https://github.com/kataras/iris.git
synced 2026-01-25 04:45:57 +00:00
reorganization of _examples and add some new examples such as iris+groupcache+mysql+docker
Former-commit-id: ed635ee95de7160cde11eaabc0c1dcb0e460a620
This commit is contained in:
54
_examples/view/herotemplate/app.go
Normal file
54
_examples/view/herotemplate/app.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
||||
"github.com/kataras/iris/v12/_examples/response-writer/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.Gzip(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())
|
||||
|
||||
// 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, 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.StopWithError(iris.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
})
|
||||
|
||||
app.Listen(":8080")
|
||||
}
|
||||
Reference in New Issue
Block a user