fromflaskimportFlask,render_templatefromflask_wtf.csrfimportCSRFProtectfromflask_loginimportLoginManagerfromflask_bcryptimportBcryptfromkusibot.database.modelsimportUserfromconfigimportconfigfromkusibot.database.dbimportinit_dbfromkusibot.appimport(main_bp,auth_bp,chatbot_bp,professional_bp)fromdotenvimportload_dotenvimportos############################################### Main entry point for the whole application ################################################ Load environment variables from .env file.load_dotenv()# Initialise Bcrypt module for encryption.bcrypt=Bcrypt()CHATBOT_URL="chatbot_bp.chatbot"DASHBOARD_URL="professional_bp.dashboard"LOGIN_URL="auth_bp.login"MAIN_URL="main_bp.index"
[docs]defcreate_app(config_name):""" Creates and Configures a Flask app instance following the Application-factory pattern. Args: config_name (str): The configuration name to use [dev/testing/prod]. Returns: Flask: The Flask app instance. """# Selecting corresponding Flask configuration object.app_config=config[config_name]# Creating Flask instance with common templates and static folders.app=Flask(__name__,template_folder='kusibot/app/templates',static_folder='kusibot/app/static')# Load selected configuration object to the Flask app.app.config.from_object(app_config)# Setting CSRF protectionCSRFProtect(app)# Initialize Bcrypt with the Flask app.bcrypt.init_app(app)# Initialise database to use with the Flask app (db route depends on the config).init_db(app)# Setting up login manager and login page for the Flask app.login_manager=LoginManager(app)login_manager.login_view='auth_bp.login'# Define the function that will be called to load a user.@login_manager.user_loaderdefload_user(user_id):returnUser.query.get(int(user_id))# Registering the blueprints routes for the Flask app.app.register_blueprint(main_bp)app.register_blueprint(auth_bp,url_prefix='/auth')app.register_blueprint(chatbot_bp,url_prefix='/chatbot')app.register_blueprint(professional_bp,url_prefix='/dashboard')@app.errorhandler(404)defpage_not_found(e):"""Custom error handler for 404 errors."""returnrender_template('not_found.html'),404@app.errorhandler(403)defpage_forbidden(e):"""Custom error handler for 403 errors."""returnrender_template('forbidden.html'),403returnapp
[docs]defmain():"""Main entry point for running the app using Flask server."""app=create_app(os.getenv('FLASK_ENV','default'))app.run(host="0.0.0.0",port=5000,debug=True)