File size: 2,597 Bytes
34b6cef
 
 
 
 
 
 
557bf7c
34b6cef
 
 
 
 
 
 
 
 
dc7a25e
34b6cef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
557bf7c
34b6cef
557bf7c
 
34b6cef
 
 
557bf7c
34b6cef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
557bf7c
34b6cef
557bf7c
 
34b6cef
 
 
557bf7c
34b6cef
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse

from src.workflows.crew import run_systemforge
from src.api.pdf_generator import generate_architecture_pdf
import traceback

app = FastAPI(
    title="SystemForge API"
)


# CORS for Next.js frontend
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)


class WorkflowRequest(BaseModel):
    workflow: list[str]


@app.get("/")
def root():
    return {
        "message": "SystemForge API Running"
    }


@app.post("/run-systemforge")
def generate_architecture(
    request: WorkflowRequest
):
    try:
        if not request.workflow:
            raise HTTPException(
                status_code=400,
                detail="Workflow steps cannot be empty"
            )

        result = run_systemforge(
            request.workflow
        )

        return result

    except HTTPException:
        raise

    except Exception as e:
        print("========== FULL BACKEND ERROR ==========")
        print(f"SystemForge Error: {str(e)}")
        traceback.print_exc()
        print("========================================")

        raise HTTPException(
            status_code=500,
            detail=f"Failed to generate architecture: {str(e)}"
        )


@app.post("/download-report")
def download_report(
    request: WorkflowRequest
):
    """
    Generates PDF architecture report
    and returns downloadable file
    """

    try:
        if not request.workflow:
            raise HTTPException(
                status_code=400,
                detail="Workflow steps cannot be empty"
            )

        # Generate architecture first
        result = run_systemforge(
            request.workflow
        )

        # Create PDF
        file_path = generate_architecture_pdf(
            data=result,
            filename="architecture_report.pdf"
        )

        return FileResponse(
            path=file_path,
            filename="SystemForge_Architecture_Report.pdf",
            media_type="application/pdf"
        )

    except HTTPException:
        raise

    except Exception as e:
        print("========== FULL BACKEND ERROR ==========")
        print(f"PDF Generation Error: {str(e)}")
        traceback.print_exc()
        print("========================================")

        raise HTTPException(
            status_code=500,
            detail=f"Failed to download pdf: {str(e)}"
        )