mirror of
https://github.com/usememos/memos.git
synced 2025-06-05 22:09:59 +02:00
chore: move rate limiter to apiv1
This commit is contained in:
19
api/v1/v1.go
19
api/v1/v1.go
@ -1,7 +1,11 @@
|
|||||||
package v1
|
package v1
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
|
"github.com/labstack/echo/v4/middleware"
|
||||||
|
|
||||||
"github.com/usememos/memos/api/resource"
|
"github.com/usememos/memos/api/resource"
|
||||||
"github.com/usememos/memos/plugin/telegram"
|
"github.com/usememos/memos/plugin/telegram"
|
||||||
@ -45,6 +49,21 @@ func (s *APIV1Service) Register(rootGroup *echo.Group) {
|
|||||||
|
|
||||||
// Register API v1 routes.
|
// Register API v1 routes.
|
||||||
apiV1Group := rootGroup.Group("/api/v1")
|
apiV1Group := rootGroup.Group("/api/v1")
|
||||||
|
apiV1Group.Use(middleware.RateLimiterWithConfig(middleware.RateLimiterConfig{
|
||||||
|
Store: middleware.NewRateLimiterMemoryStoreWithConfig(
|
||||||
|
middleware.RateLimiterMemoryStoreConfig{Rate: 30, Burst: 100, ExpiresIn: 3 * time.Minute},
|
||||||
|
),
|
||||||
|
IdentifierExtractor: func(ctx echo.Context) (string, error) {
|
||||||
|
id := ctx.RealIP()
|
||||||
|
return id, nil
|
||||||
|
},
|
||||||
|
ErrorHandler: func(context echo.Context, err error) error {
|
||||||
|
return context.JSON(http.StatusForbidden, nil)
|
||||||
|
},
|
||||||
|
DenyHandler: func(context echo.Context, identifier string, err error) error {
|
||||||
|
return context.JSON(http.StatusTooManyRequests, nil)
|
||||||
|
},
|
||||||
|
}))
|
||||||
apiV1Group.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
|
apiV1Group.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||||
return JWTMiddleware(s, next, s.Secret)
|
return JWTMiddleware(s, next, s.Secret)
|
||||||
})
|
})
|
||||||
|
@ -77,23 +77,6 @@ func NewServer(ctx context.Context, profile *profile.Profile, store *store.Store
|
|||||||
Timeout: 30 * time.Second,
|
Timeout: 30 * time.Second,
|
||||||
}))
|
}))
|
||||||
|
|
||||||
e.Use(middleware.RateLimiterWithConfig(middleware.RateLimiterConfig{
|
|
||||||
Skipper: grpcRequestSkipper,
|
|
||||||
Store: middleware.NewRateLimiterMemoryStoreWithConfig(
|
|
||||||
middleware.RateLimiterMemoryStoreConfig{Rate: 30, Burst: 100, ExpiresIn: 3 * time.Minute},
|
|
||||||
),
|
|
||||||
IdentifierExtractor: func(ctx echo.Context) (string, error) {
|
|
||||||
id := ctx.RealIP()
|
|
||||||
return id, nil
|
|
||||||
},
|
|
||||||
ErrorHandler: func(context echo.Context, err error) error {
|
|
||||||
return context.JSON(http.StatusForbidden, nil)
|
|
||||||
},
|
|
||||||
DenyHandler: func(context echo.Context, identifier string, err error) error {
|
|
||||||
return context.JSON(http.StatusTooManyRequests, nil)
|
|
||||||
},
|
|
||||||
}))
|
|
||||||
|
|
||||||
serverID, err := s.getSystemServerID(ctx)
|
serverID, err := s.getSystemServerID(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "failed to retrieve system server ID")
|
return nil, errors.Wrap(err, "failed to retrieve system server ID")
|
||||||
|
Reference in New Issue
Block a user