Skip to content

Commit

Permalink
feat(main): setup fastapi app with openapi spec (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhavir authored Aug 26, 2022
1 parent e7304f1 commit fba9514
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ services:
ports:
- 9001:9001
volumes:
- ./app:/app
- ./src/app:/src/app
networks:
- test

Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fastapi==0.80.0
uvicorn[standard]==0.18.3
54 changes: 52 additions & 2 deletions src/app/main.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,52 @@
if __name__ == '__main__':
print("hello world")
import argparse

import uvicorn
from fastapi import FastAPI, APIRouter
from fastapi.openapi.utils import get_openapi


APP_VERSION = 1.0
SERVICE_NAME = 'Password Generator'
SERVICE_DESCRIPTION = 'The password generator service'

app = FastAPI(title=SERVICE_NAME, description=SERVICE_DESCRIPTION)

# Main routers for API versioning
router_api = APIRouter()
router_api_v1 = APIRouter()
# Registration of routes


# Finalizing setup of router with FastAPI app
router_api.include_router(router_api_v1, prefix="/v1")
app.include_router(router_api, prefix="/api")


# OpenAPI
def custom_openapi():
if app.openapi_schema:
return app.openapi_schema
openapi_schema = get_openapi(
title=SERVICE_NAME,
version=f"{APP_VERSION}",
description=SERVICE_DESCRIPTION,
routes=app.routes,
)
app.openapi_schema = openapi_schema
return app.openapi_schema


app.openapi = custom_openapi

if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--port', type=int, default=9001)
parser.add_argument('--host', type=str, default="0.0.0.0")
parser.add_argument('--reload', action='store_true', default=True)

args = parser.parse_args()
host = args.host
port = args.port
reload = args.reload

uvicorn.run('main:app', host=host, port=port, reload=reload)

0 comments on commit fba9514

Please sign in to comment.