Import FastAPI paths in main.py, static/templates dir path bugfix

This commit is contained in:
DarkCat09 2023-02-18 21:25:47 +04:00
parent 804e07b555
commit bc22b17f2a
3 changed files with 32 additions and 8 deletions

View file

@ -1,13 +1,20 @@
import secrets
from pathlib import Path
from fastapi.templating import Jinja2Templates
from pydantic import BaseSettings
file_dir = Path(__file__).parent
class Settings(BaseSettings):
secret_key: str = secrets.token_hex(32)
templates_dir: str = '../templates'
static_dir: str = '../static'
templates_dir: str = str(
file_dir.parent / 'templates'
)
static_dir: str = str(
file_dir.parent / 'static'
)
settings = Settings()

View file

@ -1,12 +1,25 @@
from typing import List, Type
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
#from .common import settings, templates
from .common import settings
# Add your paths here
from .paths import Paths
from . import pages
from . import errors
paths: List[Type[Paths]] = [
pages.MainPaths,
errors.ErrorsPaths,
]
app = FastAPI()
app.mount(
'/static',
StaticFiles(directory='../static'),
StaticFiles(directory=settings.static_dir),
name='static',
)
for p in paths:
p(app).add_paths()

View file

@ -1,16 +1,20 @@
"""Main FastAPI paths"""
from fastapi import Response
from fastapi import Request, Response
from . import paths
from . import respond
class MainPaths(paths.Paths):
"""Main FastAPI app paths, inherits paths.Paths"""
"""Main FastAPI app paths,
inherits paths.Paths"""
def add_paths(self) -> None:
@self.app.get('/')
def index() -> Response:
return respond.with_tmpl('index.html')
def index(req: Request) -> Response:
return respond.with_tmpl(
'index.html',
request=req,
)