Skip to content

armgit / memos

Public Imported from GitHub
0

Memos is an open-source, self-hosted note-taking app that brings your everyday thoughts together in a personal timeline. Your data stays yours, and you choose what to keep private or share.

chore: move rate limiter to apiv1

Stevenab2c86640b90
diff --git a/api/v1/v1.go b/api/v1/v1.go
index 76c87dfd0..78d6b1ac6 100644
--- a/api/v1/v1.go
+++ b/api/v1/v1.go
@@ -1,7 +1,11 @@
package v1
import (
+ "net/http"
+ "time"
+
"github.com/labstack/echo/v4"
+ "github.com/labstack/echo/v4/middleware"
"github.com/usememos/memos/api/resource"
"github.com/usememos/memos/plugin/telegram"
@@ -45,6 +49,21 @@ func (s *APIV1Service) Register(rootGroup *echo.Group) {
// Register API v1 routes.
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 {
return JWTMiddleware(s, next, s.Secret)
})
diff --git a/server/server.go b/server/server.go
index 09bff45b7..d8a6f35e7 100644
--- a/server/server.go
+++ b/server/server.go
@@ -77,23 +77,6 @@ func NewServer(ctx context.Context, profile *profile.Profile, store *store.Store
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)
if err != nil {
return nil, errors.Wrap(err, "failed to retrieve system server ID")