From bc22b17f2a1bd5e4127894cf62b13360991676c5 Mon Sep 17 00:00:00 2001 From: DarkCat09 Date: Sat, 18 Feb 2023 21:25:47 +0400 Subject: [PATCH] Import FastAPI paths in main.py, static/templates dir path bugfix --- app/common.py | 11 +++++++++-- app/main.py | 17 +++++++++++++++-- app/pages.py | 12 ++++++++---- 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/app/common.py b/app/common.py index d56bbf2..fd32614 100644 --- a/app/common.py +++ b/app/common.py @@ -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() diff --git a/app/main.py b/app/main.py index 52c9a7f..fed3553 100644 --- a/app/main.py +++ b/app/main.py @@ -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() diff --git a/app/pages.py b/app/pages.py index 5a41aff..56c4094 100644 --- a/app/pages.py +++ b/app/pages.py @@ -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, + )