view_manager.py 3.2 KB

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