def __init__(self) -> None: # # Flask App # app = MultiStaticFlask("koseki") app.wsgi_app = ReverseProxied(app.wsgi_app) app.config.from_object(KosekiConfig()) app.config.from_pyfile(os.path.join("..", "koseki.cfg")) self.app: Flask = app # # Theme # static_folders: list[str] = [] install_theme(app, "koseki", static_folders) theme: str = app.config["THEME"] if theme is not None and len(theme.strip()) > 0 and theme != "koseki": install_theme(app, theme, static_folders) app.static_folder = static_folders # # Storage # if app.config["DB_TYPE"].lower() == "sqlite": self.storage = Storage("sqlite:///%s" % app.config["DB_SQLITE_PATH"]) elif app.config["DB_TYPE"].lower() == "mysql": self.storage = Storage("mysql://%s:%s@%s/%s" % ( app.config["DB_USER"], app.config["DB_PASSWORD"], app.config["DB_HOST"], app.config["DB_DATABASE"], )) else: raise ValueError( "DB_TYPE is unsupported. Please choose between sqlite/mysql.") # Return connections to db pool after closure self.app.teardown_appcontext(self.storage.close) # Misc Utilities self.auth = KosekiAuth(self.storage) self.mail = KosekiMailer(self.app) self.util = KosekiUtil(app, self.auth, self.storage, self.mail) self.scheduler = KosekiScheduler(app, self.storage, self.mail, self.util) self.plugins = KosekiPluginManager(self.app, self.storage, self.auth, self.util, self.scheduler)
pagure.lib.set_redis(host=APP.config['REDIS_HOST'], port=APP.config['REDIS_PORT'], dbname=APP.config['REDIS_DB']) if APP.config.get('PAGURE_CI_SERVICES'): pagure.lib.set_pagure_ci(APP.config['PAGURE_CI_SERVICES']) if not APP.debug and not APP.config.get('DEBUG', False): APP.logger.addHandler( pagure.mail_logging.get_mail_handler( smtp_server=APP.config.get('SMTP_SERVER', '127.0.0.1'), mail_admin=APP.config.get('MAIL_ADMIN', APP.config['EMAIL_ERROR']), from_email=APP.config.get('FROM_EMAIL', '*****@*****.**'))) APP.wsgi_app = pagure.proxy.ReverseProxied(APP.wsgi_app) # Back port 'equalto' to older version of jinja2 APP.jinja_env.tests.setdefault('equalto', lambda value, other: value == other) def authenticated(): ''' Utility function checking if the current user is logged in or not. ''' return hasattr(flask.g, 'fas_user') and flask.g.fas_user is not None def logout(): auth = APP.config.get('PAGURE_AUTH', None) if auth in ['fas', 'openid']: if hasattr(flask.g, 'fas_user') and flask.g.fas_user is not None:
def server_error(error=None): if error: err_logger.error("500: {}".format(error), exc_info=True) message = {"msg": "Server Error", "code": 500} return jsonify(message), 500 @app.errorhandler(404) def not_found(error=None): message = { 'code': 404, 'msg': 'Not Found: ' + request.url, } resp = jsonify(message) resp.status_code = 404 return resp @app.errorhandler(403) def Permission_denied(error=None): message = {"msg": "Authentication failed, permission denied.", "code": 403} return jsonify(message), 403 if __name__ == '__main__': from werkzeug.contrib.fixers import ProxyFix app.wsgi_app = ProxyFix(app.wsgi_app) app.run(host=config.GLOBAL["Host"], port=int(config.GLOBAL["Port"]), debug=True)
def create_app(config=None): """ Create the flask application. """ app = MultiStaticFlask(__name__) app.config = pagure_config if config: app.config.update(config) if app.config.get('SESSION_TYPE', None) is not None: import flask_session flask_session.Session(app) logging.basicConfig() logging.config.dictConfig(app.config.get('LOGGING') or {'version': 1}) app.jinja_env.trim_blocks = True app.jinja_env.lstrip_blocks = True if perfrepo: # Do this as early as possible. # We want the perfrepo before_request to be the very first thing # to be run, so that we can properly setup the stats before the # request. app.before_request(perfrepo.reset_stats) if pagure_config.get('THEME_TEMPLATE_FOLDER', False): # Jinja can be told to look for templates in different folders # That's what we do here template_folder = pagure_config['THEME_TEMPLATE_FOLDER'] if template_folder[0] != '/': template_folder = os.path.join(app.root_path, app.template_folder, template_folder) import jinja2 # Jinja looks for the template in the order of the folders specified templ_loaders = [ jinja2.FileSystemLoader(template_folder), app.jinja_loader, ] app.jinja_loader = jinja2.ChoiceLoader(templ_loaders) if pagure_config.get('THEME_STATIC_FOLDER', False): static_folder = pagure_config['THEME_STATIC_FOLDER'] if static_folder[0] != '/': static_folder = os.path.join(app.root_path, 'static', static_folder) # Unlike templates, to serve static files from multiples folders we # need flask-multistatic app.static_folder = [ static_folder, os.path.join(app.root_path, 'static'), ] auth = pagure_config.get('PAGURE_AUTH', None) if auth in ['fas', 'openid']: # Only import and set flask_fas_openid if it is needed from pagure.ui.fas_login import FAS FAS.init_app(app) elif auth == 'oidc': # Only import and set flask_fas_openid if it is needed from pagure.ui.oidc_login import oidc, fas_user_from_oidc oidc.init_app(app) app.before_request(fas_user_from_oidc) # Report error by email if not app.debug and not pagure_config.get('DEBUG', False): app.logger.addHandler( pagure.mail_logging.get_mail_handler( smtp_server=pagure_config.get('SMTP_SERVER', '127.0.0.1'), mail_admin=pagure_config.get('MAIL_ADMIN', pagure_config['EMAIL_ERROR']), from_email=pagure_config.get('FROM_EMAIL', '*****@*****.**'))) # Support proxy app.wsgi_app = pagure.proxy.ReverseProxied(app.wsgi_app) # Back port 'equalto' to older version of jinja2 app.jinja_env.tests.setdefault('equalto', lambda value, other: value == other) # Import the application from pagure.api import API # noqa: E402 app.register_blueprint(API) from pagure.ui import UI_NS # noqa: E402 app.register_blueprint(UI_NS) from pagure.internal import PV # noqa: E402 app.register_blueprint(PV) app.before_request(set_request) app.teardown_request(end_request) # Only import the login controller if the app is set up for local login if pagure_config.get('PAGURE_AUTH', None) == 'local': import pagure.ui.login as login app.before_request(login._check_session_cookie) app.after_request(login._send_session_cookie) if perfrepo: # Do this at the very end, so that this after_request comes last. app.after_request(perfrepo.print_stats) app.add_url_rule('/login/', view_func=auth_login, methods=['GET', 'POST']) app.add_url_rule('/logout/', view_func=auth_logout) return app