2025-03-06 15:50:44 -08:00
|
|
|
from flask import Flask, jsonify, request
|
|
|
|
import os
|
|
|
|
import logging
|
|
|
|
from neo4j import GraphDatabase
|
|
|
|
from dotenv import load_dotenv
|
|
|
|
from werkzeug.middleware.proxy_fix import ProxyFix
|
|
|
|
import importlib.util
|
|
|
|
|
2025-03-08 22:57:53 -08:00
|
|
|
# Load environment variables from .env file
|
2025-03-06 15:50:44 -08:00
|
|
|
load_dotenv()
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
app.wsgi_app = ProxyFix(app.wsgi_app)
|
|
|
|
|
|
|
|
# Configure logging
|
|
|
|
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
|
|
|
|
|
|
# Neo4j configuration
|
|
|
|
NEO4J_URI = os.getenv("NEO4J_URI")
|
|
|
|
NEO4J_USER = os.getenv("NEO4J_USER")
|
|
|
|
NEO4J_PASSWORD = os.getenv("NEO4J_PASSWORD")
|
|
|
|
|
|
|
|
def get_driver():
|
|
|
|
return GraphDatabase.driver(NEO4J_URI, auth=(NEO4J_USER, NEO4J_PASSWORD))
|
|
|
|
|
|
|
|
# Custom logger for Neo4j queries
|
|
|
|
neo4j_logger = logging.getLogger('Neo4jLogger')
|
|
|
|
neo4j_logger.setLevel(logging.INFO)
|
|
|
|
neo4j_handler = logging.StreamHandler()
|
|
|
|
neo4j_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
|
|
|
|
neo4j_handler.setFormatter(neo4j_formatter)
|
|
|
|
neo4j_logger.addHandler(neo4j_handler)
|
|
|
|
|
|
|
|
# Attach the logger to the app instance
|
|
|
|
app.neo4j_logger = neo4j_logger
|
|
|
|
|
|
|
|
# Function to dynamically import and register blueprints
|
|
|
|
def load_blueprints_from_directory(directory):
|
|
|
|
for filename in os.listdir(directory):
|
|
|
|
if filename.endswith('.py') and not filename.startswith('__'):
|
|
|
|
module_name = filename[:-3] # Remove .py extension
|
|
|
|
file_path = os.path.join(directory, filename)
|
|
|
|
spec = importlib.util.spec_from_file_location(module_name, file_path)
|
|
|
|
module = importlib.util.module_from_spec(spec)
|
|
|
|
spec.loader.exec_module(module)
|
|
|
|
|
|
|
|
if hasattr(module, 'bp'):
|
|
|
|
app.register_blueprint(module.bp)
|
|
|
|
|
|
|
|
# Load blueprints
|
|
|
|
load_blueprints_from_directory('endpoints')
|
|
|
|
|
2025-03-08 22:57:53 -08:00
|
|
|
# Sitemap endpoint function
|
|
|
|
def sitemap():
|
|
|
|
# Get all registered routes
|
|
|
|
routes = []
|
|
|
|
for rule in app.url_map.iter_rules():
|
|
|
|
# Skip the sitemap route itself to avoid infinite recursion
|
|
|
|
if rule.endpoint != 'sitemap':
|
|
|
|
routes.append({
|
|
|
|
'rule': rule.rule,
|
|
|
|
'methods': list(rule.methods),
|
|
|
|
'endpoint': rule.endpoint
|
|
|
|
})
|
|
|
|
|
|
|
|
return jsonify(routes)
|
|
|
|
|
|
|
|
# Register the sitemap endpoint
|
|
|
|
app.add_url_rule('/sitemap', view_func=sitemap, methods=['GET'])
|
|
|
|
|
2025-03-06 15:50:44 -08:00
|
|
|
if __name__ == '__main__':
|
|
|
|
app.run(debug=True)
|