first commit
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
import sys
|
||||
import os
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from database import engine
|
||||
from models.models import Base
|
||||
from routes import auth, plans, generate
|
||||
|
||||
Base.metadata.create_all(bind=engine)
|
||||
|
||||
# Migration helpers for existing databases - MySQL compatible
|
||||
try:
|
||||
import sqlalchemy
|
||||
with engine.connect() as conn:
|
||||
# Check existing columns in users table
|
||||
result = conn.execute(sqlalchemy.text(
|
||||
"SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'users'"
|
||||
))
|
||||
columns = [row[0] for row in result]
|
||||
if 'xp' not in columns:
|
||||
conn.execute(sqlalchemy.text("ALTER TABLE users ADD COLUMN xp INTEGER NOT NULL DEFAULT 0"))
|
||||
conn.commit()
|
||||
|
||||
# Check existing columns in tasks table
|
||||
result = conn.execute(sqlalchemy.text(
|
||||
"SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'tasks'"
|
||||
))
|
||||
task_columns = [row[0] for row in result]
|
||||
if 'priority' not in task_columns:
|
||||
conn.execute(sqlalchemy.text("ALTER TABLE tasks ADD COLUMN priority VARCHAR(10) NOT NULL DEFAULT 'medium'"))
|
||||
conn.commit()
|
||||
if 'due_date' not in task_columns:
|
||||
conn.execute(sqlalchemy.text("ALTER TABLE tasks ADD COLUMN due_date DATE NULL"))
|
||||
conn.commit()
|
||||
if 'sort_order' not in task_columns:
|
||||
conn.execute(sqlalchemy.text("ALTER TABLE tasks ADD COLUMN sort_order INTEGER NOT NULL DEFAULT 0"))
|
||||
conn.commit()
|
||||
except Exception as e:
|
||||
print(f"Migration warning (safe to ignore on first run): {e}")
|
||||
|
||||
app = FastAPI(title="Goal Planner API", version="1.0.0")
|
||||
|
||||
origins = [
|
||||
"http://localhost:3000",
|
||||
"http://127.0.0.1:3000",
|
||||
]
|
||||
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=origins,
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
app.include_router(auth.router)
|
||||
app.include_router(plans.router)
|
||||
app.include_router(generate.router)
|
||||
|
||||
@app.get("/api/health")
|
||||
def health_check():
|
||||
return {"status": "ok"}
|
||||
Reference in New Issue
Block a user