Skip to content

Commit 739d81b

Browse files
Setup GS backend directory structure (#313)
# Purpose Set up the gs backend directory structure. A lot of backend tasks are blocked until this gets merged. https://www.notion.so/uworbital/Setup-GS-backend-1898a26d767780149972cc87f7ca78d3 # New Changes - Create directory structure for gs backend - Add files that were relevant from the gs onboarding # Testing - mypy - `fastapi dev gs/backend/main.py` successfully creates a backend but there are no routes currently - There is no functionality being added so no additional tests are required # Outstanding Changes - Implement gs backend --------- Co-authored-by: William Chiu <williamrchiu18@gmail.com>
1 parent d01e12e commit 739d81b

29 files changed

+127
-2
lines changed

gs/__init__.py

Whitespace-only changes.

gs/backend/api/__init__.py

Whitespace-only changes.

gs/backend/api/backend_setup.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from fastapi import FastAPI
2+
3+
from gs.backend.api.middleware.auth_middleware import AuthMiddleware
4+
from gs.backend.api.middleware.cors_middleware import add_cors_middleware
5+
from gs.backend.api.middleware.logger_middleware import LoggerMiddleware
6+
from gs.backend.api.v1.aro.endpoints.picture_requests import picture_requests_router
7+
from gs.backend.api.v1.aro.endpoints.user import aro_user_router
8+
from gs.backend.api.v1.mcc.endpoints.aro_requests import aro_requests_router
9+
from gs.backend.api.v1.mcc.endpoints.commands import commands_router
10+
from gs.backend.api.v1.mcc.endpoints.telemetry import telemetry_router
11+
12+
13+
def setup_routes(app: FastAPI) -> None:
14+
"""Adds the routes to the app"""
15+
version_1 = "/api/v1"
16+
17+
# ARO routes
18+
aro_prefix = f"{version_1}/aro"
19+
app.include_router(aro_user_router, prefix=f"{aro_prefix}/user")
20+
app.include_router(picture_requests_router, prefix=f"{aro_prefix}/requests")
21+
22+
# MCC routes
23+
mcc_prefix = f"{version_1}/mcc"
24+
app.include_router(commands_router, prefix=f"{mcc_prefix}/commands")
25+
app.include_router(telemetry_router, prefix=f"{mcc_prefix}/telemetry")
26+
app.include_router(aro_requests_router, prefix=f"{mcc_prefix}/requests")
27+
28+
29+
def setup_middlewares(app: FastAPI) -> None:
30+
"""Adds the middlewares to the app"""
31+
add_cors_middleware(app) # Cors middleware should be added first
32+
app.add_middleware(AuthMiddleware)
33+
app.add_middleware(LoggerMiddleware)

gs/backend/api/lifespan.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from collections.abc import AsyncGenerator
2+
from contextlib import asynccontextmanager
3+
4+
from fastapi import FastAPI
5+
6+
7+
@asynccontextmanager
8+
async def lifespan(_: FastAPI) -> AsyncGenerator[None, None]:
9+
"""Lifecycle event for the FastAPI app."""
10+
yield

gs/backend/api/middleware/__init__.py

Whitespace-only changes.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from fastapi import Request, Response
2+
from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint
3+
4+
5+
class AuthMiddleware(BaseHTTPMiddleware):
6+
"""Middleware that is responsible for authenticating the user"""
7+
8+
async def dispatch(self, request: Request, call_next: RequestResponseEndpoint) -> Response:
9+
"""Authenticates the user"""
10+
# TODO: Implement authentication here
11+
response = await call_next(request)
12+
return response
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from fastapi import FastAPI
2+
from fastapi.middleware.cors import CORSMiddleware
3+
4+
5+
def add_cors_middleware(app: FastAPI) -> None:
6+
"""
7+
Adds the cors middleware to the FastAPI app
8+
9+
@param app: FastAPI app to add the middleware to
10+
"""
11+
app.add_middleware(
12+
CORSMiddleware,
13+
allow_origins=["http://localhost:5173"],
14+
allow_credentials=True,
15+
allow_methods=["*"],
16+
allow_headers=["*"],
17+
)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from fastapi import Request, Response
2+
from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint
3+
4+
5+
class LoggerMiddleware(BaseHTTPMiddleware):
6+
"""Middleware that logs the request and response"""
7+
8+
async def dispatch(self, request: Request, call_next: RequestResponseEndpoint) -> Response:
9+
"""Logs the request and response"""
10+
# TODO: Add logging here
11+
response = await call_next(request)
12+
return response

gs/backend/api/v1/__init__.py

Whitespace-only changes.

gs/backend/api/v1/aro/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)