Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Query
|
| 2 |
+
from fastapi.responses import HTMLResponse
|
| 3 |
+
from fastapi.staticfiles import StaticFiles
|
| 4 |
+
from pydantic import BaseModel
|
| 5 |
+
import os
|
| 6 |
+
import json
|
| 7 |
+
import shutil
|
| 8 |
+
import git
|
| 9 |
+
import time
|
| 10 |
+
|
| 11 |
+
app = FastAPI(title="Uspoo MVP")
|
| 12 |
+
|
| 13 |
+
# ========== تحميل الـ Rust core library ==========
|
| 14 |
+
import uspoo_core # المكتبة التي بنيناها بـ PyO3
|
| 15 |
+
|
| 16 |
+
# ========== دوال الفهرسة والبحث ==========
|
| 17 |
+
INDEX_PATH = "./uspoo_index"
|
| 18 |
+
REPO_PATH = "./repo_data"
|
| 19 |
+
|
| 20 |
+
@app.on_event("startup")
|
| 21 |
+
async def startup():
|
| 22 |
+
"""يستنسخ المستودع ويبنيه عند بدء التشغيل"""
|
| 23 |
+
# استنساخ مستودع خفيف (Flask هو مثال جيد لأنه سهل وسريع)
|
| 24 |
+
if not os.path.exists(REPO_PATH):
|
| 25 |
+
print(f"⬇️ استنساخ المستودع...")
|
| 26 |
+
repo_url = "https://github.com/pallets/flask.git"
|
| 27 |
+
git.Repo.clone_from(repo_url, REPO_PATH, depth=1)
|
| 28 |
+
print(f"✅ تم الاستنساخ")
|
| 29 |
+
|
| 30 |
+
# بناء الفهرس (يتم مرة واحدة، ويتجاهل إذا كان موجوداً)
|
| 31 |
+
if not os.path.exists(INDEX_PATH):
|
| 32 |
+
print(f"🔨 بناء فهرس Tantivy...")
|
| 33 |
+
start = time.time()
|
| 34 |
+
count = uspoo_core.index_directory(INDEX_PATH, REPO_PATH)
|
| 35 |
+
print(f"✅ تمت فهرسة {count} ملف في {time.time()-start:.1f}s")
|
| 36 |
+
|
| 37 |
+
# ========== واجهة HTML ==========
|
| 38 |
+
HTML = """
|
| 39 |
+
<html>
|
| 40 |
+
<head><title>Uspoo Code Search (Rust-powered MVP)</title>
|
| 41 |
+
<style>
|
| 42 |
+
body { font-family: system-ui, sans-serif; margin: 40px; background: #1e1e1e; color: #ccc; }
|
| 43 |
+
input[type=text] { width: 400px; padding: 10px; background: #333; border: 1px solid #555; color: white; border-radius: 5px; }
|
| 44 |
+
input[type=submit] { padding: 10px 20px; background: #007acc; border: none; color: white; border-radius: 5px; cursor: pointer; }
|
| 45 |
+
.result { background: #2d2d2d; padding: 10px; margin: 5px 0; border-radius: 3px; }
|
| 46 |
+
.result a { color: #4ec9b0; text-decoration: none; }
|
| 47 |
+
</style>
|
| 48 |
+
</head>
|
| 49 |
+
<body>
|
| 50 |
+
<h2>🔍 Uspoo Code Search (MVP with Rust + Tantivy)</h2>
|
| 51 |
+
<form method="GET">
|
| 52 |
+
<input type="text" name="q" placeholder="ابحث في الكود..." value="{{ query }}">
|
| 53 |
+
<input type="submit" value="بحث">
|
| 54 |
+
</form>
|
| 55 |
+
<div>
|
| 56 |
+
{% if results is not none %}
|
| 57 |
+
<p>تم العثور على {{ results|length }} نتيجة:</p>
|
| 58 |
+
{% for path in results %}
|
| 59 |
+
<div class="result">📄 {{ path }}</div>
|
| 60 |
+
{% else %}
|
| 61 |
+
<p>لم يتم العثور على نتائج.</p>
|
| 62 |
+
{% endfor %}
|
| 63 |
+
{% endif %}
|
| 64 |
+
</div>
|
| 65 |
+
</body>
|
| 66 |
+
</html>
|
| 67 |
+
"""
|
| 68 |
+
|
| 69 |
+
@app.get("/", response_class=HTMLResponse)
|
| 70 |
+
async def home(q: str = Query(None)):
|
| 71 |
+
results = None
|
| 72 |
+
if q:
|
| 73 |
+
results = uspoo_core.search(INDEX_PATH, q)
|
| 74 |
+
# حقن القيم في قالب HTML
|
| 75 |
+
html = HTML \
|
| 76 |
+
.replace("{{ query }}", q or "") \
|
| 77 |
+
.replace("{{ results|length }}", str(len(results) if results else 0))
|
| 78 |
+
if results:
|
| 79 |
+
results_html = "".join(f'<div class="result">📄 {r}</div>' for r in results)
|
| 80 |
+
else:
|
| 81 |
+
results_html = "<p>لم يتم العثور على نتائج.</p>" if q else ""
|
| 82 |
+
html = html.replace("{% for path in results %}...{% endfor %}", results_html)
|
| 83 |
+
return html
|