middleware

package
v0.0.22 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2025 License: AGPL-3.0 Imports: 8 Imported by: 0

README

Middleware

This module supports adding middlewares to a route adapter chain.

Using this module

Use Adapt() to construct a chain of middlewares.

Example

package main

import (
	mw "github.com/qrest/gomisc/middleware"
	"net/http"
	"time"
)

func someHandler() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		// do stuff
	})
}

func main() {
	mux := http.NewServeMux()
	// create a cache with a size of 50 MiB 
	cacheFactory, err := mw.NewCacheFactory(50, nil)
	if err != nil {
		// handle error
	}

	// call the cache factory to generate a new middleware for each request, so it shares its internal cache
	mux.Handle("/somePath", mw.Adapt(someHandler(), cacheFactory(time.Minute*5), mw.MaxBody5MiB()))
	mux.Handle("/somePath2", mw.Adapt(someHandler(), cacheFactory(time.Minute*10), mw.MaxBody5MiB()))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Adapt

func Adapt(h http.Handler, adapters ...Adapter) http.Handler

Adapt constructs a sequence of http.Handler which act as a middleware.

func NewCacheFactory added in v0.0.5

func NewCacheFactory(numMiB int64, errorCallback func(error)) (func(ttl time.Duration) Adapter, error)

NewCacheFactory returns a middleware factory. Call the factory for each route, so they share the same internal cache.

Types

type Adapter

type Adapter func(http.Handler) http.Handler

func MaxBody

func MaxBody(size int64) Adapter

MaxBody limits the amount of bytes which can be read from the request body to size (in number of MiB).

func MaxBody5MiB

func MaxBody5MiB() Adapter

MaxBody5MiB limits the amount of bytes which can be read from the request body to 5 MiB.

Jump to

Keyboard shortcuts

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