iris/_examples/request-body/read-gzip/main.go
Gerasimos (Makis) Maropoulos ed45c77be5 reorganization of _examples and add some new examples such as iris+groupcache+mysql+docker
Former-commit-id: ed635ee95de7160cde11eaabc0c1dcb0e460a620
2020-06-07 15:26:06 +03:00

45 lines
792 B
Go

package main
import (
"github.com/kataras/iris/v12"
)
func main() {
app := newApp()
app.Logger().SetLevel("debug")
app.Listen(":8080")
}
type payload struct {
Message string `json:"message"`
}
func newApp() *iris.Application {
app := iris.New()
// GzipReader is a middleware which enables gzip decompression,
// when client sends gzip compressed data.
//
// A shortcut of:
// func(ctx iris.Context) {
// ctx.GzipReader(true)
// ctx.Next()
// }
app.Use(iris.GzipReader)
app.Post("/", func(ctx iris.Context) {
// Bind incoming gzip compressed JSON to "p".
var p payload
if err := ctx.ReadJSON(&p); err != nil {
ctx.StopWithError(iris.StatusBadRequest, err)
return
}
// Send back the message as plain text.
ctx.WriteString(p.Message)
})
return app
}