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

base code for fast api with controller structure #469

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
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
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.

2 changes: 1 addition & 1 deletion apps/calendar/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
router.register(r"tags", TagViewSet, basename="tag")

urlpatterns = [
path("api/calendar/", include(router.urls)),
path("calendar/", include(router.urls)),
]
8 changes: 4 additions & 4 deletions apps/core/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
from apps.core.views import HomeView, InvalidSsoLoginView, StatusView, router

urlpatterns = [
path("api/", include(router.urls)),
path("api/home/", view=HomeView.as_view(), name="HomeView"),
path("api/status/", view=StatusView.as_view(), name="StatusView"),
path("", include(router.urls)),
path("home/", view=HomeView.as_view(), name="HomeView"),
path("status/", view=StatusView.as_view(), name="StatusView"),
path(
"api/invalid_sso_login/",
"invalid_sso_login/",
InvalidSsoLoginView.as_view(),
name="InvalidSsoLoginView",
),
Expand Down
2 changes: 1 addition & 1 deletion apps/global_notice/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@


urlpatterns = [
path("api/global_notices/", include(router.urls)),
path("global_notices/", include(router.urls)),
]
8 changes: 4 additions & 4 deletions apps/user/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from apps.user.views.unregister import Unregister

urlpatterns = [
path("api/", include(router.urls)),
path("api/me", MeView.as_view(), name="me"),
path("api/unregister", Unregister.as_view(), name="unregister"),
path("api/fcm_token/<mode>", FCMTokenView.as_view(), name="fcm"),
path("", include(router.urls)),
path("me", MeView.as_view(), name="me"),
path("unregister", Unregister.as_view(), name="unregister"),
path("fcm_token/<mode>", FCMTokenView.as_view(), name="fcm"),
]
9 changes: 9 additions & 0 deletions ara/controller/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from fastapi import APIRouter

from ara.controller.sample import router as sample_router
from ara.controller.sample.sample_controller import test_router

fastapi_router = APIRouter()

fastapi_router.include_router(sample_router, prefix="/api")
fastapi_router.include_router(test_router, prefix="/api")
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
16 changes: 16 additions & 0 deletions ara/controller/sample/sample_controller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from fastapi import APIRouter

router = APIRouter()


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


test_router = APIRouter(prefix="/test")


@router.get("/")
def test():
return {"test": "test"}
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
11 changes: 6 additions & 5 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 All @@ -23,25 +24,25 @@
)

urlpatterns = [
path("api/admin/", admin.site.urls),
path("admin/", admin.site.urls),
path("", include(("apps.core.urls", "core"))),
path("", include(("apps.user.urls", "user"))),
path("", include(("apps.global_notice.urls", "global_notice"))),
path("", include(("apps.calendar.urls", "calendar"))),
path("api/schema/", SpectacularAPIView.as_view(), name="schema"),
path("schema/", SpectacularAPIView.as_view(), name="schema"),
path(
"api/schema/swagger/",
"schema/swagger/",
SpectacularSwaggerView.as_view(url_name="schema"),
name="swagger",
),
path(
"api/schema/redoc/",
"schema/redoc/",
SpectacularRedocView.as_view(url_name="schema"),
name="redoc",
),
]

if settings.DEBUG:
urlpatterns += [
path("api/__debug__/", include("debug_toolbar.urls")),
path("__debug__/", include("debug_toolbar.urls")),
]
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 fastapi_router as 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="")
api.mount("/api", WSGIMiddleware(application))

return api


app = get_fastapi()
Loading