cfa911 commited on
Commit
47ce483
·
1 Parent(s): 77370a4

added barebones fastApi logic

Browse files
Files changed (1) hide show
  1. app.py +63 -4
app.py CHANGED
@@ -1,7 +1,66 @@
1
- from fastapi import FastAPI
 
 
 
 
 
2
 
3
  app = FastAPI()
4
 
5
- @app.get("/")
6
- def greet_json():
7
- return {"Hello": "World!"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException
2
+ from fastapi.middleware.cors import CORSMiddleware
3
+ from pydantic import BaseModel
4
+ from typing import Dict
5
+ import uuid
6
+ from datetime import datetime
7
 
8
  app = FastAPI()
9
 
10
+ # Enable CORS for React Native development
11
+ app.add_middleware(
12
+ CORSMiddleware,
13
+ allow_origins=["*"],
14
+ allow_methods=["*"],
15
+ allow_headers=["*"],
16
+ )
17
+
18
+ # In-memory "database" (replace with real DB in production)
19
+ jobs_db: Dict[str, Dict] = {}
20
+
21
+ class PostRequest(BaseModel):
22
+ query: str
23
+ topic: str
24
+ date: str # Format: "DD-MM-YYYY to DD-MM-YYYY"
25
+
26
+ @app.post("/index")
27
+ async def create_job(request: PostRequest):
28
+ """Receive query/topic/date and return job ID"""
29
+ job_id = str(uuid.uuid4())
30
+
31
+ # Store the job (simulating background processing)
32
+ jobs_db[job_id] = {
33
+ "status": "processing",
34
+ "request": request.dict(),
35
+ "created_at": datetime.now().isoformat(),
36
+ "result": None
37
+ }
38
+
39
+ # Simulate processing completion after 5 seconds
40
+ # In real apps, use Celery/BackgroundTasks
41
+ jobs_db[job_id]["result"] = {
42
+ "query": request.query,
43
+ "topic": request.topic,
44
+ "date_interval": request.date,
45
+ "processed_data": f"Analysis result for {request.query} ({request.topic})"
46
+ }
47
+ jobs_db[job_id]["status"] = "completed"
48
+
49
+ return {"status": "success", "id": job_id}
50
+
51
+ @app.get("/loading")
52
+ async def get_results(id: str):
53
+ """Check job status and return results if ready"""
54
+ if id not in jobs_db:
55
+ raise HTTPException(status_code=404, detail="Job not found")
56
+
57
+ job = jobs_db[id]
58
+
59
+ if job["status"] != "completed":
60
+ return {"status": "processing"}
61
+
62
+ return {
63
+ "status": "completed",
64
+ "result": job["result"],
65
+ "request": job["request"]
66
+ }