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 import secrets
from pathlib import Path
from fastapi.templating import Jinja2Templates from fastapi.templating import Jinja2Templates
from pydantic import BaseSettings from pydantic import BaseSettings
file_dir = Path(__file__).parent
class Settings(BaseSettings): class Settings(BaseSettings):
secret_key: str = secrets.token_hex(32) secret_key: str = secrets.token_hex(32)
templates_dir: str = '../templates' templates_dir: str = str(
static_dir: str = '../static' file_dir.parent / 'templates'
)
static_dir: str = str(
file_dir.parent / 'static'
)
settings = Settings() settings = Settings()

View file

@ -1,12 +1,25 @@
from typing import List, Type
from fastapi import FastAPI from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles 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 = FastAPI()
app.mount( app.mount(
'/static', '/static',
StaticFiles(directory='../static'), StaticFiles(directory=settings.static_dir),
name='static', name='static',
) )
for p in paths:
p(app).add_paths()

View file

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