compress

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 23, 2019 License: MIT Imports: 7 Imported by: 1

README

gowww compress GoDoc Build Coverage Go Report Status Stable

Package compress provides a clever gzip compressing handler.

It takes care to not handle small contents, or contents that are already compressed (like JPEG, MPEG or PDF).
Trying to gzip them not only wastes CPU but can potentially increase the response size.

Installing

  1. Get package:

    go get -u github.com/gowww/compress
    
  2. Import it in your code:

    import "github.com/gowww/compress"
    

Usage

To wrap an http.Handler, use Handle:

mux := http.NewServeMux()

mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
	fmt.Fprint(w, "Hello")
})

http.ListenAndServe(":8080", compress.Handle(handler))

To wrap an http.HandlerFunc, use HandleFunc:

http.Handle("/", compress.HandleFunc(func(w http.ResponseWriter, r *http.Request) {
	fmt.Fprint(w, "Hello")
}))

http.ListenAndServe(":8080", nil)

All in all, make sure to include this handler above any other handler that may write the response.

Documentation

Overview

Package compress provides a clever gzip compressing handler.

Example
package main

import (
	"fmt"
	"net/http"

	"github.com/gowww/compress"
)

func main() {
	mux := http.NewServeMux()

	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, "Hello")
	})

	http.ListenAndServe(":8080", compress.Handle(mux))
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Handle

func Handle(h http.Handler) http.Handler

Handle returns a Handler wrapping another http.Handler.

Example
package main

import (
	"fmt"
	"net/http"

	"github.com/gowww/compress"
)

func main() {
	mux := http.NewServeMux()

	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, "Hello")
	})

	http.ListenAndServe(":8080", compress.Handle(mux))
}
Output:

func HandleFunc

func HandleFunc(f http.HandlerFunc) http.Handler

HandleFunc returns a Handler wrapping an http.HandlerFunc.

Example
package main

import (
	"fmt"
	"net/http"

	"github.com/gowww/compress"
)

func main() {
	http.Handle("/", compress.HandleFunc(func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, "Hello")
	}))

	http.ListenAndServe(":8080", nil)
}
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL