Giter Club home page Giter Club logo

compress's Introduction

Compress

build status report card godocs

Fast and easy-to-use compression package for Go applications.

Installation

The only requirement is the Go Programming Language.

$ go get github.com/kataras/compress

Getting Started

Import the package:

package main

import "github.com/kataras/compress"

Wrap a handler to enable writing and reading using the best offered compression:

import "net/http"

mux := http.NewServeMux()
// [...]
http.ListenAndServe(":8080", compress.Handler(mux))

Wrap any io.Writer for writing data using compression with NewWriter:

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:

// 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:

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
  • snappy

Please navigate through _examples directory for more.

License

This software is licensed under the MIT License.

compress's People

Contributors

dependabot[bot] avatar kataras avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

isgasho forkkit

compress's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.