Skip to content

Latest commit

 

History

38 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Etag middleware for Echo Framework

Resources

Stars

11 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages