compress

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2023 License: MIT Imports: 7 Imported by: 0

README

Compress

This is a middleware for lightning that provides compression for HTTP responses. It supports four compression algorithms: Brotli, Deflate, Gzip, and Zstd.

Installation

To install the middleware, run the following command:

go get github.com/lightning-contrib/compress

Usage

To use the middleware, import the package and add it to your lightning middleware chain:

package main

import (
	"github.com/go-labx/lightning"
	"github.com/lightning-contrib/compress"
)

func main() {
	app := lightning.NewApp()

	app.Use(compress.Default())

	app.Get("/ping", func(ctx *lightning.Context) {
		ctx.Text(200, "hello world")
	})

	app.Run(":6789")
}

By default, the middleware will use the following compression levels:

  • Brotli: brotli.DefaultCompression
  • Deflate: flate.DefaultCompression
  • Gzip: gzip.DefaultCompression

You can customize the compression levels by passing options to the New function:

app.Use(compress.New(
    compress.WithBrotliCompressionLevel(6),
    compress.WithDeflateCompressionLevel(9),
    compress.WithGzipCompressionLevel(1),
))

Supported Encodings

The middleware supports the following encodings:

  • br (Brotli)
  • deflate (Deflate)
  • gzip (Gzip)
  • zstd (Zstd)

If the client sends a request with an Accept-Encoding header that includes one of these encodings, the middleware will compress the response using the corresponding algorithm.

API Documentation

For detailed API documentation and usage examples, please refer to the documentation.

Contributing

If you'd like to contribute to lightning, please see CONTRIBUTING.md for guidelines.

License

This middleware is licensed under the MIT License. See LICENSE for more information.

Documentation

Index

Constants

View Source
const (
	EncodingBrotli  = "br"
	EncodingDeflate = "deflate"
	EncodingGzip    = "gzip"
	EncodingZstd    = "zstd"
)

Variables

This section is empty.

Functions

func Default

func Default() lightning.Middleware

Default returns a lightning middleware with default options

func New

func New(options ...Options) lightning.Middleware

New returns a lightning middleware with the given options

Types

type BrotliCompression

type BrotliCompression struct{}

func (*BrotliCompression) Compress

func (c *BrotliCompression) Compress(data []byte, level int) ([]byte, error)

Compress compresses the given data using Brotli compression algorithm

type Compressor

type Compressor interface {
	Compress(data []byte, level int) ([]byte, error)
}

Compressor is an interface that defines the Compress method

type DeflateCompression

type DeflateCompression struct{}

func (*DeflateCompression) Compress

func (c *DeflateCompression) Compress(data []byte, level int) ([]byte, error)

Compress compresses the given data using Deflate compression algorithm

type GzipCompression

type GzipCompression struct{}

func (*GzipCompression) Compress

func (c *GzipCompression) Compress(data []byte, level int) ([]byte, error)

Compress compresses the given data using Gzip compression algorithm

type Options

type Options func(*config)

Options is a function that takes a pointer to a config struct

func WithBrotliCompressionLevel

func WithBrotliCompressionLevel(level int) Options

WithBrotliCompressionLevel sets the compression level for Brotli compression algorithm

func WithDeflateCompressionLevel

func WithDeflateCompressionLevel(level int) Options

WithDeflateCompressionLevel sets the compression level for Deflate compression algorithm

func WithGzipCompressionLevel

func WithGzipCompressionLevel(level int) Options

WithGzipCompressionLevel sets the compression level for Gzip compression algorithm

type ZstdCompression

type ZstdCompression struct{}

func (*ZstdCompression) Compress

func (c *ZstdCompression) Compress(data []byte, _ int) ([]byte, error)

Compress compresses the given data using Zstd compression algorithm

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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