tmpl-fastapi/app/main.py

51 lines
977 B
Python
Raw Normal View History

from typing import List, Type
2023-02-20 12:09:43 +04:00
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
2023-02-20 12:09:43 +04:00
from starlette.middleware.sessions import SessionMiddleware
from starlette_wtf import CSRFProtectMiddleware
2023-02-19 16:19:18 +04:00
from . import common
2023-02-27 20:11:55 +04:00
from . import sql
from .paths import Paths
# Add your paths below
from .paths import pages
2023-02-20 12:09:43 +04:00
from .paths import table
from .paths import errors
paths: List[Type[Paths]] = [
pages.MainPaths,
2023-02-20 12:09:43 +04:00
table.TablePaths,
errors.ErrorsPaths,
]
2023-02-27 19:05:04 +04:00
# Initialize SQL database
2023-02-27 20:11:55 +04:00
sql.Base.metadata.create_all(bind=sql.engine)
2023-02-27 19:05:04 +04:00
# Create app
app = FastAPI()
2023-02-27 19:05:04 +04:00
# Mount static files server
app.mount(
'/static',
2023-02-19 16:19:18 +04:00
StaticFiles(directory=common.static_dir),
name='static',
)
2023-02-27 19:05:04 +04:00
# Add paths
for p in paths:
p(app).add_paths()
2023-02-20 12:09:43 +04:00
2023-02-27 19:05:04 +04:00
# Add WTForms CSRF protection middlewares
2023-02-20 12:09:43 +04:00
app.add_middleware(
SessionMiddleware,
secret_key=common.settings.session_key,
)
app.add_middleware(
CSRFProtectMiddleware,
csrf_secret=common.settings.csrf_key,
)