Ttgg / app.py
Athspi's picture
Update app.py
2b09d25 verified
raw
history blame
1.41 kB
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from typing import List
import datetime
import json
import os
app = FastAPI()
# Enable CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
class SmsItem(BaseModel):
address: str
body: str
date: int
type: str
class SmsRequest(BaseModel):
sms_list: List[SmsItem]
# Use /tmp directory which has write permissions
DATA_DIR = "/tmp/sms_data"
os.makedirs(DATA_DIR, exist_ok=True)
@app.post("/save-sms")
async def save_sms(request: SmsRequest):
try:
# Create filename with timestamp
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"{DATA_DIR}/backup_{timestamp}.json"
# Save to file
with open(filename, "w") as f:
json.dump([sms.dict() for sms in request.sms_list], f, indent=2)
return {
"status": "success",
"message": f"{len(request.sms_list)} SMS saved",
"filename": filename
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/")
async def health_check():
return {
"status": "healthy",
"timestamp": datetime.datetime.now().isoformat(),
"sms_storage": DATA_DIR
}