etag

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2023 License: Apache-2.0 Imports: 7 Imported by: 0

README

ETag (This is a community driven project)

ETag middleware for Hertz framework, inspired by fiber-etag.

The ETag (or entity tag) HTTP response header is an identifier for a specific version of a resource. It lets caches be more efficient and save bandwidth, as a web server does not need to resend a full response if the content was not changed. Additionally, etags help to prevent simultaneous updates of a resource from overwriting each other ("mid-air collisions").

Install

go get github.com/hertz-contrib/etag

Example

package main

import (
	"context"
	"net/http"

	"github.com/cloudwego/hertz/pkg/app"
	"github.com/cloudwego/hertz/pkg/app/server"
	"github.com/hertz-contrib/etag"
)

func main() {
	h := server.Default()
	h.Use(etag.New())
	h.GET("/ping", func(ctx context.Context, c *app.RequestContext) {
		c.String(http.StatusOK, "pong")
	})
	h.Spin()
}

Configuration

Configuration Default Description Example
WithWeak false Enable weak validator example
WithNext nil Defines a function to skip etag middleware when return is true example
WithGenerator nil Custom etag generation logic example

Documentation

Index

Constants

View Source
const HeaderIfNoneMatch = "If-None-Match"

Variables

This section is empty.

Functions

func New

func New(opts ...Option) app.HandlerFunc

New will create an etag middleware

Types

type Generator

type Generator func(ctx context.Context, c *app.RequestContext) []byte

type NextFunc

type NextFunc func(ctx context.Context, c *app.RequestContext) bool

type Option

type Option func(o *Options)

func WithGenerator

func WithGenerator(gen Generator) Option

WithGenerator will replace default etag generation with yours Note: you should not add a weak prefix to your custom etag when used with WithWeak

func WithNext

func WithNext(next NextFunc) Option

WithNext will skip etag middleware when return is true

func WithWeak

func WithWeak() Option

WithWeak will add weak prefix to the front of etag

type Options

type Options struct {
	// contains filtered or unexported fields
}

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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