Skip to content

Commit c0ad5b3

Browse files
committed
Init
TODO - Alembic autogenerate configure - Test codes
0 parents  commit c0ad5b3

31 files changed

+784
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea/
2+
__pycache__/

Pipfile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[[source]]
2+
name = "pypi"
3+
url = "https://pypi.org/simple"
4+
verify_ssl = true
5+
6+
[dev-packages]
7+
8+
[packages]
9+
fastapi = "*"
10+
alembic = "*"
11+
sqlalchemy = "*"
12+
uvicorn = "*"
13+
pyjwt = "*"
14+
15+
[requires]
16+
python_version = "3.8"

Pipfile.lock

Lines changed: 226 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# FastAPI Boilerplate
2+
3+
- FastAPI
4+
- SQLAlchemy

alembic.ini

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# A generic, single database configuration.
2+
3+
[alembic]
4+
# path to migration scripts
5+
script_location = migrations
6+
7+
# template used to generate migration files
8+
# file_template = %%(rev)s_%%(slug)s
9+
10+
# timezone to use when rendering the date
11+
# within the migration file as well as the filename.
12+
# string value is passed to dateutil.tz.gettz()
13+
# leave blank for localtime
14+
# timezone =
15+
16+
# max length of characters to apply to the
17+
# "slug" field
18+
# truncate_slug_length = 40
19+
20+
# set to 'true' to run the environment during
21+
# the 'revision' command, regardless of autogenerate
22+
# revision_environment = false
23+
24+
# set to 'true' to allow .pyc and .pyo files without
25+
# a source .py file to be detected as revisions in the
26+
# versions/ directory
27+
# sourceless = false
28+
29+
# version location specification; this defaults
30+
# to migrations/versions. When using multiple version
31+
# directories, initial revisions must be specified with --version-path
32+
# version_locations = %(here)s/bar %(here)s/bat migrations/versions
33+
34+
# the output encoding used when revision files
35+
# are written from script.py.mako
36+
# output_encoding = utf-8
37+
38+
sqlalchemy.url = driver://user:pass@localhost/dbname
39+
40+
41+
[post_write_hooks]
42+
# post_write_hooks defines scripts or Python functions that are run
43+
# on newly generated revision scripts. See the documentation for further
44+
# detail and examples
45+
46+
# format using "black" - use the console_scripts runner, against the "black" entrypoint
47+
# hooks=black
48+
# black.type=console_scripts
49+
# black.entrypoint=black
50+
# black.options=-l 79
51+
52+
# Logging configuration
53+
[loggers]
54+
keys = root,sqlalchemy,alembic
55+
56+
[handlers]
57+
keys = console
58+
59+
[formatters]
60+
keys = generic
61+
62+
[logger_root]
63+
level = WARN
64+
handlers = console
65+
qualname =
66+
67+
[logger_sqlalchemy]
68+
level = WARN
69+
handlers =
70+
qualname = sqlalchemy.engine
71+
72+
[logger_alembic]
73+
level = INFO
74+
handlers =
75+
qualname = alembic
76+
77+
[handler_console]
78+
class = StreamHandler
79+
args = (sys.stderr,)
80+
level = NOTSET
81+
formatter = generic
82+
83+
[formatter_generic]
84+
format = %(levelname)-5.5s [%(name)s] %(message)s
85+
datefmt = %H:%M:%S

app/__init__.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from fastapi import FastAPI, Request
2+
from fastapi.middleware.cors import CORSMiddleware
3+
from fastapi.responses import JSONResponse
4+
5+
from app.views import home_router
6+
from app.views.v1 import user_router
7+
from core.config import get_config
8+
from core.db import session
9+
from core.exception import CustomException
10+
11+
12+
def init_cors(app: FastAPI) -> None:
13+
app.add_middleware(
14+
CORSMiddleware,
15+
allow_origins=['*'],
16+
allow_credentials=True,
17+
allow_methods=['*'],
18+
allow_headers=['*'],
19+
)
20+
21+
22+
def init_routers(app: FastAPI) -> None:
23+
app.include_router(home_router)
24+
app.include_router(user_router, prefix='/api/v1', tags=['User'])
25+
26+
27+
def init_listeners(app: FastAPI) -> None:
28+
# Exception handler
29+
@app.exception_handler(CustomException)
30+
async def custom_exception_handler(request: Request, exc: CustomException):
31+
return JSONResponse(
32+
status_code=exc.code,
33+
content=exc.kwargs,
34+
)
35+
36+
# Middleware for SQLAlchemy session
37+
@app.middleware('http')
38+
async def remove_session(request: Request, call_next):
39+
response = await call_next(request)
40+
session.remove()
41+
return response
42+
43+
44+
def create_app() -> FastAPI:
45+
app = FastAPI(
46+
title='Hide',
47+
description='Hide API',
48+
version='1.0.0',
49+
docs_url=None if get_config().ENV == 'production' else '/docs',
50+
redoc_url=None if get_config().ENV == 'production' else '/redoc',
51+
)
52+
init_routers(app=app)
53+
init_cors(app=app)
54+
init_listeners(app=app)
55+
return app
56+
57+
58+
app = create_app()

0 commit comments

Comments
 (0)