Queer European MD passionate about IT

app.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. """Run an aiohttp web app."""
  2. # Standard library modules
  3. import asyncio
  4. import datetime
  5. import logging
  6. import os
  7. # Third party modules
  8. from aiohttp import web
  9. # from davtelepot import Bot
  10. # Project modules
  11. from data.config import (
  12. log_file_name, errors_file_name, app_host, app_port
  13. )
  14. # from data.passwords import bot_token
  15. path = os.path.dirname(__file__)
  16. log_file = "{}/data/{}".format(path, log_file_name)
  17. errors_file = "{}/data/{}".format(path, errors_file_name)
  18. # Outputs the log in console, log_file and errors_file
  19. # Log formatter: datetime, module name (filled with spaces up to 15
  20. # characters), logging level name (filled to 8), message
  21. log_formatter = logging.Formatter(
  22. "%(asctime)s [%(module)-15s %(levelname)-8s] %(message)s",
  23. style='%'
  24. )
  25. root_logger = logging.getLogger()
  26. root_logger.setLevel(logging.DEBUG)
  27. file_handler = logging.FileHandler(log_file, mode="a", encoding="utf-8")
  28. file_handler.setFormatter(log_formatter)
  29. file_handler.setLevel(logging.DEBUG)
  30. root_logger.addHandler(file_handler)
  31. file_handler = logging.FileHandler(errors_file, mode="a", encoding="utf-8")
  32. file_handler.setFormatter(log_formatter)
  33. file_handler.setLevel(logging.ERROR)
  34. root_logger.addHandler(file_handler)
  35. consoleHandler = logging.StreamHandler()
  36. consoleHandler.setFormatter(log_formatter)
  37. consoleHandler.setLevel(logging.DEBUG)
  38. root_logger.addHandler(consoleHandler)
  39. app = web.Application()
  40. # davtebot = Bot.get(bot_token)
  41. def page(path='/', headers={'Content-Type': 'text/html'}, prefix='/michele'):
  42. """Decorator: serve the result of such decorated function at given path."""
  43. path = "{pr}{pa}".format(
  44. pr=prefix,
  45. pa=path
  46. )
  47. def decorator(func):
  48. async def decorated(request):
  49. response = await func(request)
  50. if type(response) is str:
  51. if response.endswith('.html'):
  52. with open('vita/homepage.html', 'r') as _file:
  53. response = _file.read()
  54. response = web.Response(
  55. text=response,
  56. headers=headers
  57. )
  58. assert isinstance(
  59. response, web.Response
  60. ), "Invalid response returned!"
  61. return response
  62. app.router.add_get(path, decorated)
  63. return
  64. return decorator
  65. @page('/test')
  66. async def test(request):
  67. """Test page: return datetime."""
  68. return str(datetime.datetime.now())
  69. @page('/pull/image-editor/nZ7Vgm9SSg4vNBy3ytG')
  70. async def pull_image_editor_project(request):
  71. """Git pull at request."""
  72. try:
  73. _subprocess = await asyncio.create_subprocess_shell(
  74. 'bash {path}/pull_image_editor.sh'.format(
  75. path=path
  76. )
  77. )
  78. stdout, stderr = await _subprocess.communicate()
  79. except Exception as e:
  80. logging.error(
  81. "Exception {e}:\n{o}\n{er}".format(
  82. e=e,
  83. o=stdout.decode().strip(),
  84. er=stderr.decode().strip()
  85. )
  86. )
  87. return web.Response(
  88. status=302,
  89. headers={
  90. 'location': 'https://www.davte.it/michele/image-editor/src/',
  91. },
  92. )
  93. web.run_app(app, host=app_host, port=app_port)