post_limit

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2021 License: MIT Imports: 3 Imported by: 2

README

http-post-limit

HTTP Post Limit Middleware

Usage

package main

import (
	"net/http"

	post_limit "github.com/moisespsena-go/http-post-limit"
)

func main() {
	mux := http.NewServeMux()
	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("ok"))
	})
	
	http.ListenAndServe(":8000", post_limit.New(mux)) 
	// or
	md := post_limit.New(
		mux,
		&post_limit.Opts{
		    MaxPostSize: 1024,
		    ErrorHandler: func(w http.ResponseWriter, r *http.Request) {
		    	http.Error(w, "max post size exceeded", http.StatusBadRequest)
		    },
		},
	)
	http.ListenAndServe(":8000", md)
}

Documentation

Index

Constants

View Source
const (
	PostSizeKey contextKey = "post-size"
)

Variables

View Source
var (
	DefaultMaxPostSize            int64 = 1024 * 1024 * 10 // 10Mb
	DefaultPostLimitFailedHandler       = PostLimitFailedHandler(func(w http.ResponseWriter, r *http.Request) {
		http.Error(w, "max post size exceeded", http.StatusBadRequest)
	})
)

Functions

func MaxPostSizeOf

func MaxPostSizeOf(r *http.Request) int64

func ParseForm

func ParseForm(r *http.Request) (err error)

Types

type FormParser

type FormParser func(r *http.Request) (err error)

type Middleware

type Middleware struct {
	http.Handler
	*Opts
}

func New

func New(handler http.Handler, opt ...*Opts) *Middleware

func (*Middleware) ServeHttp

func (m *Middleware) ServeHttp(w http.ResponseWriter, r *http.Request)

type Opts

type Opts struct {
	MaxPostSize  int64
	ErrorHandler http.HandlerFunc
}

type PostLimitFailedHandler

type PostLimitFailedHandler = http.HandlerFunc

Jump to

Keyboard shortcuts

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