-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
97 lines (76 loc) · 2.69 KB
/
app.py
File metadata and controls
97 lines (76 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
from flask import Flask, render_template
from flask_wtf.csrf import CSRFProtect
from flask_login import LoginManager
from flask_bcrypt import Bcrypt
from kusibot.database.models import User
from config import config
from kusibot.database.db import init_db
from kusibot.app import (
main_bp,
auth_bp,
chatbot_bp,
professional_bp
)
from dotenv import load_dotenv
import os
##############################################
# 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"
def create_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 protection
CSRFProtect(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_loader
def load_user(user_id):
return User.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)
def page_not_found(e):
"""Custom error handler for 404 errors."""
return render_template('not_found.html'), 404
@app.errorhandler(403)
def page_forbidden(e):
"""Custom error handler for 403 errors."""
return render_template('forbidden.html'), 403
return app
def main():
"""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)
if __name__ == '__main__':
main()