tmpl-fastapi/app/main.py

50 lines
977 B
Python

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