From 8042f5ac45e99becababb4599c7706f05acfc2fd Mon Sep 17 00:00:00 2001 From: ArabCoders Date: Mon, 11 Mar 2024 19:45:51 +0300 Subject: [PATCH] Serve static content from memory. --- app/main.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/app/main.py b/app/main.py index ec360acc..8866dbf0 100644 --- a/app/main.py +++ b/app/main.py @@ -397,9 +397,13 @@ class Main: if req.path not in self.staticHolder: return web.HTTPNotFound() - return web.FileResponse(path=self.staticHolder[req.path].get('file'), headers={ - 'Cache-Control': 'public, max-age=31536000', + item: dict = self.staticHolder[req.path] + + return web.Response(body=item.get('content'), headers={ 'Pragma': 'public', + 'Cache-Control': 'public, max-age=31536000', + 'Content-Type': item.get('content_type'), + 'X-Via': 'memory' if not item.get('file', None) else 'disk', }) staticDir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'frontend/dist') @@ -412,8 +416,8 @@ class Main: urlPath = f"{self.config.url_prefix}{file.replace(f'{staticDir}/', '')}" self.staticHolder[urlPath] = { - 'file': file, - # 'content': open(file, 'rb').read(), + # 'file': file, + 'content': open(file, 'rb').read(), 'content_type': self.extToMime.get(os.path.splitext(file)[1], MIME.from_file(file)), }