middleware

package
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: May 8, 2023 License: MIT Imports: 9 Imported by: 0

README

Middleware

The middleware package contains functions that are used as middleware on the server side.

Getting Started

package main

import (
	"fmt"
	"net/http"

	"github.com/gromey/proto-rest/logger"
	"github.com/gromey/proto-rest/middleware"
)

func main() {
	handlerFunc := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if _, err := fmt.Fprintln(w, "Hello World!"); err != nil {
			panic(err)
		}
	})

	http.Handle("/example/", handlerFunc)

	h := middleware.Sequencer(
		http.DefaultServeMux,
		middleware.DumpHttp(logger.LevelTrace),
		middleware.Timer(logger.LevelInfo),
		middleware.PanicCatcher,
	)

	if err := http.ListenAndServe(":8080", h); err != nil {
		panic(err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllowCORS added in v0.0.3

func AllowCORS(next http.Handler, opts *CORSOptions) http.Handler

AllowCORS sets headers for CORS mechanism supports secure.

func DumpHttp added in v0.0.3

func DumpHttp(logLevel logger.Level) func(http.Handler) http.Handler

DumpHttp dumps the HTTP request and response, and prints out with logFunc.

func PanicCatcher

func PanicCatcher(next http.Handler) http.Handler

PanicCatcher handles panics in http.HandlerFunc.

func Sequencer added in v0.1.1

func Sequencer(baseHandler http.Handler, mws ...func(http.Handler) http.Handler) http.Handler

Sequencer chains middleware functions in a chain.

func Timer

func Timer(logLevel logger.Level) func(http.Handler) http.Handler

Timer measures the time taken by http.HandlerFunc.

Types

type CORSOptions added in v0.0.3

type CORSOptions struct {
	AllowedOrigins   []string // The origins that the server allows.
	AllowMethods     []string // List of methods that the server allows.
	AllowHeaders     []string // List of headers that the server allows.
	MaxAge           int      // Tells the browser how long (in seconds) to cache the response to the preflight request.
	AllowCredentials bool     // Allow browsers to expose the response to the external JavaScript code.
}

CORSOptions represents a functional option for configuring the CORS middleware.

Jump to

Keyboard shortcuts

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