672f5b74a4
Frontend container now uses nginx to serve static files and proxy /api/* requests to the backend container internally, eliminating the hardcoded localhost:8000 build-time URL that caused "Network error" on any non-local server. CORS origins are also configurable via ALLOWED_ORIGINS env var. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
58 lines
1.6 KiB
Python
58 lines
1.6 KiB
Python
import os
|
|
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from sqlalchemy import text
|
|
|
|
from .database import engine, Base, SessionLocal
|
|
from .routes import users, purchases, stats, history, admin, candles, sells
|
|
|
|
Base.metadata.create_all(bind=engine)
|
|
|
|
app = FastAPI(title="BTC Portfolio API")
|
|
|
|
_raw_origins = os.environ.get(
|
|
"ALLOWED_ORIGINS",
|
|
"http://localhost:3000,http://localhost:3001",
|
|
)
|
|
allowed_origins = [o.strip() for o in _raw_origins.split(",") if o.strip()]
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=allowed_origins,
|
|
allow_credentials=True,
|
|
allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"],
|
|
allow_headers=["Content-Type", "Authorization"],
|
|
)
|
|
|
|
app.include_router(users.router)
|
|
app.include_router(purchases.router)
|
|
app.include_router(stats.router)
|
|
app.include_router(history.router)
|
|
app.include_router(admin.router, prefix="/admin")
|
|
app.include_router(candles.router)
|
|
app.include_router(sells.router)
|
|
|
|
|
|
@app.on_event("startup")
|
|
def startup():
|
|
# Schema migration: add is_admin column if missing
|
|
with engine.connect() as conn:
|
|
cols = [r[1] for r in conn.execute(text("PRAGMA table_info(users)"))]
|
|
if "is_admin" not in cols:
|
|
conn.execute(text("ALTER TABLE users ADD COLUMN is_admin BOOLEAN DEFAULT 0"))
|
|
conn.commit()
|
|
|
|
# Seed / refresh BTC candle data
|
|
from .services.candles import refresh_latest_candles
|
|
db = SessionLocal()
|
|
try:
|
|
refresh_latest_candles(db)
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
@app.get("/")
|
|
def root():
|
|
return {"message": "BTC Portfolio API"}
|