-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
196 lines (157 loc) · 6.19 KB
/
main.py
File metadata and controls
196 lines (157 loc) · 6.19 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse, FileResponse
from pydantic import BaseModel
import httpx
import tempfile
from prometheus_fastapi_instrumentator import Instrumentator
from src.READMECreater.GithubFetcher import DownloadRepoFiles
from src.READMECreater.READMEGenerator import GenerateREADME
from src.TagCreater.Models import ExtractJson, ModelThreading
from src.Upload.Upload2DB import (
GetVersion,
SaveGitData,
GetNextReadmeID,
SavingCareerDB,
)
from src.Upload.Upload2Storage import UploadREADME
from src.Utils.GetJWT import GetDataFromToken, GetTokenFromHeader
from src.Utils.GetImage import GetImageInGithub
from src.Utils.DBClient import ReadGithubFromUserID, ReadREADMEDB, ReadImageFromUserID
app = FastAPI(title="GitHub AI API")
instrumentator = Instrumentator().instrument(app).expose(app)
class RepoRequest(BaseModel):
git_url: str
# README 생성 API
@app.post("/api/github-service/readme")
async def GenerateReadme(request: RepoRequest, fast_api: Request):
try:
# README 쓰기
repoURL = request.git_url
repoFiles = DownloadRepoFiles(repoURL)
readmeContent = GenerateREADME(repoURL, repoFiles)
metaData = readmeContent[:1000]
accessToken = GetTokenFromHeader(fast_api)
userID = GetDataFromToken(accessToken, "user_id")
version = GetVersion(repoURL)
readmeID = GetNextReadmeID()
# README Storage & DB에 저장
downloadURL = UploadREADME(readmeContent, userID, repoURL, version)
SaveGitData(version, repoURL, readmeID, userID, downloadURL, metaData)
return {
"status": 200,
"message": "요청에 성공하였습니다.",
"data": downloadURL, # 스토리지 내에 데이터를 user에게 streaming 하는 API Call
}
except Exception:
return JSONResponse(
status_code=400,
content={"status": 400, "message": "README 생성 실패", "data": None},
)
# README 다운로드 요청 처리 API
@app.get("/api/github-service/download")
async def DownloadFile(downloadURL: str):
async with httpx.AsyncClient() as client:
try:
resp = await client.get(downloadURL)
resp.raise_for_status()
# 다운로드를 위한 임시 파일 생성
with tempfile.NamedTemporaryFile(delete=False, suffix=".md") as tmp:
tmp.write(resp.content)
tmpPath = tmp.name
return FileResponse(
path=tmpPath,
filename="README.md",
media_type="application/octet-stream",
headers={"Content-Disposition": 'attachment; filename="README.md"'},
)
except Exception:
return JSONResponse(
status_code=400,
content={
"status": 400,
"message": "README 다운로드 실패",
"data": None,
},
)
# Tag 생성 API
@app.post("/api/github-service/tag")
async def GenerateTag(request: RepoRequest, fast_api: Request):
try:
githubURL = request.git_url
response = ModelThreading(githubURL)
tags = ExtractJson(response.text)
accessToken = GetTokenFromHeader(fast_api)
userID = GetDataFromToken(accessToken, "user_id")
imageURL = GetImageInGithub(githubURL)
SavingCareerDB(tags, userID, githubURL, imageURL)
return {
"status": 200,
"message": "요청에 성공하였습니다.",
"data": tags,
}
except Exception as e:
print("[ERROR] Exception in GenerateTag:", e)
import traceback
traceback.print_exc()
return JSONResponse(
status_code=400,
content={"status": 400, "message": f"Tag 생성 실패: {e}", "data": None},
)
# 유저가 올린 모든 github 정보 읽기
@app.get("/api/github-service/db/user")
async def ReadUserGithub(request: Request):
try:
accessToken = GetTokenFromHeader(request)
userID = GetDataFromToken(accessToken, "user_id")
data = ReadGithubFromUserID(userID)
return {
"status": 200,
"message": "요청에 성공하였습니다.",
"data": data,
}
except Exception:
return JSONResponse(
status_code=400,
content={"status": 400, "message": "DB read 실패", "data": None},
)
# 유저가 올린 github url의 README 데이터 읽기
@app.get("/api/github-service/db/readme")
async def ReadREADME(userID: int, gitURL: str):
try:
# README 데이터 읽기
data = ReadREADMEDB(userID, gitURL)
if not data:
return JSONResponse(
status_code=404,
content={
"status": 404,
"message": "해당 README 데이터를 찾을 수 없습니다.",
"data": None,
},
)
firstData = data[0]
# 이미지 URL 가져오기
imgResult = ReadImageFromUserID(userID, gitURL)
imgURL = None
if isinstance(imgResult, dict) and "result" in imgResult:
imgURL = imgResult["result"].get("img_url")
elif isinstance(imgResult, str): # img_url이 직접 반환될 수도 있음
imgURL = imgResult
print(f"Image URL: {imgURL}")
# 결과 데이터에 img_url 추가
firstData["img_url"] = imgURL if imgURL else ""
return {
"status": 200,
"message": "요청에 성공하였습니다.",
"data": firstData,
}
except Exception as e:
print(f"Error: {e}")
return JSONResponse(
status_code=400,
content={"status": 400, "message": "DB read 실패", "data": None},
)
# 헬스 체크
@app.get("/api/github-service/health-check")
async def HealthCheck():
return {"status": 200, "message": "서버 상태 확인", "data": "Working"}