Skip to content

Refactors environment MVC #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
# lib/__init__.py
import logging
Copy link
Contributor

@coderabbitai coderabbitai bot May 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider removing or using the imported app from .api.app if it's not being used in this file.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm providing logging to the project upper level by importing it in __init__.py and then accessing the same import object from the other modules via from lib import logging

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GabrielBarberini, thank you for clarifying the setup of the logging configuration in lib/__init__.py. Centralizing the logging setup in this way is indeed a good practice as it ensures consistency and maintainability of logging across different modules of your application. If you encounter any issues or need further assistance with this setup, feel free to reach out. Happy coding!

from .api import app

logging.basicConfig(
level=logging.INFO,
filename='app.log',
filemode='a',
format='%(asctime)s - %(levelname)s - %(message)s',
)
12 changes: 8 additions & 4 deletions lib/api.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
"""
This is the main API file for the RocketPy API.
"""
import logging

from fastapi import FastAPI, Request, status
from fastapi.exceptions import RequestValidationError
from fastapi.middleware.cors import CORSMiddleware
from fastapi.openapi.utils import get_openapi
from fastapi.responses import RedirectResponse, JSONResponse

from lib import logging
from lib.routes import flight, environment, motor, rocket

logger = logging.getLogger(__name__)

app = FastAPI(
swagger_ui_parameters={
"defaultModelsExpandDepth": 0,
Expand All @@ -35,7 +37,7 @@ def custom_openapi():
return app.openapi_schema
openapi_schema = get_openapi(
title="RocketPy Infinity-API",
version="1.0.0 BETA",
version="1.1.0 BETA",
description=(
"<p style='font-size: 18px;'>RocketPy Infinity-API is a RESTful Open API for RocketPy, a rocket flight simulator.</p>"
"<br/>"
Expand Down Expand Up @@ -79,9 +81,11 @@ async def __perform_healthcheck():

# Errors
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request, exc: RequestValidationError):
async def validation_exception_handler(
request: Request, exc: RequestValidationError
):
exc_str = f"{exc}".replace("\n", " ").replace(" ", " ")
logging.error(f"{request}: {exc_str}")
logger.error(f"{request}: {exc_str}")
content = {"status_code": 10422, "message": exc_str, "data": None}
return JSONResponse(
content=content, status_code=status.HTTP_422_UNPROCESSABLE_ENTITY
Expand Down
6 changes: 6 additions & 0 deletions lib/controllers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# lib/controllers/__init__.py


def parse_error(e):
exc_str = f"{e}".replace("\n", " ").replace(" ", " ")
return exc_str
Loading