56 lines
1.7 KiB
Python
56 lines
1.7 KiB
Python
# app.py
|
|
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
|
|
|
|
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')
|
|
|
|
if __name__ == '__main__':
|
|
app.run(debug=True)
|