small static cleanup

This commit is contained in:
2019-09-11 12:13:43 +03:00
parent c11103a5ba
commit e86f3ee092
10 changed files with 70 additions and 63 deletions

View File

@ -12,11 +12,21 @@ from aiohttp.web import HTTPNotFound, Response, View
class StaticHtmlView(View):
def __get_content_type(self, filename: str) -> str:
_, ext = os.path.splitext(filename)
print(ext)
if ext == '.css':
return 'text/css'
elif ext == '.js':
return 'text/javascript'
return 'text/plain'
async def get(self) -> Response:
resource_name = self.request.match_info['resource_id']
resource_path = os.path.join(self.request.app['templates_root'], 'static', resource_name)
if not os.path.exists(resource_path) or os.path.isdir(resource_path):
return HTTPNotFound()
content_type = self.__get_content_type(resource_name)
with open(resource_path) as resource_file:
return Response(text=resource_file.read(), content_type='text/css')
return Response(text=resource_file.read(), content_type=content_type)