mirror of
https://github.com/kataras/iris.git
synced 2025-02-02 15:30:36 +01:00
add example for the simple context#WriteGzip
Former-commit-id: 3b4e0834370ebde5e4796b3d75cdbd5c2c25093f
This commit is contained in:
parent
22504f01ef
commit
434b07675e
|
@ -292,6 +292,7 @@ You can serve [quicktemplate](https://github.com/valyala/quicktemplate) and [her
|
||||||
- [Write `valyala/quicktemplate` templates](http_responsewriter/quicktemplate)
|
- [Write `valyala/quicktemplate` templates](http_responsewriter/quicktemplate)
|
||||||
- [Write `shiyanhui/hero` templates](http_responsewriter/hero)
|
- [Write `shiyanhui/hero` templates](http_responsewriter/hero)
|
||||||
- [Text, Markdown, HTML, JSON, JSONP, XML, Binary](http_responsewriter/write-rest/main.go)
|
- [Text, Markdown, HTML, JSON, JSONP, XML, Binary](http_responsewriter/write-rest/main.go)
|
||||||
|
- [Write Gzip](http_responsewriter/write-gzip/main.go)
|
||||||
- [Stream Writer](http_responsewriter/stream-writer/main.go)
|
- [Stream Writer](http_responsewriter/stream-writer/main.go)
|
||||||
- [Transactions](http_responsewriter/transactions/main.go)
|
- [Transactions](http_responsewriter/transactions/main.go)
|
||||||
|
|
||||||
|
|
21
_examples/http_responsewriter/write-gzip/main.go
Normal file
21
_examples/http_responsewriter/write-gzip/main.go
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "github.com/kataras/iris"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
app := iris.New()
|
||||||
|
app.Get("/", func(ctx iris.Context) {
|
||||||
|
ctx.WriteGzip([]byte("Hello World!"))
|
||||||
|
ctx.Header("X-Custom",
|
||||||
|
"Headers can be set here after WriteGzip as well, because the data are kept before sent to the client when using the context's GzipResponseWriter and ResponseRecorder.")
|
||||||
|
})
|
||||||
|
|
||||||
|
app.Get("/2", func(ctx iris.Context) {
|
||||||
|
// same as the `WriteGzip`.
|
||||||
|
// However GzipResponseWriter gives you more options, like
|
||||||
|
// reset data, disable and more, look its methods.
|
||||||
|
ctx.GzipResponseWriter().WriteString("Hello World!")
|
||||||
|
})
|
||||||
|
|
||||||
|
app.Run(iris.Addr(":8080"))
|
||||||
|
}
|
|
@ -599,13 +599,11 @@ type Context interface {
|
||||||
ClientSupportsGzip() bool
|
ClientSupportsGzip() bool
|
||||||
// WriteGzip accepts bytes, which are compressed to gzip format and sent to the client.
|
// WriteGzip accepts bytes, which are compressed to gzip format and sent to the client.
|
||||||
// returns the number of bytes written and an error ( if the client doesn' supports gzip compression)
|
// returns the number of bytes written and an error ( if the client doesn' supports gzip compression)
|
||||||
//
|
// You may re-use this function in the same handler
|
||||||
// This function writes temporary gzip contents, the ResponseWriter is untouched.
|
// to write more data many times without any troubles.
|
||||||
WriteGzip(b []byte) (int, error)
|
WriteGzip(b []byte) (int, error)
|
||||||
// TryWriteGzip accepts bytes, which are compressed to gzip format and sent to the client.
|
// TryWriteGzip accepts bytes, which are compressed to gzip format and sent to the client.
|
||||||
// If client does not supprots gzip then the contents are written as they are, uncompressed.
|
// If client does not supprots gzip then the contents are written as they are, uncompressed.
|
||||||
//
|
|
||||||
// This function writes temporary gzip contents, the ResponseWriter is untouched.
|
|
||||||
TryWriteGzip(b []byte) (int, error)
|
TryWriteGzip(b []byte) (int, error)
|
||||||
// GzipResponseWriter converts the current response writer into a response writer
|
// GzipResponseWriter converts the current response writer into a response writer
|
||||||
// which when its .Write called it compress the data to gzip and writes them to the client.
|
// which when its .Write called it compress the data to gzip and writes them to the client.
|
||||||
|
@ -1900,28 +1898,18 @@ var (
|
||||||
// WriteGzip accepts bytes, which are compressed to gzip format and sent to the client.
|
// WriteGzip accepts bytes, which are compressed to gzip format and sent to the client.
|
||||||
// returns the number of bytes written and an error ( if the client doesn' supports gzip compression)
|
// returns the number of bytes written and an error ( if the client doesn' supports gzip compression)
|
||||||
//
|
//
|
||||||
// This function writes temporary gzip contents, the ResponseWriter is untouched.
|
// You may re-use this function in the same handler
|
||||||
|
// to write more data many times without any troubles.
|
||||||
func (ctx *context) WriteGzip(b []byte) (int, error) {
|
func (ctx *context) WriteGzip(b []byte) (int, error) {
|
||||||
if ctx.ClientSupportsGzip() {
|
if !ctx.ClientSupportsGzip() {
|
||||||
ctx.writer.Header().Add(varyHeaderKey, acceptEncodingHeaderKey)
|
return 0, errClientDoesNotSupportGzip
|
||||||
|
|
||||||
gzipWriter := acquireGzipWriter(ctx.writer)
|
|
||||||
defer releaseGzipWriter(gzipWriter)
|
|
||||||
n, err := gzipWriter.Write(b)
|
|
||||||
|
|
||||||
if err == nil {
|
|
||||||
ctx.Header(contentEncodingHeaderKey, "gzip")
|
|
||||||
} // else write the contents as it is? no let's create a new func for this
|
|
||||||
return n, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0, errClientDoesNotSupportGzip
|
return ctx.GzipResponseWriter().Write(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TryWriteGzip accepts bytes, which are compressed to gzip format and sent to the client.
|
// TryWriteGzip accepts bytes, which are compressed to gzip format and sent to the client.
|
||||||
// If client does not supprots gzip then the contents are written as they are, uncompressed.
|
// If client does not supprots gzip then the contents are written as they are, uncompressed.
|
||||||
//
|
|
||||||
// This function writes temporary gzip contents, the ResponseWriter is untouched.
|
|
||||||
func (ctx *context) TryWriteGzip(b []byte) (int, error) {
|
func (ctx *context) TryWriteGzip(b []byte) (int, error) {
|
||||||
n, err := ctx.WriteGzip(b)
|
n, err := ctx.WriteGzip(b)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user