Skip to content

pablor21/echo-etag

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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.

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

About

Etag middleware for Echo Framework

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages