etag

package module
v5.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2026 License: MIT Imports: 12 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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.

Version compatibility

echo-etag Echo Branch
v5.x github.com/labstack/echo/v5 main
v4.x github.com/labstack/echo/v4 v4

Installation

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

Usage

package main

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

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

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

    e.Start(":1323")
}

Example


package main

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

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/v5"
    etag "github.com/pablor21/echo-etag/v5"
)

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

func WithConfig(config Config) echo.MiddlewareFunc

WithConfig returns a Etag middleware with config.

Types

type Config

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