from fastapi import FastAPI, Query from fastapi.responses import HTMLResponse from fastapi.staticfiles import StaticFiles from pydantic import BaseModel import os import json import shutil import git import time app = FastAPI(title="Uspoo MVP") # ========== تحميل الـ Rust core library ========== import uspoo_core # المكتبة التي بنيناها بـ PyO3 # ========== دوال الفهرسة والبحث ========== INDEX_PATH = "./uspoo_index" REPO_PATH = "./repo_data" @app.on_event("startup") async def startup(): """يستنسخ المستودع ويبنيه عند بدء التشغيل""" # استنساخ مستودع خفيف (Flask هو مثال جيد لأنه سهل وسريع) if not os.path.exists(REPO_PATH): print(f"⬇️ استنساخ المستودع...") repo_url = "https://github.com/pallets/flask.git" git.Repo.clone_from(repo_url, REPO_PATH, depth=1) print(f"✅ تم الاستنساخ") # بناء الفهرس (يتم مرة واحدة، ويتجاهل إذا كان موجوداً) if not os.path.exists(INDEX_PATH): print(f"🔨 بناء فهرس Tantivy...") start = time.time() count = uspoo_core.index_directory(INDEX_PATH, REPO_PATH) print(f"✅ تمت فهرسة {count} ملف في {time.time()-start:.1f}s") # ========== واجهة HTML ========== HTML = """ Uspoo Code Search (Rust-powered MVP)

🔍 Uspoo Code Search (MVP with Rust + Tantivy)

{% if results is not none %}

تم العثور على {{ results|length }} نتيجة:

{% for path in results %}
📄 {{ path }}
{% else %}

لم يتم العثور على نتائج.

{% endfor %} {% endif %}
""" @app.get("/", response_class=HTMLResponse) async def home(q: str = Query(None)): results = None if q: results = uspoo_core.search(INDEX_PATH, q) # حقن القيم في قالب HTML html = HTML \ .replace("{{ query }}", q or "") \ .replace("{{ results|length }}", str(len(results) if results else 0)) if results: results_html = "".join(f'
📄 {r}
' for r in results) else: results_html = "

لم يتم العثور على نتائج.

" if q else "" html = html.replace("{% for path in results %}...{% endfor %}", results_html) return html