tmpl-fastapi/app/main.py

51 lines
977 B
Python
Raw Permalink Normal View History

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