website.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. from flask import render_template, send_file, redirect
  2. from time import time
  3. from os import urandom
  4. class Website:
  5. def __init__(self, app) -> None:
  6. self.app = app
  7. self.routes = {
  8. '/': {
  9. 'function': lambda: redirect('/chat'),
  10. 'methods': ['GET', 'POST']
  11. },
  12. '/chat/': {
  13. 'function': self._index,
  14. 'methods': ['GET', 'POST']
  15. },
  16. '/chat/<conversation_id>': {
  17. 'function': self._chat,
  18. 'methods': ['GET', 'POST']
  19. },
  20. '/assets/<folder>/<file>': {
  21. 'function': self._assets,
  22. 'methods': ['GET', 'POST']
  23. }
  24. }
  25. def _chat(self, conversation_id):
  26. if '-' not in conversation_id:
  27. return redirect('/chat')
  28. return render_template('index.html', chat_id = conversation_id)
  29. def _index(self):
  30. return render_template('index.html', chat_id = f'{urandom(4).hex()}-{urandom(2).hex()}-{urandom(2).hex()}-{urandom(2).hex()}-{hex(int(time() * 1000))[2:]}')
  31. def _assets(self, folder: str, file: str):
  32. try:
  33. return send_file(f"./../client/{folder}/{file}", as_attachment=False)
  34. except:
  35. return "File not found", 404