Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kataras/compress
HTTP Compression for Go
https://github.com/kataras/compress
brotli compression deflate go golang gzip http iris snappy
Last synced: about 2 months ago
JSON representation
HTTP Compression for Go
- Host: GitHub
- URL: https://github.com/kataras/compress
- Owner: kataras
- License: mit
- Created: 2020-07-11T00:39:49.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-04-09T13:08:13.000Z (9 months ago)
- Last Synced: 2024-10-14T11:24:22.213Z (2 months ago)
- Topics: brotli, compression, deflate, go, golang, gzip, http, iris, snappy
- Language: Go
- Homepage:
- Size: 34.2 KB
- Stars: 16
- Watchers: 3
- Forks: 2
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Compress
[![build status](https://img.shields.io/github/actions/workflow/status/kataras/compress/ci.yml?style=for-the-badge)](https://github.com/kataras/compress/actions) [![report card](https://img.shields.io/badge/report%20card-a%2B-ff3333.svg?style=for-the-badge)](https://goreportcard.com/report/github.com/kataras/compress) [![godocs](https://img.shields.io/badge/go-%20docs-488AC7.svg?style=for-the-badge)](https://pkg.go.dev/github.com/kataras/compress)
Fast and easy-to-use compression package for Go applications.
## Installation
The only requirement is the [Go Programming Language](https://go.dev/dl/).
```sh
$ go get github.com/kataras/compress
```## Getting Started
Import the package:
```go
package mainimport "github.com/kataras/compress"
```Wrap a handler to enable writing and reading using the best offered compression:
```go
import "net/http"mux := http.NewServeMux()
// [...]
http.ListenAndServe(":8080", compress.Handler(mux))
```Wrap any `io.Writer` for writing data using compression with `NewWriter`:
```go
import "bytes"
import "encoding/json"buf := new(bytes.Buffer)
w, err := compress.NewWriter(buf, compress.GZIP, -1)
if err != nil {
panic(err)
}json.NewEncoder(w).Encode(payload{Data: "my data"})
w.Close()
```Wrap any `io.Reader` for reading compressed data with `NewReader`:
```go
// Where resp.Body is an io.Reader.
r, err := compress.NewReader(resp.Body, compress.GZIP)
if err != nil {
panic(err)
}
defer r.Close()body, err := ioutil.ReadAll(r)
```To retrieve the underline `http.ResponseWriter` please use `w.(*compress.ResponseWriter).ResponseWriter`.
Example Code:
```go
import "net/http"func handler(w http.ResponseWriter, r *http.Request) {
target := "/your/asset.js"if pusher, ok := w.(*compress.ResponseWriter).ResponseWriter.(http.Pusher); ok {
err := pusher.Push(target, &http.PushOptions{
Header: http.Header{
"Accept-Encoding": r.Header["Accept-Encoding"],
}})
if err != nil {
if err == http.ErrNotSupported {
http.Error(w, "HTTP/2 push not supported", http.StatusHTTPVersionNotSupported)
} else {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
return
}
}
// [...]
}
```> The `http.CloseNotifier` is obselete by Go authors, please use `Request.Context().Done()` instead.
Supported compression algorithms:
- gzip
- deflate
- brotli
- snappyPlease navigate through [_examples](_examples) directory for more.
## License
This software is licensed under the [MIT License](LICENSE).