Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions checkpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ func LimitByIp(maxTokens, refillRate, tokensPerRefill int) func(next http.Handle
func LimitIpByEndpoint(maxTokens, refillRate, tokensPerRefill int) func(next http.Handler) http.Handler {
return Limit(maxTokens, refillRate, tokensPerRefill)
}

func WithConfig(config Config) func(next http.Handler) http.Handler {
return config.LimitMethod(config.MaxTokens, config.RefillRate, config.TokensPerRefill)
}
20 changes: 16 additions & 4 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,30 @@ package main
import (
"fmt"
"net/http"
_ "net/http/pprof"

checkpoint "github.com/aidenfine/checkpoint"
)

func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello World")
})

rlMiddleware := checkpoint.LimitByIp(20, 1, 1)
rlHandler := rlMiddleware(mux)
// CONFIG METHOD ==
config := checkpoint.Config{
IgnorePaths: []string{"/logs"},
MaxTokens: 25,
RefillRate: 1,
TokensPerRefill: 1,
LimitMethod: checkpoint.LimitByIp,
}

// Quick method
// rlMiddleware := checkpoint.LimitByIp(25, 1, 1)

rlMiddleware := checkpoint.WithConfig(config)
rlHandler := rlMiddleware(http.DefaultServeMux)

http.ListenAndServe(":8080", rlHandler)
}
11 changes: 11 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package checkpoint

import "net/http"

type Config struct {
IgnorePaths []string `json:"ignorePaths" yaml:"ignorePaths"`
MaxTokens int `json:"maxTokens" yaml:"maxTokens"`
RefillRate int `json:"refillRate" yaml:"refillRate"`
TokensPerRefill int `json:"tokensPerRefill" yaml:"tokensPerRefill"`
LimitMethod func(maxTokens, refillRate, tokensPerRefill int) func(next http.Handler) http.Handler
}
1 change: 1 addition & 0 deletions config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package checkpoint_test
Loading