add postgres

This commit is contained in:
2026-04-07 23:48:47 +03:00
parent 4fff4bebaf
commit 3d29e1f84e
8 changed files with 326 additions and 12 deletions

View File

@@ -4,8 +4,10 @@ import os
import requests
import yaml
from fastapi import FastAPI, HTTPException
from fastapi import Depends, FastAPI, HTTPException
from pydantic import BaseModel
from sqlalchemy import select
from sqlalchemy.orm import Session
app = FastAPI(title="AI CI/CD Assistant")
@@ -17,6 +19,11 @@ def _env(name: str, default: str) -> str:
# Example for local Ollama: http://localhost:11434/api/generate
OLLAMA_API_URL = _env("OLLAMA_API_URL", "http://host.docker.internal:11434/api/generate")
REQUIRE_AUTH_FOR_GENERATE = _env("REQUIRE_AUTH_FOR_GENERATE", "true").lower() in ("1", "true", "yes", "on")
from backend.auth import create_access_token, get_current_user, hash_password, verify_password # noqa: E402
from backend.db import engine, get_db # noqa: E402
from backend.models import Base, User # noqa: E402
class GenerationRequest(BaseModel):
@@ -38,8 +45,69 @@ async def health():
return {"status": "ok"}
@app.on_event("startup")
def _startup():
Base.metadata.create_all(bind=engine)
class RegisterRequest(BaseModel):
email: str
password: str
full_name: str | None = None
class LoginRequest(BaseModel):
email: str
password: str
@app.post("/auth/register")
def register(req: RegisterRequest, db: Session = Depends(get_db)):
email = req.email.strip().lower()
if not email or not req.password:
raise HTTPException(status_code=400, detail="Email and password are required")
exists = db.execute(select(User).where(User.email == email)).scalar_one_or_none()
if exists:
raise HTTPException(status_code=409, detail="Email already registered")
user = User(
email=email,
full_name=req.full_name.strip() if req.full_name else None,
password_hash=hash_password(req.password),
is_active=True,
is_admin=False,
)
db.add(user)
db.commit()
db.refresh(user)
return {"id": str(user.id), "email": user.email, "full_name": user.full_name, "is_admin": user.is_admin}
@app.post("/auth/login")
def login(req: LoginRequest, db: Session = Depends(get_db)):
email = req.email.strip().lower()
user = db.execute(select(User).where(User.email == email)).scalar_one_or_none()
if not user or not verify_password(req.password, user.password_hash):
raise HTTPException(status_code=401, detail="Invalid credentials")
if not user.is_active:
raise HTTPException(status_code=403, detail="User is inactive")
token = create_access_token(user_id=str(user.id))
return {"access_token": token, "token_type": "bearer"}
@app.get("/auth/me")
def me(user: User = Depends(get_current_user)):
return {"id": str(user.id), "email": user.email, "full_name": user.full_name, "is_admin": user.is_admin}
@app.post("/generate")
async def generate_pipeline(req: GenerationRequest):
async def generate_pipeline(
req: GenerationRequest,
user: User | None = Depends(get_current_user) if REQUIRE_AUTH_FOR_GENERATE else None,
):
prompt = (
f"Write only a valid {req.platform} YAML configuration for: {req.description}. "
"No explanations, just code."