File size: 1,989 Bytes
2b09d25 e775e54 2b09d25 e775e54 4549091 e775e54 4549091 e775e54 2b09d25 e775e54 2b09d25 e775e54 a190574 4549091 2b09d25 e775e54 2b09d25 e775e54 2b09d25 502e522 2b09d25 e775e54 2b09d25 502e522 e775e54 2b09d25 e775e54 502e522 e775e54 4549091 a190574 2b09d25 502e522 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
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("/get-sms")
async def get_sms():
try:
sms_list = []
files = sorted(os.listdir(DATA_DIR))
for file in files:
if file.endswith(".json"):
file_path = os.path.join(DATA_DIR, file)
with open(file_path, "r") as f:
data = json.load(f)
sms_list.extend(data)
return {
"status": "success",
"total_sms": len(sms_list),
"sms_list": sms_list
}
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
}
|