Skip to content

Instantly share code, notes, and snippets.

@hotafrika
Created November 9, 2022 12:25
Show Gist options
  • Save hotafrika/daba1b77d08146e65d1bcc6025996783 to your computer and use it in GitHub Desktop.
Save hotafrika/daba1b77d08146e65d1bcc6025996783 to your computer and use it in GitHub Desktop.
medium / chi rate limiter
func main() {
router := chi.NewRouter()
router.Use(httprate.LimitByIP(100, time.Minute))
router.Get("/info", infoHandler)
router.Group(func(router chi.Router) {
router.Use(httprate.Limit(20, time.Minute, httprate.WithKeyFuncs(
httprate.KeyByIP,
httprate.KeyByEndpoint,
)))
router.Use(authMiddleware)
router.Get("/profile", profileHandler)
router.Get("/stat", statHandler)
})
_ = http.ListenAndServe(":8080", router)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment