Update main.py
Browse files
main.py
CHANGED
|
@@ -1,14 +1,18 @@
|
|
| 1 |
-
from fastapi import FastAPI, File, UploadFile, Form,
|
| 2 |
from fastapi.responses import PlainTextResponse, FileResponse, HTMLResponse
|
| 3 |
from fastapi.requests import Request
|
| 4 |
from fastapi.staticfiles import StaticFiles
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
from typing import Optional
|
| 7 |
from starlette.exceptions import HTTPException as StarletteHTTPException
|
|
|
|
|
|
|
| 8 |
from services.client import generate_code_comments_and_docs
|
| 9 |
|
| 10 |
# β
Initialize FastAPI app
|
| 11 |
app = FastAPI()
|
|
|
|
|
|
|
| 12 |
app.add_middleware(
|
| 13 |
CORSMiddleware,
|
| 14 |
allow_origins=["*"],
|
|
@@ -17,28 +21,33 @@ app.add_middleware(
|
|
| 17 |
allow_headers=["*"],
|
| 18 |
)
|
| 19 |
|
| 20 |
-
#
|
| 21 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 22 |
|
| 23 |
|
|
|
|
| 24 |
@app.get("/", response_class=HTMLResponse)
|
| 25 |
async def serve_index():
|
| 26 |
return FileResponse("static/index.html")
|
| 27 |
|
| 28 |
-
|
|
|
|
| 29 |
@app.post("/generate-docs")
|
| 30 |
async def generate_docs(
|
| 31 |
file: Optional[UploadFile] = File(None),
|
| 32 |
code: Optional[str] = Form(None)
|
| 33 |
-
|
| 34 |
try:
|
|
|
|
| 35 |
if not file and not code:
|
| 36 |
raise HTTPException(status_code=400, detail="You must provide either a file or raw code.")
|
| 37 |
|
|
|
|
| 38 |
elif code:
|
| 39 |
response = generate_code_comments_and_docs(code)
|
| 40 |
return response
|
| 41 |
|
|
|
|
| 42 |
elif file:
|
| 43 |
content = await file.read()
|
| 44 |
code = content.decode("utf-8")
|
|
@@ -51,8 +60,10 @@ async def generate_docs(
|
|
| 51 |
except Exception as ex:
|
| 52 |
return PlainTextResponse(f"β Server error: {str(ex)}", status_code=500)
|
| 53 |
|
|
|
|
|
|
|
| 54 |
@app.exception_handler(StarletteHTTPException)
|
| 55 |
async def custom_404_handler(request: Request, exc: StarletteHTTPException):
|
| 56 |
if exc.status_code == 404:
|
| 57 |
-
return FileResponse("static/not-found.html"
|
| 58 |
-
return
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile, Form, HTTPException
|
| 2 |
from fastapi.responses import PlainTextResponse, FileResponse, HTMLResponse
|
| 3 |
from fastapi.requests import Request
|
| 4 |
from fastapi.staticfiles import StaticFiles
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
from typing import Optional
|
| 7 |
from starlette.exceptions import HTTPException as StarletteHTTPException
|
| 8 |
+
|
| 9 |
+
# Import your custom AI service
|
| 10 |
from services.client import generate_code_comments_and_docs
|
| 11 |
|
| 12 |
# β
Initialize FastAPI app
|
| 13 |
app = FastAPI()
|
| 14 |
+
|
| 15 |
+
# Add CORS middleware to allow cross-origin requests if needed
|
| 16 |
app.add_middleware(
|
| 17 |
CORSMiddleware,
|
| 18 |
allow_origins=["*"],
|
|
|
|
| 21 |
allow_headers=["*"],
|
| 22 |
)
|
| 23 |
|
| 24 |
+
# β
Mount the static folder so FastAPI can serve HTML, CSS, and JS files
|
| 25 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 26 |
|
| 27 |
|
| 28 |
+
# β
Serve the main frontend page
|
| 29 |
@app.get("/", response_class=HTMLResponse)
|
| 30 |
async def serve_index():
|
| 31 |
return FileResponse("static/index.html")
|
| 32 |
|
| 33 |
+
|
| 34 |
+
# β
Main API route to handle code generation
|
| 35 |
@app.post("/generate-docs")
|
| 36 |
async def generate_docs(
|
| 37 |
file: Optional[UploadFile] = File(None),
|
| 38 |
code: Optional[str] = Form(None)
|
| 39 |
+
):
|
| 40 |
try:
|
| 41 |
+
# Check if neither file nor code was provided
|
| 42 |
if not file and not code:
|
| 43 |
raise HTTPException(status_code=400, detail="You must provide either a file or raw code.")
|
| 44 |
|
| 45 |
+
# If raw text code is provided
|
| 46 |
elif code:
|
| 47 |
response = generate_code_comments_and_docs(code)
|
| 48 |
return response
|
| 49 |
|
| 50 |
+
# If a file is uploaded
|
| 51 |
elif file:
|
| 52 |
content = await file.read()
|
| 53 |
code = content.decode("utf-8")
|
|
|
|
| 60 |
except Exception as ex:
|
| 61 |
return PlainTextResponse(f"β Server error: {str(ex)}", status_code=500)
|
| 62 |
|
| 63 |
+
|
| 64 |
+
# β
Custom 404 Error Handler to serve your not-found.html
|
| 65 |
@app.exception_handler(StarletteHTTPException)
|
| 66 |
async def custom_404_handler(request: Request, exc: StarletteHTTPException):
|
| 67 |
if exc.status_code == 404:
|
| 68 |
+
return FileResponse("static/not-found.html")
|
| 69 |
+
return PlainTextResponse(str(exc.detail), status_code=exc.status_code)
|