Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
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
3 changes: 2 additions & 1 deletion 3 .env.local
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#Note specify the values within double-quotes("")
ENVIRONMENT=development

PORT=8501
PORT_API=8502
MONGODB_URI="mongodb://<user>:<password>@127.0.0.1:27017/?retryWrites=true&w=majority&appName=sample"
DATABASE_NAME="sample"
4 changes: 4 additions & 0 deletions 4 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ poetry install

```bash
poetry run sample dev
# with custom port
poetry run sample dev --port 8501
```

Access: <http://localhost:8501>
Expand All @@ -63,6 +65,8 @@ Access: <http://localhost:8501>
poetry run sample api
# OR
python src/api/fast_api.py
# with custom port
poetry run sample dev --port 5000
```

Access: <http://127.0.0.1:5000>
Expand Down
2 changes: 1 addition & 1 deletion 2 pyproject.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
11 changes: 4 additions & 7 deletions 11 src/sample/__main__.py
Original file line number Diff line number Diff line change
@@ -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)
5 changes: 2 additions & 3 deletions 5 src/sample/api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
9 changes: 5 additions & 4 deletions 9 src/sample/api/routes.py
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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)
28 changes: 20 additions & 8 deletions 28 src/sample/cli.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import click

from sample.utils.constants import PORT, PORT_API

from . import __version__


Expand All @@ -13,17 +15,27 @@ 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=int(PORT),
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=int(PORT_API),
show_default=True,
help="Port to run the FastAPI backend on.",
)
def api(port: int):
from sample.api.routes import start

start()
start(port)
5 changes: 4 additions & 1 deletion 5 src/sample/utils/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -30,8 +31,10 @@ 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)
PORT = int(safe_get("PORT", DEFAULT_PORT))


def get_mongo_config():
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.