view_manager.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. from array import array
  2. import threading
  3. from flask import session,Flask,request, jsonify
  4. import flask
  5. import persistence
  6. import Model.isp_model as isp_model
  7. import Model.model_manager as model_manager
  8. from werkzeug.security import check_password_hash,generate_password_hash
  9. import View.view_privilege as privileges
  10. import Modules.Inventory.inventory_view as inventory_view
  11. from datetime import timedelta
  12. import View.view_error_management as view_error_management
  13. from flask_limiter import Limiter
  14. from flask_limiter.util import get_remote_address
  15. import json
  16. import View.view_basics_api as view_basic_api
  17. import logging
  18. from utility.app_logging import logger_name
  19. logger = logging.getLogger(logger_name + ".VIEW")
  20. __app__ = Flask("OpenIsp")
  21. __app__.secret_key = "aseqzdwxc"
  22. __app__.permanent_session_lifetime = timedelta(minutes=2)
  23. __app__.logger = logger
  24. __app__.config['JSONIFY_PRETTYPRINT_REGULAR'] = True
  25. limiter = Limiter(__app__,key_func=get_remote_address,default_limits=["100 per minute"])
  26. limiter.logger = logger
  27. from werkzeug.serving import make_server
  28. class ServerThread(threading.Thread):
  29. def __init__(self, app,ip,port):
  30. threading.Thread.__init__(self)
  31. self.server = make_server(ip, port, app)
  32. self.ctx = app.app_context()
  33. self.ctx.push()
  34. def run(self):
  35. logger.info('starting server')
  36. self.server.serve_forever()
  37. def shutdown(self):
  38. self.server.shutdown()
  39. __server_process__ : ServerThread
  40. def get_user_privilege() :
  41. return privileges.get_privileges_from_user(session["user_account_id"])
  42. def init() :
  43. privileges.init()
  44. view_error_management.define_error_management(__app__)
  45. view_basic_api.define_basic_api(__app__)
  46. @__app__.after_request
  47. def after_request(response : flask.Response):
  48. header = response.headers
  49. # adding this to the header to allow cross origin
  50. # for exemple origin cross origin is when website with javascript has it's server (origin 1)
  51. # and the javascript call some request on another server (origin 2), typically our API.
  52. header['Access-Control-Allow-Origin'] = '*'
  53. header['Access-Control-Allow-Methods'] = 'GET,DELETE,UPDATE,HEAD,OPTIONS,POST,PUT'
  54. header['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept, Authorization'
  55. logger.debug(response.__dict__)
  56. return response
  57. def JsonStringToResponse(string : str) :
  58. return jsonify(json.loads(string))
  59. def run() :
  60. global __server_process__
  61. __server_process__ = ServerThread(__app__,"0.0.0.0",8000)
  62. __server_process__.start()
  63. logger.info('View server started')
  64. def stop() :
  65. global __server_process__
  66. __server_process__.shutdown()
  67. logger.info('View server stopped')
  68. def register_blueprint(blueprint : flask.Blueprint) :
  69. assert isinstance(blueprint,flask.Blueprint)
  70. logger.info("registering view (blueprint) : '" + blueprint.name + "' with prefix '" + blueprint.url_prefix +"'")
  71. __app__.register_blueprint(blueprint)