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