暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

run.py 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # -*- coding: utf-8 -*-
  2. import loader # Lodel2 loader
  3. import os
  4. from werkzeug.contrib.sessions import FilesystemSessionStore
  5. from werkzeug.wrappers import Response
  6. from lodel.settings import Settings
  7. from .interface.router import get_controller
  8. from .interface.lodelrequest import LodelRequest
  9. from .exceptions import *
  10. from .client import WebUiClient
  11. from lodel.utils.datetime import get_utc_timestamp
  12. from lodel.plugin.hooks import LodelHook
  13. SESSION_FILES_BASE_DIR = Settings.webui.sessions.directory
  14. SESSION_FILES_TEMPLATE = Settings.webui.sessions.file_template
  15. SESSION_EXPIRATION_LIMIT = Settings.webui.sessions.expiration
  16. session_store = FilesystemSessionStore(path=SESSION_FILES_BASE_DIR, filename_template=SESSION_FILES_TEMPLATE)
  17. #Starting instance
  18. loader.start()
  19. #providing access to dyncode
  20. import lodel
  21. import leapi_dyncode as dyncode
  22. lodel.dyncode = dyncode
  23. # TODO déplacer dans un module "sessions.py"
  24. def delete_old_session_files(timestamp_now):
  25. session_files_path = os.path.abspath(session_store.path)
  26. session_files = [os.path.join(session_files_path, file_object) for file_object in os.listdir(session_files_path)
  27. if os.path.isfile(os.path.join(session_files_path, file_object))]
  28. for session_file in session_files:
  29. last_modified = os.stat(session_file).st_mtime
  30. expiration_timestamp = last_modified + SESSION_EXPIRATION_LIMIT
  31. if timestamp_now > expiration_timestamp:
  32. os.unlink(session_file)
  33. def is_session_file_expired(timestamp_now, sid):
  34. session_file = session_store.get_session_filename(sid)
  35. expiration_timestamp = os.stat(session_file).st_mtime + SESSION_EXPIRATION_LIMIT
  36. if timestamp_now < expiration_timestamp:
  37. return False
  38. return True
  39. # WSGI Application
  40. def application(env, start_response):
  41. WebUiClient(env['REMOTE_ADDR'], env['HTTP_USER_AGENT'])
  42. current_timestamp = get_utc_timestamp()
  43. delete_old_session_files(current_timestamp)
  44. request = LodelRequest(env)
  45. sid = request.cookies.get('sid')
  46. if sid is None or sid not in session_store.list():
  47. request.session = session_store.new()
  48. request.session['last_accessed'] = current_timestamp
  49. else:
  50. request.session = session_store.get(sid)
  51. if is_session_file_expired(current_timestamp, sid):
  52. session_store.delete(request.session)
  53. request.session = session_store.new()
  54. request.session['user_context'] = None
  55. request.session['last_accessed'] = current_timestamp
  56. try:
  57. controller = get_controller(request)
  58. response = controller(request)
  59. except HttpException as e:
  60. try:
  61. response = e.render(request)
  62. except Exception as eb:
  63. res = Response()
  64. res.status_code = 500
  65. return res
  66. if request.session.should_save:
  67. session_store.save(request.session)
  68. response.set_cookie('sid', request.session.sid)
  69. res = response(env, start_response)
  70. WebUiClient.destroy()
  71. return res