From 9f891f19640cb63df4af43b1f465e9c6e1abbf7e Mon Sep 17 00:00:00 2001 From: Samuel Huestis Date: Wed, 12 Mar 2025 12:52:05 -0600 Subject: [PATCH 1/6] version and description pulled from toml --- src/main.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/main.py b/src/main.py index e6aada7..85786f1 100644 --- a/src/main.py +++ b/src/main.py @@ -2,8 +2,16 @@ from fastapi.responses import RedirectResponse from routers.health import health_router from routers.mtcars import data_output -from docs.documentation import description import uvicorn +import tomllib + +try: + with open("pyproject.toml","rb") as f: + tomldata = tomllib.load(f) + version = tomldata["project"]["version"] + description = tomldata["project"]["description"] +except FileNotFoundError: + print("pyproject.toml not found") app = FastAPI( # This info goes directly into /docs @@ -11,7 +19,7 @@ # Description of API defined in docs/documentation.py for ease of reading description=description, summary="This project is a proof-of-concept (POC) web API built using the FastAPI library.", - version="0.0.1", + version=version, contact={ "name": "RMI", "url": "https://github.com/RMI", From 97609d36976859fa9b8c6757dfaf94feb7ba4d1c Mon Sep 17 00:00:00 2001 From: Samuel Huestis Date: Wed, 12 Mar 2025 12:54:33 -0600 Subject: [PATCH 2/6] black reformatting --- src/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main.py b/src/main.py index 85786f1..3cc976d 100644 --- a/src/main.py +++ b/src/main.py @@ -5,8 +5,8 @@ import uvicorn import tomllib -try: - with open("pyproject.toml","rb") as f: +try: + with open("pyproject.toml", "rb") as f: tomldata = tomllib.load(f) version = tomldata["project"]["version"] description = tomldata["project"]["description"] From db149ad406856beffc1234d477b66066515ff189 Mon Sep 17 00:00:00 2001 From: Samuel Huestis Date: Wed, 12 Mar 2025 15:11:32 -0600 Subject: [PATCH 3/6] moved main.py to root folder, dockerfile explicitly loads src --- Dockerfile | 5 +++-- src/main.py => main.py | 13 +++++++++---- 2 files changed, 12 insertions(+), 6 deletions(-) rename src/main.py => main.py (83%) diff --git a/Dockerfile b/Dockerfile index 1a7532e..5f83702 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,7 +8,8 @@ WORKDIR /app # Copy the current directory contents into the container at /app COPY . /app - +COPY src /app/ + # Install dependencies RUN uv sync --frozen --no-install-project @@ -16,4 +17,4 @@ RUN uv sync --frozen --no-install-project RUN uv sync --frozen # Command to run the app when the container starts -CMD ["uv", "run", "src/main.py"] +CMD ["uv", "run", "main.py"] diff --git a/src/main.py b/main.py similarity index 83% rename from src/main.py rename to main.py index 3cc976d..2a6a73a 100644 --- a/src/main.py +++ b/main.py @@ -1,12 +1,17 @@ from fastapi import FastAPI from fastapi.responses import RedirectResponse -from routers.health import health_router -from routers.mtcars import data_output +from src.routers.health import health_router +from src.routers.mtcars import data_output import uvicorn import tomllib +import sys -try: - with open("pyproject.toml", "rb") as f: +sys.path.append('../') + + +# Import pyproject toml info using tomllib +try: + with open("pyproject.toml","rb") as f: tomldata = tomllib.load(f) version = tomldata["project"]["version"] description = tomldata["project"]["description"] From 521c31efc2dd90d26d5da1f7bf3c703c7b9e3d17 Mon Sep 17 00:00:00 2001 From: Samuel Huestis Date: Tue, 18 Mar 2025 15:52:02 -0600 Subject: [PATCH 4/6] same as srcsplit but retaining src/main.py (#38) --- Dockerfile | 2 +- test_main.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 test_main.py diff --git a/Dockerfile b/Dockerfile index 5f83702..32a2918 100644 --- a/Dockerfile +++ b/Dockerfile @@ -17,4 +17,4 @@ RUN uv sync --frozen --no-install-project RUN uv sync --frozen # Command to run the app when the container starts -CMD ["uv", "run", "main.py"] +CMD ["uv", "run", "test_main.py"] diff --git a/test_main.py b/test_main.py new file mode 100644 index 0000000..e6aada7 --- /dev/null +++ b/test_main.py @@ -0,0 +1,37 @@ +from fastapi import FastAPI +from fastapi.responses import RedirectResponse +from routers.health import health_router +from routers.mtcars import data_output +from docs.documentation import description +import uvicorn + +app = FastAPI( + # This info goes directly into /docs + title="RMI Web API poc", + # Description of API defined in docs/documentation.py for ease of reading + description=description, + summary="This project is a proof-of-concept (POC) web API built using the FastAPI library.", + version="0.0.1", + contact={ + "name": "RMI", + "url": "https://github.com/RMI", + }, + license_info={ + "name": "MIT", + "url": "https://github.com/RMI/web-api-poc/blob/main/LICENSE.txt", + }, +) + + +@app.get("/") +async def redirect(): + response = RedirectResponse(url="/docs") + return response + + +app.include_router(health_router) +app.include_router(data_output) + + +if __name__ == "__main__": + uvicorn.run("main:app", host="0.0.0.0", port=5000, log_level="info") From 9e4e73f1f9b2e194aa4270943fe82d83dcf68139 Mon Sep 17 00:00:00 2001 From: Samuel Huestis Date: Tue, 18 Mar 2025 16:43:06 -0600 Subject: [PATCH 5/6] removed main.py from root, added to src --- main.py => src/main.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename main.py => src/main.py (100%) diff --git a/main.py b/src/main.py similarity index 100% rename from main.py rename to src/main.py From 723c4a25ce105d2e50c1f6ba906e2fd93d41f946 Mon Sep 17 00:00:00 2001 From: Samuel Huestis Date: Tue, 18 Mar 2025 16:50:03 -0600 Subject: [PATCH 6/6] black reformatting --- src/main.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main.py b/src/main.py index 2a6a73a..34257b2 100644 --- a/src/main.py +++ b/src/main.py @@ -6,12 +6,12 @@ import tomllib import sys -sys.path.append('../') +sys.path.append("../") # Import pyproject toml info using tomllib -try: - with open("pyproject.toml","rb") as f: +try: + with open("pyproject.toml", "rb") as f: tomldata = tomllib.load(f) version = tomldata["project"]["version"] description = tomldata["project"]["description"]