Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, WebSocket, WebSocketDisconnect, Depends
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from sqlalchemy import create_engine, Column, Integer, String, DateTime
|
| 4 |
+
from sqlalchemy.orm import declarative_base, sessionmaker, Session
|
| 5 |
+
import datetime
|
| 6 |
+
|
| 7 |
+
# --- DATABASE SETUP (SQLAlchemy + SQLite) ---
|
| 8 |
+
SQLALCHEMY_DATABASE_URL = "sqlite:///./volatile.db"
|
| 9 |
+
|
| 10 |
+
# check_same_thread=False is needed for SQLite in FastAPI
|
| 11 |
+
engine = create_engine(SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False})
|
| 12 |
+
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
| 13 |
+
Base = declarative_base()
|
| 14 |
+
|
| 15 |
+
# Define the Message Model
|
| 16 |
+
class Message(Base):
|
| 17 |
+
__tablename__ = "messages"
|
| 18 |
+
id = Column(Integer, primary_key=True, index=True)
|
| 19 |
+
sender = Column(String, index=True)
|
| 20 |
+
content = Column(String)
|
| 21 |
+
timestamp = Column(DateTime, default=datetime.datetime.utcnow)
|
| 22 |
+
|
| 23 |
+
# Create the database tables
|
| 24 |
+
Base.metadata.create_all(bind=engine)
|
| 25 |
+
|
| 26 |
+
# Dependency to get DB session
|
| 27 |
+
def get_db():
|
| 28 |
+
db = SessionLocal()
|
| 29 |
+
try:
|
| 30 |
+
yield db
|
| 31 |
+
finally:
|
| 32 |
+
db.close()
|
| 33 |
+
|
| 34 |
+
# --- FASTAPI APP SETUP ---
|
| 35 |
+
app = FastAPI(title="Volatile API")
|
| 36 |
+
|
| 37 |
+
# Allow your Next.js Vercel frontend to connect
|
| 38 |
+
app.add_middleware(
|
| 39 |
+
CORSMiddleware,
|
| 40 |
+
allow_origins=["*"], # Update this to your Vercel domain later!
|
| 41 |
+
allow_credentials=True,
|
| 42 |
+
allow_methods=["*"],
|
| 43 |
+
allow_headers=["*"],
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
# --- ROUTES ---
|
| 47 |
+
@app.get("/")
|
| 48 |
+
def read_root():
|
| 49 |
+
return {"status": "Volatile Backend is running!", "database": "SQLite connected"}
|
| 50 |
+
|
| 51 |
+
# Example route to test saving a message to SQLite
|
| 52 |
+
@app.post("/test-message")
|
| 53 |
+
def create_test_message(sender: str, content: str, db: Session = Depends(get_db)):
|
| 54 |
+
new_msg = Message(sender=sender, content=content)
|
| 55 |
+
db.add(new_msg)
|
| 56 |
+
db.commit()
|
| 57 |
+
db.refresh(new_msg)
|
| 58 |
+
return {"message": "Saved to Volatile memory", "data": new_msg}
|
| 59 |
+
|
| 60 |
+
# --- WEBSOCKETS ---
|
| 61 |
+
@app.websocket("/ws")
|
| 62 |
+
async def websocket_endpoint(websocket: WebSocket):
|
| 63 |
+
await websocket.accept()
|
| 64 |
+
try:
|
| 65 |
+
while True:
|
| 66 |
+
data = await websocket.receive_text()
|
| 67 |
+
await websocket.send_text(f"Echo from Volatile Server: {data}")
|
| 68 |
+
except WebSocketDisconnect:
|
| 69 |
+
print("Client disconnected")
|