Import FastAPI paths in main.py, static/templates dir path bugfix
This commit is contained in:
parent
804e07b555
commit
bc22b17f2a
3 changed files with 32 additions and 8 deletions
|
@ -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()
|
||||
|
||||
|
|
17
app/main.py
17
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()
|
||||
|
|
12
app/pages.py
12
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,
|
||||
)
|
||||
|
|
Loading…
Reference in a new issue