Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
import threading
|
| 3 |
+
import uuid
|
| 4 |
+
from fastapi import FastAPI, BackgroundTasks
|
| 5 |
+
from fastapi.responses import JSONResponse
|
| 6 |
+
import uvicorn
|
| 7 |
+
|
| 8 |
+
# Istihbarat modülünü içe aktar
|
| 9 |
+
# !!! DİKKAT: Bu dosya 'Istihbarat.py' ile aynı klasörde olmalı !!!
|
| 10 |
+
import Istihbarat
|
| 11 |
+
|
| 12 |
+
app = FastAPI()
|
| 13 |
+
|
| 14 |
+
# Çalışan işlemleri takip etmek için basit bir sözlük
|
| 15 |
+
tasks_status = {}
|
| 16 |
+
|
| 17 |
+
def run_istihbarat(task_id: str):
|
| 18 |
+
"""Istihbarat modülünü arka planda çalıştırır."""
|
| 19 |
+
try:
|
| 20 |
+
# Istihbarat modülünün ana fonksiyonunu çağır
|
| 21 |
+
# Bu fonksiyon genelde sonsuz döngüye girer, bu yüzden bir thread'de çalıştırılır.
|
| 22 |
+
Istihbarat.istihbarat_modulu()
|
| 23 |
+
tasks_status[task_id] = "Tamamlandı"
|
| 24 |
+
except Exception as e:
|
| 25 |
+
tasks_status[task_id] = f"Hata: {str(e)}"
|
| 26 |
+
|
| 27 |
+
@app.get("/")
|
| 28 |
+
async def root():
|
| 29 |
+
"""Ana sayfa, sunucunun çalıştığını gösterir."""
|
| 30 |
+
return {"message": "Dexo API Sunucusu Çalışıyor!", "status": "online"}
|
| 31 |
+
|
| 32 |
+
@app.post("/start")
|
| 33 |
+
async def start_istihbarat(background_tasks: BackgroundTasks):
|
| 34 |
+
"""Istihbarat modülünü başlatmak için endpoint."""
|
| 35 |
+
task_id = str(uuid.uuid4())
|
| 36 |
+
tasks_status[task_id] = "Başlatılıyor..."
|
| 37 |
+
|
| 38 |
+
# Arka planda çalıştır
|
| 39 |
+
background_tasks.add_task(run_istihbarat, task_id)
|
| 40 |
+
|
| 41 |
+
return JSONResponse(content={
|
| 42 |
+
"message": "Istihbarat modülü başlatılıyor...",
|
| 43 |
+
"task_id": task_id,
|
| 44 |
+
"status_url": f"/status/{task_id}"
|
| 45 |
+
})
|
| 46 |
+
|
| 47 |
+
@app.get("/status/{task_id}")
|
| 48 |
+
async def get_status(task_id: str):
|
| 49 |
+
"""İşlemin durumunu sorgulamak için endpoint."""
|
| 50 |
+
status = tasks_status.get(task_id, "Bilinmeyen işlem ID'si")
|
| 51 |
+
return {"task_id": task_id, "status": status}
|
| 52 |
+
|
| 53 |
+
# Sunucuyu başlatmak için (Docker'da CMD ile çağrılacak)
|
| 54 |
+
if __name__ == "__main__":
|
| 55 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|