File size: 2,766 Bytes
0e3f40f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
78
79
80
81
82
83
\"\"\"
OmniParse AI — FastAPI Backend (HTML UI Ready).
Tento soubor slouží jako most mezi CORE logikou v app.py a budoucím HTML/JS frontendem.
Všechny business funkce se importují z app.py.
\"\"\"

import os
from fastapi import FastAPI, UploadFile, File, Header, HTTPException, Depends
from fastapi.responses import JSONResponse
from fastapi.middleware.cors import CORSMiddleware
import app as core

app = FastAPI(title="OmniParse AI API")

# CORS pro HTML/JS frontend
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_methods=["*"],
    allow_headers=["*"],
)

# --- AUTH ENDPOINTY ---

@app.post("/api/auth/signup")
async def api_signup(data: dict):
    ok, msg, token = core.signup(data.get("name"), data.get("email"), data.get("password"), data.get("accepted_terms"))
    if not ok:
        raise HTTPException(status_code=400, detail=msg)
    return {"message": msg, "token": token}

@app.post("/api/auth/login")
async def api_login(data: dict):
    ok, msg, token = core.login(data.get("email"), data.get("password"))
    if not ok:
        raise HTTPException(status_code=401, detail=msg)
    return {"message": msg, "token": token}

# --- BUSINESS ENDPOINTY (vyžadují token nebo API klíč) ---

async def get_current_user(authorization: str = Header(None)):
    if not authorization:
        raise HTTPException(status_code=401, detail="Missing Authorization header")
    
    token = authorization.replace("Bearer ", "")
    
    # Zkusíme session token (pro UI)
    user = core.resolve_session(token)
    if user:
        return user
        
    # Zkusíme API klíč (pro externí integrace)
    # Poznámka: v SQLiteDB/SupabaseDB by se musela přidat funkce get_user_by_api_key
    # Pro demo účely budeme hledat v DB ručně
    db_user = core.DB.get_user_by_email("demo@omniparse.ai") # Placeholder pro ukázku
    if db_user and db_user.get("api_key") == token:
        return db_user
        
    raise HTTPException(status_code=401, detail="Invalid token or API key")

@app.post("/api/extract")
async def api_extract(file: UploadFile = File(...), user: dict = Depends(get_current_user)):
    # Uložíme dočasně soubor
    temp_path = f"/tmp/{file.filename}"
    with open(temp_path, "wb") as buffer:
        buffer.write(await file.read())
    
    # Zavoláme CORE pipeline
    result = core.process_invoice_file(user, temp_path, file.filename)
    
    if "error" in result:
        raise HTTPException(status_code=500, detail=result["error"])
        
    return result

@app.get("/api/invoices")
async def api_get_invoices(user: dict = Depends(get_current_user)):
    return core.DB.get_invoices(user["id"])

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)