MCPcopy
hub / github.com/labstack/echo / ToMiddleware

Method ToMiddleware

middleware/rate_limiter.go:124–167  ·  view source on GitHub ↗

ToMiddleware converts RateLimiterConfig to middleware or returns an error for invalid configuration

()

Source from the content-addressed store, hash-verified

122
123// ToMiddleware converts RateLimiterConfig to middleware or returns an error for invalid configuration
124func (config RateLimiterConfig) ToMiddleware() (echo.MiddlewareFunc, error) {
125 if config.Skipper == nil {
126 config.Skipper = DefaultRateLimiterConfig.Skipper
127 }
128 if config.IdentifierExtractor == nil {
129 config.IdentifierExtractor = DefaultRateLimiterConfig.IdentifierExtractor
130 }
131 if config.ErrorHandler == nil {
132 config.ErrorHandler = DefaultRateLimiterConfig.ErrorHandler
133 }
134 if config.DenyHandler == nil {
135 config.DenyHandler = DefaultRateLimiterConfig.DenyHandler
136 }
137 if config.Store == nil {
138 return nil, errors.New("echo rate limiter store configuration must be provided")
139 }
140 return func(next echo.HandlerFunc) echo.HandlerFunc {
141 return func(c *echo.Context) error {
142 if config.Skipper(c) {
143 return next(c)
144 }
145 if config.BeforeFunc != nil {
146 config.BeforeFunc(c)
147 }
148
149 identifier, err := config.IdentifierExtractor(c)
150 if err != nil {
151 return config.ErrorHandler(c, err)
152 }
153
154 var allow bool
155 var allowErr error
156 if sc, ok := config.Store.(RateLimiterStoreContext); ok {
157 allow, allowErr = sc.AllowContext(c, identifier)
158 } else {
159 allow, allowErr = config.Store.Allow(identifier)
160 }
161 if !allow {
162 return config.DenyHandler(c, identifier, allowErr)
163 }
164 return next(c)
165 }
166 }, nil
167}
168
169// RateLimiterMemoryStore is the built-in store implementation for RateLimiter
170type RateLimiterMemoryStore struct {

Callers

nothing calls this directly

Calls 2

AllowContextMethod · 0.65
AllowMethod · 0.65

Tested by

no test coverage detected