From 3e40ab65f1e9981647e09f8e70288cb1907afca4 Mon Sep 17 00:00:00 2001 From: Vojtech Vitek Date: Thu, 25 Jul 2024 15:49:32 +0200 Subject: [PATCH] Improve README --- README.md | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 13edb62..0efaa1a 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,50 @@ # httprate-redis -httprate.LimitCounter implementation with Redis backend +![CI workflow](https://github.com/go-chi/httprate-redis/actions/workflows/ci.yml/badge.svg) +![Benchmark workflow](https://github.com/go-chi/httprate-redis/actions/workflows/benchmark.yml/badge.svg) +[![GoDoc Widget]][GoDoc] -See _example/main.go for usage. +[GoDoc]: https://pkg.go.dev/github.com/go-chi/httprate-redis +[GoDoc Widget]: https://godoc.org/github.com/go-chi/httprate-redis?status.svg + +Redis backend for [github.com/go-chi/httprate](https://github.com/go-chi/httprate), implementing `httprate.LimitCounter` interface. + +See [_example/main.go](./_example/main.go) for usage. + +## Example + +```go +package main + +import ( + "net/http" + + "github.com/go-chi/chi/v5" + "github.com/go-chi/chi/v5/middleware" + "github.com/go-chi/httprate" + httprateredis "github.com/go-chi/httprate-redis" +) + +func main() { + r := chi.NewRouter() + r.Use(middleware.Logger) + + r.Use(httprate.Limit( + 5, + time.Minute, + httprate.WithKeyByIP(), + httprateredis.WithRedisLimitCounter(&httprateredis.Config{ + Host: "127.0.0.1", Port: 6379, + }), + )) + + r.Get("/", func(w http.ResponseWriter, r *http.Request) { + w.Write([]byte("This is IP rate-limited by 5 req/min")) + }) + + http.ListenAndServe(":3333", r) +} +``` ## LICENSE