45 lines
1.5 KiB
Python
45 lines
1.5 KiB
Python
import os
|
|
import importlib
|
|
from flask import Flask, Blueprint
|
|
from models import db
|
|
import secrets
|
|
app = Flask(__name__, static_folder='static', static_url_path='/static')
|
|
|
|
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db'
|
|
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
|
if os.path.isfile("secret.key"):
|
|
with open("secret.key", "r") as f:
|
|
app.config['SECRET_KEY'] = f.read().strip()
|
|
else:
|
|
app.config['SECRET_KEY'] = secrets.token_hex(16)
|
|
with open("secret.key", "w") as f:
|
|
f.write(app.config['SECRET_KEY'])
|
|
|
|
def register_blueprints(app, package_name, package_path):
|
|
"""
|
|
Auto-register all blueprints found in the package folder.
|
|
Supports files like index.py as well.
|
|
Each module must expose a blueprint named `<something>_bp`.
|
|
"""
|
|
for filename in os.listdir(package_path):
|
|
# Accept .py files including index.py, except __init__.py
|
|
if filename.endswith('.py') and filename != '__init__.py':
|
|
module_name = filename[:-3] # strip .py
|
|
module = importlib.import_module(f"{package_name}.{module_name}")
|
|
|
|
# Register all blueprint instances in the module
|
|
for attr_name in dir(module):
|
|
attr = getattr(module, attr_name)
|
|
if isinstance(attr, Blueprint):
|
|
app.register_blueprint(attr)
|
|
|
|
register_blueprints(app, 'pages', os.path.join(os.path.dirname(__file__), 'pages'))
|
|
|
|
db.init_app(app)
|
|
|
|
with app.app_context():
|
|
db.create_all() # if you want to create tables
|
|
|
|
if __name__ == '__main__':
|
|
app.run(debug=True)
|