from array import array import threading from flask import session,Flask,request, jsonify import flask import flask_cors import persistence import Model.isp_model as isp_model import Model.model_manager as model_manager from werkzeug.security import check_password_hash,generate_password_hash import View.view_privilege as privileges import Modules.Inventory.inventory_view as inventory_view from datetime import timedelta import View.view_error_management as view_error_management from flask_limiter import Limiter from flask_limiter.util import get_remote_address import json import View.view_basics_api as view_basic_api import jwt import logging from utility.app_logging import logger_name logger = logging.getLogger(logger_name + ".VIEW") __app__ = Flask("OpenIsp") __app__.secret_key = "aseqzdwxc" __app__.permanent_session_lifetime = timedelta(minutes=2) __app__.logger = logger __app__.config['JSONIFY_PRETTYPRINT_REGULAR'] = True flask_cors.CORS(__app__) logging.getLogger('flask_cors').level = logging.DEBUG limiter = Limiter(__app__,key_func=get_remote_address,default_limits=["100 per minute"]) limiter.logger = logger from werkzeug.serving import make_server class ServerThread(threading.Thread): def __init__(self, app,ip,port): threading.Thread.__init__(self) self.server = make_server(ip, port, app) self.ctx = app.app_context() self.ctx.push() def run(self): logger.info('starting server') self.server.serve_forever() def shutdown(self): self.server.shutdown() __server_process__ : ServerThread def get_user_privilege() : return privileges.get_privileges_from_user(session["user_id"]) def init() : privileges.init() view_error_management.define_error_management(__app__) view_basic_api.define_basic_api(__app__) @__app__.after_request def after_request(response : flask.Response): header = response.headers # adding this to the header to allow cross origin # for exemple origin cross origin is when website with javascript has it's server (origin 1) # and the javascript call some request on another server (origin 2), typically our API. header['Access-Control-Allow-Origin'] = '*' header['Access-Control-Allow-Methods'] = 'GET,DELETE,UPDATE,HEAD,OPTIONS,POST,PUT,PATCH' header['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept, Authorization, x-Auth-Token' logger.debug(response.__dict__) return response def JsonStringToResponse(string : str) : return jsonify(json.loads(string)) def run() : global __server_process__ __server_process__ = ServerThread(__app__,"0.0.0.0",8000) __server_process__.start() logger.info('View server started') def stop() : global __server_process__ __server_process__.shutdown() logger.info('View server stopped') def register_blueprint(blueprint : flask.Blueprint) : assert isinstance(blueprint,flask.Blueprint) logger.info("registering view (blueprint) : '" + blueprint.name + "' with prefix '" + blueprint.url_prefix +"'") __app__.register_blueprint(blueprint)