Skip to content
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

Major API refactoring #25

Merged
merged 9 commits into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
2 changes: 1 addition & 1 deletion .github/workflows/pylint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.11.5"]
python-version: ["3.12.5"]
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3.11-slim-bookworm
FROM python:3.12.5-slim-bookworm

EXPOSE 3000

Expand All @@ -16,4 +16,4 @@ RUN apt-get update && \

COPY ./lib /app/lib

CMD ["gunicorn", "-c", "lib/settings/gunicorn.py", "-w", "1", "--threads=2", "-k", "uvicorn.workers.UvicornWorker", "lib.api:app", "--log-level", "Debug", "-b", "0.0.0.0:3000", "--timeout", "30"]
CMD ["gunicorn", "-c", "lib/settings/gunicorn.py", "-w", "1", "--threads=2", "-k", "uvicorn.workers.UvicornWorker", "lib.api:app", "--log-level", "Debug", "-b", "0.0.0.0:3000", "--timeout", "60"]
21 changes: 21 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
black:
black ./lib

lint: flake8 pylint

flake8:
flake8 --ignore E501,E402,F401,W503 ./lib

pylint:
pylint --extension-pkg-whitelist='pydantic' ./lib/*

dev:
python3 -m uvicorn lib:app --reload --port 3000

clean:
docker stop infinity-api
docker rm infinity-api
docker system prune -fa

build:
docker build -t infinity-api . --no-cache
68 changes: 43 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
- [install mongodb-atlas](https://www.mongodb.com/try/download/community)
- Install dependencies `python3 -m pip install -r requirements.txt`

## Development
- black ./lib
- pylint --extension-pkg-whitelist='pydantic' ./lib/*
- flake8 --ignore E501,E402,F401,W503 ./lib
Comment on lines +13 to +15
Copy link
Member

Choose a reason for hiding this comment

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

Suggestion to create a Makefile with these commands.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

patched in 470eb1e


## Starting the server
- Setup MONGODB_CONNECTION_STRING:
```
Expand All @@ -18,7 +23,7 @@ $ touch .env && echo MONGODB_CONNECTION_STRING="$ConnectionString" > .env
### Docker
- run docker compose: `docker-compose up --build -d`

### Standard
### Standalone
- Dev: `python3 -m uvicorn lib:app --reload --port 3000`
- Prod: `gunicorn -k uvicorn.workers.UvicornWorker lib:app -b 0.0.0.0:3000`

Expand All @@ -38,6 +43,12 @@ $ touch .env && echo MONGODB_CONNECTION_STRING="$ConnectionString" > .env
│   │   ├── motor.py
│   │   └── rocket.py
│   │  
│   ├── services
│   │   ├── environment.py
│   │   ├── flight.py
│   │   ├── motor.py
│   │   └── rocket.py
│   │  
│   ├── routes
│   │   ├── environment.py
│   │   ├── flight.py
Expand All @@ -56,7 +67,6 @@ $ touch .env && echo MONGODB_CONNECTION_STRING="$ConnectionString" > .env
│   │   ├── environment.py
│   │   ├── flight.py
│   │   ├── motor.py
│   │   ├── parachute.py
│   │   └── rocket.py
│   │  
│   └── views
Expand All @@ -66,43 +76,51 @@ $ touch .env && echo MONGODB_CONNECTION_STRING="$ConnectionString" > .env
│   └── rocket.py
│  
└── tests
├── infinity-api-postman-collection.json
│  
├── integration
│   ├── test_environment_integration.py
│   ├── test_motor_integration.py
│   ├── test_rocket_integration.py
│   └── test_flight_integration.py
│  
└── unit
├── test_secrets.py
├── test_api.py
│  
├── test_controllers
│   ├── test_environment.py
│   ├── test_flight.py
│   ├── test_motor.py
│   └── test_rocket.py
│   ├── test_environment_controller.py
│   ├── test_flight_controller.py
│   ├── test_motor_controller.py
│   └── test_rocket_controller.py
│  
├── test_services
│   ├── test_environment_service.py
│   ├── test_flight_service.py
│   ├── test_motor_service.py
│   └── test_rocket_serice.py
├── test_routes
│   ├── test_environment.py
│   ├── test_flight.py
│   ├── test_motor.py
│   └── test_rocket.py
│   ├── test_environment_route.py
│   ├── test_flight_route.py
│   ├── test_motor_route.py
│   └── test_rocket_route.py
├── test_repositories
│   ├── test_environment.py
│   ├── test_flight.py
│   ├── test_motor.py
│   └── test_rocket.py
│   ├── test_environment_repo.py
│   ├── test_flight_repo.py
│   ├── test_motor_repo.py
│   └── test_rocket_repo.py
├── test_models
│   ├── test_environment.py
│   ├── test_flight.py
│   ├── test_motor.py
│   └── test_rocket.py
│   ├── test_environment_model.py
│   ├── test_flight_model.py
│   ├── test_motor_model.py
│   └── test_rocket_model.py
│  
└── test_views
   ├── test_environment.py
   ├── test_flight.py
   ├── test_motor.py
   └── test_rocket.py
   ├── test_environment_view.py
   ├── test_flight_view.py
   ├── test_motor_view.py
   └── test_rocket_view.py
```

## DOCS
Expand Down Expand Up @@ -149,7 +167,7 @@ sequenceDiagram
participant MongoDB
participant Rocketpy lib

User ->> API: POST /simulate/rocketpy-model/:id
User ->> API: POST /summary/rocketpy-model/:id
GabrielBarberini marked this conversation as resolved.
Show resolved Hide resolved
API -->> MongoDB: Retrieve Rocketpy Model
MongoDB -->> API: Rocketpy Model
API ->> Rocketpy lib: Simulate Rocketpy Model
Expand Down
5 changes: 4 additions & 1 deletion lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,7 @@
def parse_error(error):
exc_type = type(error).__name__
exc_obj = f"{error}".replace("\n", " ").replace(" ", " ")
return f"{exc_type} exception: {exc_obj}"
return f"{exc_type}: {exc_obj}"


from lib.api import app # pylint: disable=wrong-import-position,cyclic-import
GabrielBarberini marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion lib/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
from lib.api import app

if __name__ == '__main__':
app.run()
app.run() # pylint: disable=no-member
14 changes: 10 additions & 4 deletions lib/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@
from fastapi import FastAPI, Request, status
from fastapi.exceptions import RequestValidationError
from fastapi.middleware.cors import CORSMiddleware
from fastapi.middleware.gzip import GZipMiddleware
from fastapi.openapi.utils import get_openapi
from fastapi.responses import RedirectResponse, JSONResponse

from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
from opentelemetry.instrumentation.requests import RequestsInstrumentor

from lib import logger, parse_error
from lib.routes import flight, environment, motor, rocket
from lib.utils import RocketPyGZipMiddleware

app = FastAPI(
swagger_ui_parameters={
"defaultModelsExpandDepth": 0,
"syntaxHighlight.theme": "obsidian",
}
)

app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
Expand All @@ -30,16 +34,19 @@
app.include_router(motor.router)
app.include_router(rocket.router)

FastAPIInstrumentor.instrument_app(app)
RequestsInstrumentor().instrument()

# Compress responses above 1KB
app.add_middleware(GZipMiddleware, minimum_size=1000)
app.add_middleware(RocketPyGZipMiddleware, minimum_size=1000)


def custom_openapi():
if app.openapi_schema:
return app.openapi_schema
openapi_schema = get_openapi(
title="RocketPy Infinity-API",
version="1.2.0 BETA",
version="2.0.0",
description=(
"<p style='font-size: 18px;'>RocketPy Infinity-API is a RESTful Open API for RocketPy, a rocket flight simulator.</p>"
"<br/>"
Expand All @@ -51,7 +58,6 @@ def custom_openapi():
"<a href='https://api.rocketpy.org/redoc' style='color: white; text-decoration: none;'>ReDoc</a>"
"</button>"
"<p>Create, manage, and simulate rocket flights, environments, rockets, and motors.</p>"
"<p>Currently, the API only supports TrapezoidalFins. We apologize for the limitation, but we are actively working to expand its capabilities soon.</p>"
"<p>Please report any bugs at <a href='https://github.com/RocketPy-Team/infinity-api/issues/new/choose' style='text-decoration: none; color: #008CBA;'>GitHub Issues</a></p>"
),
routes=app.routes,
Expand Down
Loading
Loading