view_manager.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. __app__.json_encoder = model_manager.ComplexEncoder
  28. limiter = Limiter(__app__,key_func=get_remote_address,default_limits=["100 per minute"])
  29. limiter.logger = logger
  30. from werkzeug.serving import make_server
  31. class ServerThread(threading.Thread):
  32. def __init__(self, app,ip,port):
  33. threading.Thread.__init__(self)
  34. self.server = make_server(ip, port, app)
  35. self.ctx = app.app_context()
  36. self.ctx.push()
  37. def run(self):
  38. logger.info('starting server')
  39. self.server.serve_forever()
  40. def shutdown(self):
  41. self.server.shutdown()
  42. __server_process__ : ServerThread
  43. def get_user_privilege() :
  44. return privileges.get_privileges_from_user(session["user_id"])
  45. def init() :
  46. privileges.init()
  47. view_error_management.define_error_management(__app__)
  48. view_basic_api.define_basic_api(__app__)
  49. @__app__.after_request
  50. def after_request(response : flask.Response):
  51. header = response.headers
  52. # adding this to the header to allow cross origin
  53. # for exemple origin cross origin is when website with javascript has it's server (origin 1)
  54. # and the javascript call some request on another server (origin 2), typically our API.
  55. header['Access-Control-Allow-Origin'] = '*'
  56. header['Access-Control-Allow-Methods'] = 'GET,DELETE,UPDATE,HEAD,OPTIONS,POST,PUT,PATCH'
  57. header['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept, Authorization, x-Auth-Token'
  58. logger.debug(response.__dict__)
  59. return response
  60. def JsonStringToResponse(string : str) :
  61. resp = jsonify(json.loads(string))
  62. resp.status_code = 200
  63. return
  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)