Skip to content

Commit 52b47c7

Browse files
committed
Fix: issue #385 - Resctructured to follow bigger apps layout
1 parent 8d0e44c commit 52b47c7

File tree

16 files changed

+48
-46
lines changed

16 files changed

+48
-46
lines changed

Dockerfile

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ COPY requirements.txt .
4545
RUN pip install --no-cache-dir --no-index --find-links /app/wheelhouse -r requirements.txt && \
4646
rm -rf /app/wheelhouse
4747

48-
# Copy application source code
49-
COPY main.py ./
50-
COPY databases/ ./databases/
51-
COPY models/ ./models/
52-
COPY routes/ ./routes/
53-
COPY schemas/ ./schemas/
54-
COPY services/ ./services/
48+
# Copy application source code
49+
COPY app/main.py ./
50+
COPY app/databases/ ./databases/
51+
COPY app/models/ ./models/
52+
COPY app/routes/ ./routes/
53+
COPY app/schemas/ ./schemas/
54+
COPY app/services/ ./services/
5555

5656
# https://rules.sonarsource.com/docker/RSPEC-6504/
5757

@@ -78,4 +78,4 @@ HEALTHCHECK --interval=30s --timeout=5s --start-period=5s --retries=3 \
7878
CMD ["./healthcheck.sh"]
7979

8080
ENTRYPOINT ["./entrypoint.sh"]
81-
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "9000"]
81+
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "9000"]
File renamed without changes.
File renamed without changes.
File renamed without changes.

main.py renamed to app/main.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import logging
1313
from typing import AsyncIterator
1414
from fastapi import FastAPI
15-
from routes import player_route, health_route
15+
from app.routes import player, health
1616

1717
# https://github.com/encode/uvicorn/issues/562
1818
UVICORN_LOGGER = "uvicorn.error"
@@ -35,5 +35,5 @@ async def lifespan(_: FastAPI) -> AsyncIterator[None]:
3535
version="1.0.0",
3636
)
3737

38-
app.include_router(player_route.api_router)
39-
app.include_router(health_route.api_router)
38+
app.include_router(player.router)
39+
app.include_router(health.router)
File renamed without changes.
File renamed without changes.
File renamed without changes.

routes/health_route.py renamed to app/routes/health.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77

88
from fastapi import APIRouter
99

10-
api_router = APIRouter()
10+
router = APIRouter()
1111

1212

13-
@api_router.get("/health", tags=["Health"])
13+
@router.get("/health", tags=["Health"])
1414
async def health_check():
1515
"""
1616
Simple health check endpoint.

routes/player_route.py renamed to app/routes/player.py

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,22 @@
2222
from sqlalchemy.ext.asyncio import AsyncSession
2323
from aiocache import SimpleMemoryCache
2424

25-
from databases.player_database import generate_async_session
26-
from models.player_model import PlayerModel
27-
from services import player_service
25+
from app.databases.player import generate_async_session
26+
from app.models.player import PlayerModel
27+
from app.services import player
2828

29-
api_router = APIRouter()
29+
router = APIRouter()
3030
simple_memory_cache = SimpleMemoryCache()
3131

3232
CACHE_KEY = "players"
3333
CACHE_TTL = 600 # 10 minutes
3434

35+
PLAYER_TITLE = "The ID of the Player"
36+
3537
# POST -------------------------------------------------------------------------
3638

3739

38-
@api_router.post(
40+
@router.post(
3941
"/players/",
4042
status_code=status.HTTP_201_CREATED,
4143
summary="Creates a new Player",
@@ -56,17 +58,17 @@ async def post_async(
5658
Raises:
5759
HTTPException: HTTP 409 Conflict error if the Player already exists.
5860
"""
59-
player = await player_service.retrieve_by_id_async(async_session, player_model.id)
60-
if player:
61+
player_res = await player.retrieve_by_id_async(async_session, player_model.id)
62+
if player_res:
6163
raise HTTPException(status_code=status.HTTP_409_CONFLICT)
62-
await player_service.create_async(async_session, player_model)
64+
await player.create_async(async_session, player_model)
6365
await simple_memory_cache.clear(CACHE_KEY)
6466

6567

6668
# GET --------------------------------------------------------------------------
6769

6870

69-
@api_router.get(
71+
@router.get(
7072
"/players/",
7173
response_model=List[PlayerModel],
7274
status_code=status.HTTP_200_OK,
@@ -88,21 +90,21 @@ async def get_all_async(
8890
players = await simple_memory_cache.get(CACHE_KEY)
8991
response.headers["X-Cache"] = "HIT"
9092
if not players:
91-
players = await player_service.retrieve_all_async(async_session)
93+
players = await player.retrieve_all_async(async_session)
9294
await simple_memory_cache.set(CACHE_KEY, players, ttl=CACHE_TTL)
9395
response.headers["X-Cache"] = "MISS"
9496
return players
9597

9698

97-
@api_router.get(
99+
@router.get(
98100
"/players/{player_id}",
99101
response_model=PlayerModel,
100102
status_code=status.HTTP_200_OK,
101103
summary="Retrieves a Player by its Id",
102104
tags=["Players"],
103105
)
104106
async def get_by_id_async(
105-
player_id: int = Path(..., title="The ID of the Player"),
107+
player_id: int = Path(..., title=PLAYER_TITLE),
106108
async_session: AsyncSession = Depends(generate_async_session),
107109
):
108110
"""
@@ -119,13 +121,13 @@ async def get_by_id_async(
119121
HTTPException: Not found error if the Player with the specified ID does not
120122
exist.
121123
"""
122-
player = await player_service.retrieve_by_id_async(async_session, player_id)
123-
if not player:
124+
player_res = await player.retrieve_by_id_async(async_session, player_id)
125+
if not player_res:
124126
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
125-
return player
127+
return player_res
126128

127129

128-
@api_router.get(
130+
@router.get(
129131
"/players/squadnumber/{squad_number}",
130132
response_model=PlayerModel,
131133
status_code=status.HTTP_200_OK,
@@ -150,25 +152,25 @@ async def get_by_squad_number_async(
150152
HTTPException: HTTP 404 Not Found error if the Player with the specified
151153
Squad Number does not exist.
152154
"""
153-
player = await player_service.retrieve_by_squad_number_async(
155+
player_res = await player.retrieve_by_squad_number_async(
154156
async_session, squad_number
155157
)
156-
if not player:
158+
if not player_res:
157159
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
158-
return player
160+
return player_res
159161

160162

161163
# PUT --------------------------------------------------------------------------
162164

163165

164-
@api_router.put(
166+
@router.put(
165167
"/players/{player_id}",
166168
status_code=status.HTTP_204_NO_CONTENT,
167169
summary="Updates an existing Player",
168170
tags=["Players"],
169171
)
170172
async def put_async(
171-
player_id: int = Path(..., title="The ID of the Player"),
173+
player_id: int = Path(..., title=PLAYER_TITLE),
172174
player_model: PlayerModel = Body(...),
173175
async_session: AsyncSession = Depends(generate_async_session),
174176
):
@@ -185,24 +187,24 @@ async def put_async(
185187
HTTPException: HTTP 404 Not Found error if the Player with the specified ID
186188
does not exist.
187189
"""
188-
player = await player_service.retrieve_by_id_async(async_session, player_id)
189-
if not player:
190+
player_res = await player.retrieve_by_id_async(async_session, player_id)
191+
if not player_res:
190192
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
191-
await player_service.update_async(async_session, player_model)
193+
await player.update_async(async_session, player_model)
192194
await simple_memory_cache.clear(CACHE_KEY)
193195

194196

195197
# DELETE -----------------------------------------------------------------------
196198

197199

198-
@api_router.delete(
200+
@router.delete(
199201
"/players/{player_id}",
200202
status_code=status.HTTP_204_NO_CONTENT,
201203
summary="Deletes an existing Player",
202204
tags=["Players"],
203205
)
204206
async def delete_async(
205-
player_id: int = Path(..., title="The ID of the Player"),
207+
player_id: int = Path(..., title=PLAYER_TITLE),
206208
async_session: AsyncSession = Depends(generate_async_session),
207209
):
208210
"""
@@ -216,8 +218,8 @@ async def delete_async(
216218
HTTPException: HTTP 404 Not Found error if the Player with the specified ID
217219
does not exist.
218220
"""
219-
player = await player_service.retrieve_by_id_async(async_session, player_id)
220-
if not player:
221+
player_res = await player.retrieve_by_id_async(async_session, player_id)
222+
if not player_res:
221223
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
222-
await player_service.delete_async(async_session, player_id)
224+
await player.delete_async(async_session, player_id)
223225
await simple_memory_cache.clear(CACHE_KEY)
File renamed without changes.

schemas/player_schema.py renamed to app/schemas/player.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"""
88

99
from sqlalchemy import Column, String, Integer, Boolean
10-
from databases.player_database import Base
10+
from app.databases.player import Base
1111

1212

1313
class Player(Base):

app/services/__init__.py

Whitespace-only changes.

services/player_service.py renamed to app/services/player.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
from sqlalchemy import select
1616
from sqlalchemy.ext.asyncio import AsyncSession
1717
from sqlalchemy.exc import SQLAlchemyError
18-
from models.player_model import PlayerModel
19-
from schemas.player_schema import Player
18+
from app.models.player import PlayerModel
19+
from app.schemas.player import Player
2020

2121
# Create -----------------------------------------------------------------------
2222

storage/players-sqlite3.db

0 Bytes
Binary file not shown.

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import warnings
22
import pytest
33
from fastapi.testclient import TestClient
4-
from main import app
4+
from app.main import app
55

66
# Suppress the DeprecationWarning from httpx
77
warnings.filterwarnings("ignore", category=DeprecationWarning)

0 commit comments

Comments
 (0)