etag

package module
v4.0.3 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2023 License: MIT Imports: 10 Imported by: 1

README

Echo Etag Middleware

GoDoc GitHub release GitHub license

Etag middleware for Echo Framework

Features

  • Support Etag and Weak Etag
  • Support Skipper
  • Configurable Hash Function (default: sha256 for Etag and crc32 for Weak Etag) Any hash function that implements the hash.Hash interface can be used.

BEWARE: Creating an Etag will buffer the entire response body. This may consume a lot of memory if the response body is large (files). If this is a concern, you should use the Skipper option to skip Etag generation for large responses and use other method of caching, for example: Last-Modified header.

Installation

$ go get github.com/pablor21/echo-etag/v4

Usage

package main

import (
    "github.com/labstack/echo/v4"
    etag "github.com/pablor21/echo-etag/v4"
)

func main() {
    e := echo.New()

    //Etag middleware
    e.Use(etag.Etag())

    e.Start(":1323")
}

Example


package main

import (
    "github.com/labstack/echo/v4"
    etag "github.com/pablor21/echo-etag/v4"
)

func main() {
    e := echo.New()

    //Etag middleware
    e.Use(etag.Etag())

    e.GET("/", func(c echo.Context) error {
        return c.String(200, "Hello, World!")
    })

    e.Start(":1323")
}

Configuration


package main

import (
    "crypto/md5"
    "github.com/labstack/echo/v4"
    etag "github.com/pablor21/echo-etag/v4"
)

func main() {
    e := echo.New()

    //Etag middleware
    e.Use(etag.WithConfig(etag.Config{
        Skipper: func(c echo.Context) bool {
            return c.Path() == "/skip"
        },
        Weak: true,
        HashFn: func(config etag.Config) hash.Hash {
            return md5.New() //use md5 hash
		},
    }))

    e.GET("/", func(c echo.Context) error {
        return c.String(200, "Hello, World!")
    })

    e.Start(":1323")
}

License

MIT License

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultEtagConfig is the default Etag middleware config.
	DefaultEtagConfig = Config{
		Skipper: middleware.DefaultSkipper,
		Weak:    true,
		HashFn: func(config Config) hash.Hash {
			if config.Weak {
				const crcPol = 0xD5828281
				crc32qTable := crc32.MakeTable(crcPol)
				return crc32.New(crc32qTable)
			}
			return sha1.New()
		},
	}
)

Functions

func Etag

func Etag() echo.MiddlewareFunc

Etag returns a Etag middleware.

func WithConfig added in v4.0.3

func WithConfig(config Config) echo.MiddlewareFunc

WithConfig returns a Etag middleware with config.

Types

type Config added in v4.0.3

type Config struct {
	// Skipper defines a function to skip middleware.
	Skipper middleware.Skipper
	// Weak defines if the Etag is weak or strong.
	Weak bool
	// HashFn defines the hash function to use. Default is crc32q.
	HashFn func(config Config) hash.Hash
}

Config defines the config for Etag middleware.

Jump to

Keyboard shortcuts

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