-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
124 lines (97 loc) · 3.32 KB
/
main.py
File metadata and controls
124 lines (97 loc) · 3.32 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import logging
import os
from fastapi import FastAPI
from fastapi.openapi.utils import get_openapi
from fastapi.middleware.cors import CORSMiddleware
from routers import analysis, user, keyword, search
from pydantic import BaseModel
from dotenv import load_dotenv
from routers.analysis import load_model
from routers.search import search_naver, search_datalab
from routers.analysis import analyze_sentiment
from fastapi import Depends
from sqlalchemy.orm import Session
from database import get_db
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from datetime import datetime
import asyncio
load_dotenv()
load_model()
# 로깅 설정
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
app = FastAPI(
title="TreAna API",
description="API for analyze trend services",
version="1.0.0",
)
scheduler = AsyncIOScheduler()
# CORS 미들웨어 설정
origins = [
"http://127.0.0.1:8000",
"http://localhost:5173", # 클라이언트 개발환경
os.getenv("FRONTEND_DOMAIN"), # 배포된 프론트엔드 도메인
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins, # 허용할 도메인
allow_credentials=True,
allow_methods=["*"], # 모든 HTTP 메서드 허용
allow_headers=["*"], # 모든 헤더 허용
)
# 라우터 등록
app.include_router(user.router, prefix="/users", tags=["user"])
app.include_router(analysis.router, prefix="/analysis", tags=["analysis"])
app.include_router(keyword.router, prefix="/keywords", tags=["keywords"])
app.include_router(search.router, prefix="/search", tags=["search"])
# OpenAPI 스키마 수정
def custom_openapi():
if app.openapi_schema:
return app.openapi_schema
openapi_schema = get_openapi(
title="Treana API",
version="1.0.0",
description="API documentation for Treana",
routes=app.routes,
)
# BearerAuth 보안 설정
openapi_schema["components"]["securitySchemes"] = {
"BearerAuth": {
"type": "http",
"scheme": "bearer",
"bearerFormat": "JWT"
}
}
openapi_schema["security"] = [{"BearerAuth": []}]
for path, methods in openapi_schema["paths"].items():
for method, details in methods.items():
if "parameters" in details:
# Authorization 쿼리 파라미터 제거
details["parameters"] = [
param for param in details["parameters"]
if param.get("name") != "authorization"
]
app.openapi_schema = openapi_schema
return app.openapi_schema
app.openapi = custom_openapi
@app.post("/result")
async def get_result(keyword: str):
# 정확도 기반 검색
result = []
data = search_naver(keyword)
for desc, date in data:
sentiment = analyze_sentiment(desc)
result.append({"date": date, "description": desc, "sentiment": sentiment["predicted_class_label"]})
return result
@app.post("/datalab")
async def get_datalab(body: dict):
result = search_datalab(body)
# 결과 반환
return result
@app.get("/health")
def health_check():
return {"status": "healthy"}
async def alert_auto_reports():
await keyword.generate_reports()
scheduler.add_job(alert_auto_reports, 'cron', hour=9, minute=0, timezone='Asia/Seoul')
scheduler.start()