-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
80 lines (67 loc) · 2.01 KB
/
main.py
File metadata and controls
80 lines (67 loc) · 2.01 KB
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
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from app.core.config import settings
from app.api.endpoints import pdf
import os
import subprocess
import time
enable_docs = settings.enable_docs or settings.environment in ("dev", "staging")
app = FastAPI(
title=settings.api_title,
version=settings.api_version,
description=settings.api_description,
docs_url="/docs" if enable_docs else None,
redoc_url="/redoc" if enable_docs else None,
openapi_url="/openapi.json" if enable_docs else None,
)
START_TIME = time.time()
def _git_commit_short() -> str:
# Prefer env var if provided
commit = os.getenv("GIT_COMMIT")
if commit:
return commit[:7]
# Try git command
try:
out = subprocess.run([
"git", "rev-parse", "--short", "HEAD"
], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
return out.stdout.decode().strip()
except Exception:
return "unknown"
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=settings.cors_origins,
allow_credentials=settings.cors_allow_credentials,
allow_methods=settings.cors_allow_methods,
allow_headers=settings.cors_allow_headers,
)
# Include routers
app.include_router(pdf.router)
@app.get("/")
async def root():
return {
"message": "PDF Dieline Processor API",
"version": settings.api_version,
"endpoints": {
"analyze": "/api/pdf/analyze",
"process": "/api/pdf/process",
"process_with_json": "/api/pdf/process-with-json-file"
}
}
@app.get("/hello/{name}")
async def say_hello(name: str):
return {"message": f"Hello {name}"}
@app.get("/healthz")
async def healthz():
return {
"status": "ok",
"uptime_seconds": round(time.time() - START_TIME, 2)
}
@app.get("/version")
async def version():
return {
"name": settings.api_title,
"version": settings.api_version,
"git_commit": _git_commit_short(),
}