Spaces:
Sleeping
Sleeping
""" | |
Tiny runner that imports `app` (main portal) and mounts `app2`'s blueprint-like routes under `/bot`. | |
This lets you start a single Flask server that serves both apps' pages. | |
""" | |
from flask import Flask | |
import os | |
# Import both modules (they create Flask `app` instances). We'll mount app2 routes into the main app. | |
import importlib | |
# Import user's main app | |
import app as portal_app_module | |
import app2 as bot_app_module | |
# portal_app_module.app is a Flask instance. We'll reuse it as the canonical app. | |
app = portal_app_module.app | |
# app2 defines routes on its own `app` instance. To reuse routes, we copy rules from bot's app to portal app. | |
# This is a minimal adapter: we iterate over bot_app.url_map and register proxy endpoints on portal app that call the original view functions. | |
from werkzeug.routing import Rule | |
from types import FunctionType | |
# Helper to copy view functions by name | |
for rule in list(bot_app_module.app.url_map.iter_rules()): | |
# Skip static rule from the bot app to avoid clashing | |
if rule.endpoint == 'static': | |
continue | |
endpoint = rule.endpoint | |
view_func = bot_app_module.app.view_functions.get(endpoint) | |
if view_func is None: | |
continue | |
# Create a wrapper that calls the original view function from app2 | |
def make_wrapper(f): | |
def wrapper(*args, **kwargs): | |
return f(*args, **kwargs) | |
wrapper.__name__ = f"bot_{f.__name__}" | |
return wrapper | |
wrapper = make_wrapper(view_func) | |
# Build a new rule with a /bot prefix | |
new_rule = str(rule) | |
if not new_rule.startswith('/bot'): | |
new_rule = '/bot' + new_rule | |
try: | |
# Register the wrapper on the portal app under a unique endpoint | |
app.add_url_rule(new_rule, endpoint=f'bot.{endpoint}', view_func=wrapper, methods=list(rule.methods - {'HEAD','OPTIONS'})) | |
except Exception as e: | |
print(f"Warning: failed to register route {new_rule}: {e}") | |
if __name__ == '__main__': | |
# Run the combined app. Use PORT env var when provided (Hugging Face Spaces sets this). | |
port = int(os.environ.get('PORT', os.environ.get('FLASK_RUN_PORT', '5000'))) | |
app.run(host='0.0.0.0', port=port, debug=False) | |