Queer European MD passionate about IT

app.py 3.1 KB

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