From 70e0b040715cb804d32b607111fe89be961436a3 Mon Sep 17 00:00:00 2001 From: Heisenberg208 Date: Tue, 10 Mar 2026 20:51:07 +0530 Subject: [PATCH 1/4] Refactor port handling in CLI and API startup functions --- src/sample/__main__.py | 11 ++++------- src/sample/api/main.py | 5 ++--- src/sample/api/routes.py | 9 +++++---- src/sample/cli.py | 23 +++++++++++++++-------- src/sample/utils/constants.py | 1 - 5 files changed, 26 insertions(+), 23 deletions(-) diff --git a/src/sample/__main__.py b/src/sample/__main__.py index ab1001d..12b6440 100644 --- a/src/sample/__main__.py +++ b/src/sample/__main__.py @@ -1,13 +1,10 @@ -from sample.utils.constants import PORT - - -def main(): +def main(port: int = 8501): """Entry point for CLI dev command.""" from sample.api.main import start - print(f"🚀 Starting Sample app on port {PORT}...\n") - start() + print(f"🚀 Starting Sample app on port {port}...\n") + start(port) if __name__ == "__main__": - main() + main(port=8501) diff --git a/src/sample/api/main.py b/src/sample/api/main.py index e1bcef2..1494d02 100644 --- a/src/sample/api/main.py +++ b/src/sample/api/main.py @@ -9,7 +9,6 @@ from sample.api.routes import greet_router from sample.api.web_page import web_router from sample.db.connection import connect_db -from sample.utils.constants import PORT @asynccontextmanager @@ -44,7 +43,7 @@ async def lifespan(app: FastAPI): app.include_router(greet_router) -def start(): +def start(port: int = 5000): import uvicorn - uvicorn.run("sample.api.main:app", host="127.0.0.1", port=PORT, reload=True) + uvicorn.run("sample.api.main:app", host="127.0.0.1", port=port, reload=True) diff --git a/src/sample/api/routes.py b/src/sample/api/routes.py index 2d43ff0..8e5327f 100644 --- a/src/sample/api/routes.py +++ b/src/sample/api/routes.py @@ -1,9 +1,10 @@ from fastapi import APIRouter, FastAPI, HTTPException, Request from pydantic import BaseModel -from sample.utils.constants import API_PREFIX, GREETING, PORT -from sample.utils.helper import normalize_name from starlette.responses import HTMLResponse, JSONResponse + from sample import __version__ +from sample.utils.constants import API_PREFIX, GREETING +from sample.utils.helper import normalize_name app = FastAPI( title="Sample API", @@ -98,7 +99,7 @@ def get_help(): app.include_router(greet_router) -def start(): +def start(port: int = 5000): import uvicorn - uvicorn.run("sample.api.routes:app", host="127.0.0.1", port=PORT, reload=True) + uvicorn.run("sample.api.routes:app", host="127.0.0.1", port=port, reload=True) diff --git a/src/sample/cli.py b/src/sample/cli.py index c0a0746..96c5207 100644 --- a/src/sample/cli.py +++ b/src/sample/cli.py @@ -13,17 +13,24 @@ def cli(ctx, version): ctx.exit() -@cli.command() -def dev(): - """Run the Sample app.""" +@cli.command(help="Port to run the Sample app on.") +@click.option( + "--port", default=8501, show_default=True, help="Port to run the Sample app on." +) +def dev(port: int): from sample.__main__ import main - main() + main(port) -@cli.command() -def api(): - """Run the Sample FastAPI backend.""" +@cli.command(help="Run the Sample FastAPI backend.") +@click.option( + "--port", + default=5000, + show_default=True, + help="Port to run the FastAPI backend on.", +) +def api(port: int): from sample.api.routes import start - start() + start(port) diff --git a/src/sample/utils/constants.py b/src/sample/utils/constants.py index 15b54c1..b7416bb 100644 --- a/src/sample/utils/constants.py +++ b/src/sample/utils/constants.py @@ -31,7 +31,6 @@ def safe_get(env_key: str, default) -> str: API_PREFIX = safe_get("API_PREFIX", DEFAULT_API_PREFIX) -PORT = int(safe_get("PORT", DEFAULT_PORT)) def get_mongo_config(): From 59812ba4758164c9324cc6a40ca7f8ce6a753fd6 Mon Sep 17 00:00:00 2001 From: Heisenberg208 Date: Tue, 10 Mar 2026 20:53:12 +0530 Subject: [PATCH 2/4] Add custom port options for running sample app and FastAPI server --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index b653ca9..788dbc0 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,8 @@ poetry install ```bash poetry run sample dev +# with custom port +poetry run sample dev --port 8501 ``` Access: @@ -63,6 +65,8 @@ Access: poetry run sample api # OR python src/api/fast_api.py +# with custom port +poetry run sample dev --port 5000 ``` Access: From 0eb309c03fc29557422b10154624ab984c98adda Mon Sep 17 00:00:00 2001 From: Heisenberg208 Date: Tue, 10 Mar 2026 21:34:20 +0530 Subject: [PATCH 3/4] Add environment variable support for PORT and PORT_API in configuration --- .env.local | 3 ++- src/sample/cli.py | 9 +++++++-- src/sample/utils/constants.py | 4 ++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/.env.local b/.env.local index c25c366..a4f0d34 100644 --- a/.env.local +++ b/.env.local @@ -1,5 +1,6 @@ #Note specify the values within double-quotes("") ENVIRONMENT=development - +PORT=8501 +PORT_API=8502 MONGODB_URI="mongodb://:@127.0.0.1:27017/?retryWrites=true&w=majority&appName=sample" DATABASE_NAME="sample" diff --git a/src/sample/cli.py b/src/sample/cli.py index 96c5207..e4e1efc 100644 --- a/src/sample/cli.py +++ b/src/sample/cli.py @@ -1,5 +1,7 @@ import click +from sample.utils.constants import PORT, PORT_API + from . import __version__ @@ -15,7 +17,10 @@ def cli(ctx, version): @cli.command(help="Port to run the Sample app on.") @click.option( - "--port", default=8501, show_default=True, help="Port to run the Sample app on." + "--port", + default=int(PORT), + show_default=True, + help="Port to run the Sample app on.", ) def dev(port: int): from sample.__main__ import main @@ -26,7 +31,7 @@ def dev(port: int): @cli.command(help="Run the Sample FastAPI backend.") @click.option( "--port", - default=5000, + default=int(PORT_API), show_default=True, help="Port to run the FastAPI backend on.", ) diff --git a/src/sample/utils/constants.py b/src/sample/utils/constants.py index b7416bb..4db25aa 100644 --- a/src/sample/utils/constants.py +++ b/src/sample/utils/constants.py @@ -7,6 +7,7 @@ load_env() # These should be in environment variables or .env file. DEFAULT_PORT = 8005 + DEFAULT_API_PREFIX = "/api/v1" DEFAULT_MONGODB_URI = ( "mongodb://127.0.0.1:27017/?retryWrites=true&w=majority&appName=Sample" @@ -30,6 +31,9 @@ def safe_get(env_key: str, default) -> str: return value +PORT = safe_get("PORT", DEFAULT_PORT) +PORT_API = safe_get("PORT_API", int(PORT) + 1) + API_PREFIX = safe_get("API_PREFIX", DEFAULT_API_PREFIX) From d7952a37125c8963a547320ca22973f26bbf3f9a Mon Sep 17 00:00:00 2001 From: Heisenberg208 Date: Tue, 10 Mar 2026 21:52:10 +0530 Subject: [PATCH 4/4] Bump version to 1.0.1 in pyproject.toml --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index dd4c905..37f0f15 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "python-starterkit" -version = "1.0.0" +version = "1.0.1" description = "A python starterkit for fastapi and mongo projects." authors = [{ name = "admin", email = "recursivezero@outlook.com" }] license = "MIT"