feat: move data to dedicated directory
This commit is contained in:
parent
fcad85af89
commit
034acdf411
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
data
|
10
app/main.py
10
app/main.py
@ -1,18 +1,21 @@
|
||||
# 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")
|
||||
|
||||
DATA_DIR = Path("data")
|
||||
DATA_FILE = DATA_DIR / "data.json"
|
||||
|
||||
# Ensure the data directory exists
|
||||
DATA_DIR.mkdir(exist_ok=True)
|
||||
|
||||
@app.post("/submit")
|
||||
async def submit_data(request: Request):
|
||||
payload = await request.json()
|
||||
|
||||
# Create the file if it doesn't exist
|
||||
# Initialize data file if it doesn't exist
|
||||
if not DATA_FILE.exists():
|
||||
with open(DATA_FILE, "w") as f:
|
||||
json.dump([], f)
|
||||
@ -27,3 +30,4 @@ async def submit_data(request: Request):
|
||||
json.dump(existing_data, f, indent=4)
|
||||
|
||||
return {"message": "Data saved successfully"}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user