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
14 changes: 7 additions & 7 deletions checkpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import (
"net/http"
)

func Limit(maxTokens, refillRate, tokensPerRefill int) func(next http.Handler) http.Handler {
return NewTokenBucket(maxTokens, refillRate, tokensPerRefill).Handler
func Limit(maxTokens, refillRate, tokensPerRefill int, config Config) func(next http.Handler) http.Handler {
return NewTokenBucket(maxTokens, refillRate, tokensPerRefill, config).Handler
}

func LimitByIp(maxTokens, refillRate, tokensPerRefill int) func(next http.Handler) http.Handler {
return Limit(maxTokens, refillRate, tokensPerRefill)
func LimitByIp(maxTokens, refillRate, tokensPerRefill int, config Config) func(next http.Handler) http.Handler {
return Limit(maxTokens, refillRate, tokensPerRefill, config)
}
func LimitIpByEndpoint(maxTokens, refillRate, tokensPerRefill int) func(next http.Handler) http.Handler {
return Limit(maxTokens, refillRate, tokensPerRefill)
func LimitIpByEndpoint(maxTokens, refillRate, tokensPerRefill int, config Config) func(next http.Handler) http.Handler {
return Limit(maxTokens, refillRate, tokensPerRefill, config)
}

func WithConfig(config Config) func(next http.Handler) http.Handler {
return config.LimitMethod(config.MaxTokens, config.RefillRate, config.TokensPerRefill)
return config.LimitMethod(config.MaxTokens, config.RefillRate, config.TokensPerRefill, config)
}
6 changes: 5 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ func main() {
fmt.Fprintln(w, "Hello World")
})

http.HandleFunc("/logs", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "logs page")
})

// CONFIG METHOD ==
config := checkpoint.Config{
IgnorePaths: []string{"/logs"},
Expand All @@ -23,7 +27,7 @@ func main() {
}

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

rlMiddleware := checkpoint.WithConfig(config)
rlHandler := rlMiddleware(http.DefaultServeMux)
Expand Down
2 changes: 1 addition & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ type Config struct {
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
LimitMethod func(maxTokens, refillRate, tokensPerRefill int, config Config) func(next http.Handler) http.Handler
}
41 changes: 41 additions & 0 deletions paths.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package checkpoint

import "strings"

func MatchPathPattern(pattern, path string) bool {
if pattern == path {
return true
}

if strings.Contains(pattern, "*") {
parts := strings.Split(pattern, "*")

// does path start with the prefix?
if !strings.HasPrefix(path, parts[0]) {
return false
}

if len(parts) == 2 {
return strings.HasSuffix(path, parts[1])
}
currentIndex := len(parts[0])
for i := 1; i < len(parts)-1; i++ {
if parts[i] == "" {
continue
}

index := strings.Index(path[currentIndex:], parts[i])
if index == -1 {
return false
}

currentIndex += index + len(parts[i])
}
lastPart := parts[len(parts)-1]
if lastPart != "" {
return strings.HasSuffix(path, lastPart)
}
return true
}
return false
}
Loading
Loading