Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
412 changes: 244 additions & 168 deletions api_server.py

Large diffs are not rendered by default.

77 changes: 32 additions & 45 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import sys
import os
import argparse
from pathlib import Path


def print_banner():
Expand All @@ -19,38 +18,38 @@ def print_banner():
def check_requirements() -> bool:
"""Check if required dependencies are installed."""
missing_deps = []

try:
import sqlite3
except ImportError:
missing_deps.append("sqlite3")

try:
import uvicorn
except ImportError:
missing_deps.append("uvicorn")

try:
import fastapi
except ImportError:
missing_deps.append("fastapi")

if missing_deps:
print(f"❌ Missing dependencies: {', '.join(missing_deps)}")
print("💡 Install with: pip install -r requirements.txt")
return False

print("✅ Dependencies verified")
return True


def setup_directories():
"""Create necessary directories."""
directories = ["database", "static", "workflows"]

for directory in directories:
os.makedirs(directory, exist_ok=True)

print("✅ Directories verified")


Expand All @@ -72,7 +71,7 @@ def setup_database(force_reindex: bool = False, skip_index: bool = False) -> str

# Check if database has data or force reindex
stats = db.get_stats()
if stats['total'] == 0 or force_reindex:
if stats["total"] == 0 or force_reindex:
print("📚 Indexing workflows...")
index_stats = db.index_all_workflows(force_reindex=True)
print(f"✅ Indexed {index_stats['processed']} workflows")
Expand All @@ -94,25 +93,26 @@ def start_server(host: str = "127.0.0.1", port: int = 8000, reload: bool = False
print()
print("Press Ctrl+C to stop the server")
print("-" * 50)

# Configure database path
os.environ['WORKFLOW_DB_PATH'] = "database/workflows.db"
os.environ["WORKFLOW_DB_PATH"] = "database/workflows.db"

# Start uvicorn with better configuration
import uvicorn

uvicorn.run(
"api_server:app",
host=host,
port=port,
"api_server:app",
host=host,
port=port,
reload=reload,
log_level="info",
access_log=False # Reduce log noise
access_log=False, # Reduce log noise
)


def main():
"""Main entry point with command line arguments."""
sys.stdout.reconfigure(encoding='utf-8')
sys.stdout.reconfigure(encoding="utf-8")
parser = argparse.ArgumentParser(
description="N8N Workflows Search Engine",
formatter_class=argparse.RawDescriptionHelpFormatter,
Expand All @@ -123,65 +123,52 @@ def main():
python run.py --host 0.0.0.0 # Accept external connections
python run.py --reindex # Force database reindexing
python run.py --dev # Development mode with auto-reload
"""
""",
)

parser.add_argument(
"--host",
default="127.0.0.1",
help="Host to bind to (default: 127.0.0.1)"
"--host", default="127.0.0.1", help="Host to bind to (default: 127.0.0.1)"
)
parser.add_argument(
"--port",
type=int,
default=8000,
help="Port to bind to (default: 8000)"
"--port", type=int, default=8000, help="Port to bind to (default: 8000)"
)
parser.add_argument(
"--reindex",
action="store_true",
help="Force database reindexing"
"--reindex", action="store_true", help="Force database reindexing"
)
parser.add_argument(
"--dev",
action="store_true",
help="Development mode with auto-reload"
"--dev", action="store_true", help="Development mode with auto-reload"
)
parser.add_argument(
"--skip-index",
action="store_true",
help="Skip workflow indexing (useful for CI/testing)"
help="Skip workflow indexing (useful for CI/testing)",
)

args = parser.parse_args()

# Also check environment variable for CI mode
ci_mode = os.environ.get('CI', '').lower() in ('true', '1', 'yes')
ci_mode = os.environ.get("CI", "").lower() in ("true", "1", "yes")
skip_index = args.skip_index or ci_mode

print_banner()

# Check dependencies
if not check_requirements():
sys.exit(1)

# Setup directories
setup_directories()

# Setup database
try:
setup_database(force_reindex=args.reindex, skip_index=skip_index)
except Exception as e:
print(f"❌ Database setup error: {e}")
sys.exit(1)

# Start server
try:
start_server(
host=args.host,
port=args.port,
reload=args.dev
)
start_server(host=args.host, port=args.port, reload=args.dev)
except KeyboardInterrupt:
print("\n👋 Server stopped!")
except Exception as e:
Expand All @@ -190,4 +177,4 @@ def main():


if __name__ == "__main__":
main()
main()
Loading
Loading