mirror of
https://github.com/kataras/iris.git
synced 2025-12-17 18:07:01 +00:00
(#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
This commit is contained in:
64
_examples/compression/main.go
Normal file
64
_examples/compression/main.go
Normal file
@@ -0,0 +1,64 @@
|
||||
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.Compress, iris.CompressReader)
|
||||
|
||||
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.Compress(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()
|
||||
}
|
||||
*/
|
||||
Reference in New Issue
Block a user