-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.py
More file actions
94 lines (74 loc) ยท 2.58 KB
/
main.py
File metadata and controls
94 lines (74 loc) ยท 2.58 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import logging
import os
from dotenv import load_dotenv
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from .routers import certificate
from .routers.logging import logging_router
from .utils.access_log import access_log_middleware, close_access_log, init_access_log
def configure_logging() -> None:
"""๊ธฐ๋ณธ ๋ก๊น
์ค์ ์ ๊ตฌ์ฑํ๋ค."""
log_level = os.getenv("LOG_LEVEL", "INFO").upper()
logging.basicConfig(
level=log_level,
format="%(asctime)s [%(levelname)s] %(name)s - %(message)s",
)
# .env ํ์ผ ๋ก๋ ๋ฐ ๋ก๊น
์ค์
load_dotenv()
configure_logging()
logger = logging.getLogger(__name__)
environment = os.getenv("ENVIRONMENT", "dev").lower()
hide_docs = environment.startswith("prod")
# FastAPI ์ฑ ์์ฑ
app = FastAPI(
title="PseudoLab ์๋ฃ์ฆ ๋ฐ๊ธ ์์คํ
",
description="PseudoLab ์๋ฃ์ฆ ๋ฐ๊ธ์ ์ํ API ์๋ฒ",
version="1.0.0",
docs_url=None if hide_docs else "/docs",
redoc_url=None if hide_docs else "/redoc",
openapi_url=None if hide_docs else "/openapi.json",
)
# Access log middleware
app.middleware("http")(access_log_middleware)
# CORS configuration
# Load allowed origins from CORS_ORIGINS environment variable (comma-separated)
cors_origins_str = os.getenv("CORS_ORIGINS", "")
if cors_origins_str:
origins = [origin.strip() for origin in cors_origins_str.split(",") if origin.strip()]
else:
# Default origins for local development and known production/dev domains
origins = [
"http://localhost:3000",
"http://localhost:5173",
"https://cert.pseudo-lab.com",
"https://dev-cert.pseudolab-devfactory.com",
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# ๋ชจ๋ ํ๊ฒฝ์์ /api ํ๋ฆฌํฝ์ค ์ฌ์ฉ (๊ฐ๋ฐ/ํ๋ก๋์
ํต์ผ)
app.include_router(certificate.certificate_router, prefix="/api")
app.include_router(logging_router, prefix="/api")
logger.info("FastAPI app initialized", extra={"environment": os.getenv("ENVIRONMENT")})
@app.get("/")
async def read_root():
"""๋ฃจํธ ์๋ํฌ์ธํธ"""
return {
"message": "PseudoLab ์๋ฃ์ฆ ๋ฐ๊ธ ์์คํ
API ์๋ฒ",
"version": "1.0.0",
"status": "running",
}
@app.get("/health")
async def health_check():
"""ํฌ์ค ์ฒดํฌ ์๋ํฌ์ธํธ"""
return {"status": "healthy"}
@app.on_event("startup")
async def setup_access_log():
await init_access_log(app)
@app.on_event("shutdown")
async def teardown_access_log():
await close_access_log(app)