middleware

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2019 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package middleware provides flexibility at the HTTP request/response process. For this purpose, the Adapter pattern is used, which consist in wrapping handlers. An adapter may run code before and/or after the handler it is wrapping.

func MyAdapter(h http.Handler) http.Handler {
	nh := func(w http.ResponseWriter, r *http.Request) {
		// Code that run before
		h.ServeHTTP(w, r) // Breaks the flow if not used
		// Code that run after
	}

	return http.HandlerFunc(nh)
}

When a set of adapters is used, the result is a sequence of wrappers and its execution flow depends in the order that the adapters were given.

Adapt(h, f1, f2, f3)

1. f1 before code

2. f2 before code

3. f3 before code

4. h

5. f3 after code

6. f2 after code

7. f1 after code

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Adapt

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

Adapt wraps a http.Handler into a list of Adapters. Takes a http.Handler and a list of Adapters, which will be wrapped from right to left (and ran left to right).

Example
package main

import (
	"fmt"
	"net/http"
	"net/http/httptest"

	"nt.web.ve/go/ntgo/net/http/middleware"
)

func main() {
	h := middleware.Adapt(
		http.FileServer(http.Dir(".")),
		middleware.Cache("max-age=3600, s-max-age=3600"),
		middleware.Gzip(-1),
	)

	// http.Handle("/", h)

	w := httptest.NewRecorder()
	r := httptest.NewRequest(http.MethodGet, "/", nil)
	r.Header.Set("Accept-Encoding", "gzip")
	h.ServeHTTP(w, r)
	res := w.Result()
	fmt.Printf("Status: %v\n", res.Status)
	fmt.Printf("Cache-Control: %+v\n", res.Header.Get("Cache-Control"))
	fmt.Printf("Content-Encoding: %v", res.Header.Get("Content-Encoding"))
}
Output:

Status: 200 OK
Cache-Control: max-age=3600, s-max-age=3600
Content-Encoding: gzip

func AdaptFunc

func AdaptFunc(h func(w http.ResponseWriter, r *http.Request), a ...Adapter) http.Handler

AdaptFunc works as Adapt but for http.HandlerFunc.

Example
package main

import (
	"fmt"
	"net/http"
	"net/http/httptest"

	"nt.web.ve/go/ntgo/net/http/middleware"
)

func main() {
	h := middleware.AdaptFunc(
		func(w http.ResponseWriter, r *http.Request) {},
		middleware.JSONResponse(),
		middleware.Cache("max-age=3600, s-max-age=3600"),
	)

	// http.Handle("/", h)

	w := httptest.NewRecorder()
	r := httptest.NewRequest(http.MethodGet, "/", nil)
	h.ServeHTTP(w, r)
	res := w.Result()
	fmt.Printf("Status: %v\n", res.Status)
	fmt.Printf("Cache-Control: %+v\n", res.Header.Get("Cache-Control"))
	fmt.Printf("Content-Type: %v", res.Header.Get("Content-Type"))
}
Output:

Status: 200 OK
Cache-Control: max-age=3600, s-max-age=3600
Content-Type: application/json; charset=utf-8

Types

type Adapter

type Adapter func(http.Handler) http.Handler

Adapter is a wrapper for http.Handler. Takes a http.Handler as argument and creates a new one that may run code before and/or after calling the given.

func AddHeader

func AddHeader(key, value string) Adapter

AddHeader creates/appends a HTTP header before calling the http.Handler.

func Cache

func Cache(directives string) Adapter

Cache sets HTTP cache headers for GET requests.

Directives

* public/private: whether the cached response is for any or a specific user.

* max-age=TIME: cache life time in seconds. The maximum value is 1 year.

* s-max-age=TIME: same as max-age, but this one has effect in proxies.

* must-revalidate: force expired cached response revalidation, even in special circumstances (like slow connections, were cached responses are used even after they had expired).

* proxy-revalidate: same as must-revalidate, but this one has effect in proxies.

* no-cache: disables cache.

* no-store: disables cache, even in proxies.

func DelHeader

func DelHeader(key string) Adapter

DelHeader removes a HTTP header before calling the http.Handler.

func Gzip

func Gzip(level int) Adapter

Gzip compresses the response body. The compression level is given as an integer value according to the compress/flate package.

func JSONRequest

func JSONRequest(body interface{}) Adapter

JSONRequest checks that request has the appropriate HTTP method and the appropriate 'Content-Type' header. Responds with http.StatusMethodNotAllowed if the used method is not one of POST, PUT or PATCH. Responds with http.StatusUnsupportedMediaType if the 'Content-Type' header is not valid. body is used as response body.

func JSONResponse

func JSONResponse() Adapter

JSONResponse prepares the response to be a JSON response.

func Replace

func Replace(old, New string) Adapter

Replace replaces old by new from the request URL.

func SetHeader

func SetHeader(key, value string) Adapter

SetHeader creates/replaces a HTTP header before calling the http.Handler.

func StripPrefix

func StripPrefix(prefix string) Adapter

StripPrefix strips the given prefix from the request URL.

Jump to

Keyboard shortcuts

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