Skip to content

Commit

Permalink
feat: add sample code for fastapi
Browse files Browse the repository at this point in the history
  • Loading branch information
hyukychang committed May 1, 2024
1 parent e10acb2 commit f342004
Show file tree
Hide file tree
Showing 12 changed files with 1,184 additions and 496 deletions.
2 changes: 1 addition & 1 deletion .docker/supervisor-app.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[program:gunicorn]
command=gunicorn -b 0.0.0.0:9000 -w 3 --timeout 120 ara.wsgi:application
command=gunicorn -b 0.0.0.0:9000 -w 3 --timeout 120 ara.wsgi:app
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ flush_test:
reset: flush init

run:
python manage.py runserver 0.0.0.0:9000
python manage.py runserver_v2 0.0.0.0:9000

shell:
python manage.py shell -i ipython
Expand Down
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ tqdm = "*"
lxml = "*"
django-ses = "*"
pydantic = "*"
fastapi = {extras = ["all"], version = "*"}

[dev-packages]
black = "*"
Expand Down
1,606 changes: 1,112 additions & 494 deletions Pipfile.lock

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions ara/controller/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from fastapi import APIRouter

from ara.controller.sample import router as sample_router

api_router = APIRouter()

api_router.include_router(sample_router, prefix="/sample")
1 change: 1 addition & 0 deletions ara/controller/sample/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .sample_controller import router
11 changes: 11 additions & 0 deletions ara/controller/sample/sample_controller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from fastapi import APIRouter

router = APIRouter()


@router.get("/")
def test():
return {"test": "test"}


auth_router = APIRouter()
Empty file added ara/management/__init__.py
Empty file.
16 changes: 16 additions & 0 deletions ara/management/commands/runserver_v2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import uvicorn
from django.core.management import BaseCommand
from django.core.management.base import CommandParser


class Command(BaseCommand):
def add_arguments(self, parser: CommandParser) -> None:
super().add_arguments(parser)
# positional argument
parser.add_argument("server", type=str, help="address")

def handle(self, *args, **options):
server: str = options["server"]
host = server.split(":")[0]
port: int = int(server.split(":")[1])
uvicorn.run("ara.wsgi:app", host=host, port=port, reload=True)
1 change: 1 addition & 0 deletions ara/settings/django.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"apps.user",
"apps.global_notice",
"apps.calendar",
"ara",
]

MIDDLEWARE = [
Expand Down
1 change: 1 addition & 0 deletions ara/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""

from django.conf import settings
from django.contrib import admin
from django.urls import include, path
Expand Down
32 changes: 32 additions & 0 deletions ara/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,40 @@

import os

from django.conf import settings
from django.core.wsgi import get_wsgi_application
from fastapi import FastAPI
from fastapi.middleware.wsgi import WSGIMiddleware
from starlette.middleware.cors import CORSMiddleware

from ara.controller.api import api_router

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ara.settings")

application = get_wsgi_application()


def get_fastapi() -> FastAPI:
api = FastAPI(title="ARA")
api.add_middleware(
CORSMiddleware,
allow_origins=settings.ALLOWED_HOSTS or ["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# #################################################################################
# https://fastapi.tiangolo.com/deployment/server-workers/
# Nevertheless, as of now, Uvicorn's capabilities for handling worker processes
# are more limited than Gunicorn's.
# So, if you want to have a process manager at this level (at the Python level),
# then it might be better to try with Gunicorn as the process manager.
# ##################################################################################

api.include_router(api_router, prefix="/v2")
api.mount("/django", WSGIMiddleware(application))

return api


app = get_fastapi()

0 comments on commit f342004

Please sign in to comment.