conditional

package
v0.0.26 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2026 License: MIT Imports: 3 Imported by: 0

README

Conditional Requests Middleware

What it does

Handles If-None-Match, If-Match, If-Modified-Since, and related precondition headers using configured ETag or Last-Modified functions.

How to implement

package main

import (
	"github.com/oarkflow/fh"
	"github.com/oarkflow/fh/mw/conditional"
)

func main() {
	app := fh.New()
	app.Use(conditional.New(conditional.Config{ETag: func(c fh.Ctx) string { return `"v1"` }}))

	app.Get("/", func(c fh.Ctx) error {
		return c.Status(fh.StatusOK).SendString("ok")
	})
}

Impact

Reduces bandwidth and handler work when clients have fresh cached data. It can return 304 or precondition failures.

Ordering guidance

Run before expensive handlers if tags can be computed cheaply. Pair with ETag/static/cache middleware.

Production considerations

Use strong validators for write preconditions. Ensure ETags change whenever the representation changes.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(cfg Config) fh.HandlerFunc

Types

type Config

type Config struct {
	ETag         ETagFunc
	LastModified LastModifiedFunc
	WeakCompare  bool
}

type ETagFunc

type ETagFunc func(fh.Ctx) string

type LastModifiedFunc

type LastModifiedFunc func(fh.Ctx) time.Time

Jump to

Keyboard shortcuts

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