Skip to content

Commit f7c48c9

Browse files
committed
refactor: reorganize limiter setup to dedicated services/limiter.py file and update imports across modules
1 parent 2c15b03 commit f7c48c9

File tree

6 files changed

+16
-10
lines changed

6 files changed

+16
-10
lines changed

app/main.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
from contextlib import asynccontextmanager
33

44
from fastapi import FastAPI
5-
from slowapi import Limiter, _rate_limit_exceeded_handler
6-
from slowapi.util import get_remote_address
5+
from slowapi import _rate_limit_exceeded_handler
76

87
from app.routers.router import setup_router as setup_router_v2
98
from app.services.database.database import AsyncSessionLocal, init_db
9+
from app.services.limiter import limiter
1010

1111
logger = logging.getLogger(__name__)
1212

@@ -22,7 +22,7 @@ async def lifespan(app: FastAPI):
2222
pass
2323

2424

25-
limiter = Limiter(key_func=get_remote_address)
25+
appLimiter = limiter
2626

2727

2828
app = FastAPI(
@@ -32,7 +32,7 @@ async def lifespan(app: FastAPI):
3232
)
3333

3434

35-
app.state.limiter = limiter
35+
app.state.limiter = appLimiter
3636
app.add_exception_handler(429, _rate_limit_exceeded_handler)
3737

3838
app.include_router(setup_router_v2(), prefix="/api")

app/routers/authentication.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,15 @@
44
from fastapi import APIRouter, Depends, HTTPException, Request, status
55
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
66
from jwt.exceptions import InvalidTokenError
7-
from slowapi import Limiter
8-
from slowapi.util import get_remote_address
97
from sqlmodel.ext.asyncio.session import AsyncSession
108

119
from app.schemas import Community, Token, TokenPayload
1210
from app.services import auth
1311
from app.services.database.models import Community as DBCommunity
1412
from app.services.database.orm.community import get_community_by_username
13+
from app.services.limiter import limiter
1514

1615
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/authentication/token")
17-
limiter = Limiter(key_func=get_remote_address)
1816

1917

2018
def setup():
@@ -115,6 +113,7 @@ async def login_for_access_token(
115113
@router.get("/me", response_model=Community)
116114
@limiter.limit("60/minute")
117115
async def read_community_me(
116+
request: Request,
118117
current_community: Annotated[
119118
DBCommunity, Depends(get_current_active_community)
120119
],

app/routers/news/routes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from fastapi import APIRouter, Request, status
22
from pydantic import BaseModel
3-
from services.database.orm.news import get_news_by_query_params
3+
4+
from app.services.database.orm.news import get_news_by_query_params
45

56

67
class NewsPostResponse(BaseModel):

app/services/database/models/subscriptions.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
from typing import List, Optional
22

3-
from schemas import SubscriptionTagEnum
43
from sqlalchemy import JSON, Column
54
from sqlmodel import Field, SQLModel
65

6+
from app.schemas import SubscriptionTagEnum
7+
78

89
class Subscription(SQLModel, table=True):
910
__tablename__ = "subscriptions" # type: ignore

app/services/database/orm/news.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
from typing import Optional
22

3-
from services.database.models import News
43
from sqlmodel import select
54
from sqlmodel.ext.asyncio.session import AsyncSession
65

6+
from app.services.database.models import News
7+
78

89
async def get_news_by_query_params(
910
session: AsyncSession,

app/services/limiter.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from slowapi import Limiter
2+
from slowapi.util import get_remote_address
3+
4+
limiter = Limiter(key_func=get_remote_address)

0 commit comments

Comments
 (0)