This commit is contained in:
retoor 2025-01-24 23:42:24 +01:00
parent b56371994f
commit dae877113c

View File

@ -1,33 +1,43 @@
from app.app import Application as BaseApplication import pathlib
import pathlib
from aiohttp import web from aiohttp import web
from app.app import Application as BaseApplication
from snek.system.markdown import MarkdownExtension from snek.system.markdown import MarkdownExtension
from snek.system.markdown import render_markdown
class Application(BaseApplication): class Application(BaseApplication):
def __init__(self, path=None, *args,**kwargs): def __init__(self, path=None, *args, **kwargs):
self.path = pathlib.Path(path) self.path = pathlib.Path(path)
template_path = self.path template_path = self.path
super().__init__(template_path=template_path ,*args, **kwargs) super().__init__(template_path=template_path, *args, **kwargs)
self.jinja2_env.add_extension(MarkdownExtension) self.jinja2_env.add_extension(MarkdownExtension)
self.router.add_get("/{tail:.*}",self.handle_document) self.router.add_get("/{tail:.*}", self.handle_document)
async def handle_document(self, request): async def handle_document(self, request):
relative_path = request.match_info['tail'].strip("/") relative_path = request.match_info["tail"].strip("/")
if relative_path == '': if relative_path == "":
relative_path = 'index.html' relative_path = "index.html"
document_path = self.path.joinpath(relative_path) document_path = self.path.joinpath(relative_path)
if not document_path.exists(): if not document_path.exists():
return web.Response(status=404,body=b'Resource is not found on this server.',content_type="text/plain") return web.Response(
status=404,
body=b"Resource is not found on this server.",
content_type="text/plain",
)
if document_path.is_dir(): if document_path.is_dir():
document_path = document_path.joinpath("index.html") document_path = document_path.joinpath("index.html")
if not document_path.exists(): if not document_path.exists():
return web.Response(status=404,body=b'Resource is not found on this server.',content_type="text/plain") return web.Response(
status=404,
response = await self.render_template(str(document_path.relative_to(self.path)),request) body=b"Resource is not found on this server.",
return response content_type="text/plain",
)
response = await self.render_template(
str(document_path.relative_to(self.path)), request
)
return response