mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 18:51:03 +01:00
d697426cb6
Change the new ctx.Compres to ctx.CompressWriter and iris.Compress and iris.CompressReader as one iris.Compression Update the README example (master development branch) Former-commit-id: fb67858fe5be5662b5816df41020c28ff9a8c6f6
65 lines
1.3 KiB
Go
65 lines
1.3 KiB
Go
package main
|
|
|
|
import "github.com/kataras/iris/v12"
|
|
|
|
func main() {
|
|
app := newApp()
|
|
app.Logger().SetLevel("debug")
|
|
app.Listen(":8080")
|
|
}
|
|
|
|
func newApp() *iris.Application {
|
|
app := iris.New()
|
|
// HERE and you are ready to GO:
|
|
app.Use(iris.Compression)
|
|
|
|
app.Get("/", send)
|
|
app.Post("/", receive)
|
|
|
|
return app
|
|
}
|
|
|
|
type payload struct {
|
|
Username string `json:"username"`
|
|
}
|
|
|
|
func send(ctx iris.Context) {
|
|
ctx.JSON(payload{
|
|
Username: "Makis",
|
|
})
|
|
}
|
|
|
|
func receive(ctx iris.Context) {
|
|
var p payload
|
|
if err := ctx.ReadJSON(&p); err != nil {
|
|
ctx.Application().Logger().Debugf("ReadJSON: %v", err)
|
|
}
|
|
|
|
ctx.WriteString(p.Username)
|
|
}
|
|
|
|
/* Manually:
|
|
func enableCompression(ctx iris.Context) {
|
|
// Enable writing using compression (deflate, gzip, brotli, snappy, s2):
|
|
err := ctx.CompressWriter(true)
|
|
if err != nil {
|
|
ctx.Application().Logger().Debugf("writer: %v", err)
|
|
// if you REQUIRE server to SEND compressed data then `return` here.
|
|
// return
|
|
}
|
|
|
|
// Enable reading and binding request's compressed data:
|
|
err = ctx.CompressReader(true)
|
|
if err != nil &&
|
|
// on GET we don't expect writing with gzip from client
|
|
ctx.Method() != iris.MethodGet {
|
|
ctx.Application().Logger().Debugf("reader: %v", err)
|
|
// if you REQUIRE server to RECEIVE only
|
|
// compressed data then `return` here.
|
|
// return
|
|
}
|
|
|
|
ctx.Next()
|
|
}
|
|
*/
|