109 lines
3.5 KiB
Python
109 lines
3.5 KiB
Python
# app/__init__.py
|
|
|
|
from flask import Flask
|
|
from flask_sqlalchemy import SQLAlchemy
|
|
from flask_migrate import Migrate
|
|
from flask_login import LoginManager, UserMixin
|
|
from flask_ldap3_login import LDAP3LoginManager
|
|
import os
|
|
from werkzeug.middleware.proxy_fix import ProxyFix
|
|
|
|
# ---------------------------
|
|
# Initialize extensions
|
|
# ---------------------------
|
|
db = SQLAlchemy()
|
|
migrate = Migrate()
|
|
login_manager = LoginManager()
|
|
ldap_manager = LDAP3LoginManager()
|
|
|
|
# ---------------------------
|
|
# LDAP User class for Flask-Login
|
|
# ---------------------------
|
|
class LDAPUser(UserMixin):
|
|
def __init__(self, dn, username, data):
|
|
self.id = username # Flask-Login requires .id
|
|
self.username = username
|
|
self.dn = dn
|
|
self.data = data
|
|
|
|
# ---------------------------
|
|
# Create Flask app factory
|
|
# ---------------------------
|
|
def create_app():
|
|
app = Flask(__name__) # simplified, no APPLICATION_ROOT
|
|
app.secret_key = "o17d88dba8ebd13565e862c752bf017b7"
|
|
|
|
# ---------------------------
|
|
# Upload folder config
|
|
# ---------------------------
|
|
app.config['UPLOAD_FOLDER'] = os.path.join(os.getcwd(), 'uploads')
|
|
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
|
|
|
|
# ---------------------------
|
|
# Database config
|
|
# ---------------------------
|
|
# app.config["SQLALCHEMY_DATABASE_URI"] = "mysql+pymysql://root:admin@mysql:3306/excel_data7"
|
|
app.config["SQLALCHEMY_DATABASE_URI"] = "mysql+pymysql://root:root@localhost:3306/excel_data7"
|
|
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
|
|
|
# ---------------------------
|
|
# LDAP config
|
|
# ---------------------------
|
|
# app.config['LDAP_HOST'] = 'openldap'
|
|
app.config['LDAP_HOST'] = 'localhost'
|
|
# app.config['LDAP_PORT'] = 389
|
|
app.config['LDAP_PORT'] = 389
|
|
app.config['LDAP_USE_SSL'] = False
|
|
|
|
# Base DN for your directory
|
|
app.config['LDAP_BASE_DN'] = 'dc=lcepl,dc=org'
|
|
|
|
# Admin bind credentials
|
|
app.config['LDAP_BIND_USER_DN'] = 'uid=admin,dc=lcepl,dc=org'
|
|
app.config['LDAP_BIND_USER_PASSWORD'] = 'admin123'
|
|
|
|
# User search config
|
|
app.config['LDAP_USER_SEARCH_BASE'] = 'dc=lcepl,dc=org'
|
|
app.config['LDAP_USER_SEARCH_FILTER'] = '(uid=%s)'
|
|
app.config['LDAP_USER_RDN_ATTR'] = 'uid'
|
|
app.config['LDAP_USER_LOGIN_ATTR'] = 'uid'
|
|
|
|
# ---------------------------
|
|
# Initialize extensions
|
|
# ---------------------------
|
|
db.init_app(app)
|
|
migrate.init_app(app, db)
|
|
login_manager.init_app(app)
|
|
ldap_manager.init_app(app)
|
|
|
|
# ---------------------------
|
|
# Login view
|
|
# ---------------------------
|
|
login_manager.login_view = 'main.login' # blueprint + endpoint
|
|
login_manager.login_message_category = 'info'
|
|
|
|
# ---------------------------
|
|
# Flask-Login user loader
|
|
# ---------------------------
|
|
@login_manager.user_loader
|
|
def load_user(user_id):
|
|
return LDAPUser(dn=None, username=user_id, data={})
|
|
|
|
# ---------------------------
|
|
# Register blueprints
|
|
# ---------------------------
|
|
try:
|
|
from app.routes.main import main as main_bp
|
|
from app.routes.reports import reports as reports_bp
|
|
app.register_blueprint(main_bp)
|
|
app.register_blueprint(reports_bp)
|
|
except ImportError as e:
|
|
print(f"Error importing blueprints: {e}")
|
|
|
|
# ---------------------------
|
|
# Fix proxy headers for reverse proxy (Nginx)
|
|
# ---------------------------
|
|
app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1, x_proto=1, x_host=1, x_port=1, x_prefix=1)
|
|
|
|
return app
|