Skip to content

didip/tollbooth_negroni

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 

Repository files navigation

tollbooth_negroni

Negroni middleware for rate limiting HTTP requests.

Five Minutes Tutorial

package main

import (
    "github.com/urfave/negroni"
    "github.com/didip/tollbooth"
    "github.com/didip/tollbooth_negroni"
    "net/http"
    "time"
)

func HelloHandler() http.Handler {
    handleFunc := func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("Hello, world!"))
    }

    return http.HandlerFunc(handleFunc)
}

func main() {
    // Create a limiter struct.
    limiter := tollbooth.NewLimiter(1, time.Second, nil)

    mux := http.NewServeMux()

    mux.Handle("/", negroni.New(
        tollbooth_negroni.LimitHandler(limiter),
        negroni.Wrap(HelloHandler()),
    ))

    n := negroni.Classic()
    n.UseHandler(mux)
    n.Run(":12345")
}

About

Tollbooth - Negroni integration layer

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages