first commit
This commit is contained in:
29
app/main.py
Normal file
29
app/main.py
Normal file
@@ -0,0 +1,29 @@
|
||||
# app/main.py
|
||||
from fastapi import FastAPI, Request
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
import json
|
||||
|
||||
app = FastAPI()
|
||||
DATA_FILE = Path("data.json")
|
||||
|
||||
|
||||
@app.post("/submit")
|
||||
async def submit_data(request: Request):
|
||||
payload = await request.json()
|
||||
|
||||
# Create the file if it doesn't exist
|
||||
if not DATA_FILE.exists():
|
||||
with open(DATA_FILE, "w") as f:
|
||||
json.dump([], f)
|
||||
|
||||
with open(DATA_FILE, "r+", encoding="utf-8") as f:
|
||||
existing_data = json.load(f)
|
||||
existing_data.append({
|
||||
"timestamp": datetime.now().isoformat(),
|
||||
"data": payload
|
||||
})
|
||||
f.seek(0)
|
||||
json.dump(existing_data, f, indent=4)
|
||||
|
||||
return {"message": "Data saved successfully"}
|
Reference in New Issue
Block a user