-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
172 lines (148 loc) · 5.02 KB
/
server.js
File metadata and controls
172 lines (148 loc) · 5.02 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
// =========================================================================
// LangChain 기반 코드 생성 추천기
// =========================================================================
// LangChain: 대규모 언어 모델(LLM)을 활용한 애플리케이션 개발을 위한 프레임워크
// RAG(Retrieval Augmented Generation): 외부 데이터를 검색하여 LLM의 응답을 강화하는 기법
// =========================================================================
// LangSmith 콜백 백그라운드 처리 설정 (LangChain 경고 해결을 위해 추가)
process.env.LANGCHAIN_CALLBACKS_BACKGROUND = "true";
// 기본 의존성 로드
require("dotenv").config(); // .env 파일에서 환경 변수 로드
const express = require("express"); // 웹 서버 프레임워크
const session = require("express-session");
const passport = require("passport");
const connectDB = require("./config/database");
const config = require("./config/config");
const apiRoutes = require("./routes/api");
const { getMainTemplate } = require("./utils/frontend/templates");
const GitHubStrategy = require("passport-github2").Strategy;
const { Octokit } = require("@octokit/rest");
const MongoStore = require("connect-mongo");
// =========================================================================
// 환경변수 확인
// =========================================================================
const apiKey = process.env.OPENAI_API_KEY;
if (!apiKey || apiKey === "your-api-key") {
console.error(
"⚠️ OpenAI API 키가 설정되지 않았습니다. .env 파일에 OPENAI_API_KEY를 설정해주세요."
);
process.exit(1);
}
// Express 앱 설정
const app = express();
// 미들웨어 설정
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static("public"));
// 세션 설정
app.use(
session({
secret: config.session.secret,
resave: false,
saveUninitialized: false,
cookie: { secure: false },
store: MongoStore.create({
mongoUrl: process.env.MONGODB_URI,
ttl: 24 * 60 * 60, // 1일
}),
})
);
// Passport 초기화
app.use(passport.initialize());
app.use(passport.session());
// Passport 설정
passport.serializeUser((user, done) => {
done(null, user);
});
passport.deserializeUser((user, done) => {
done(null, user);
});
// GitHub 전략 설정
passport.use(
new GitHubStrategy(
{
clientID: config.github.clientId,
clientSecret: config.github.clientSecret,
callbackURL: config.github.callbackUrl,
scope: ["repo", "user"],
},
async (accessToken, refreshToken, profile, done) => {
try {
const octokit = new Octokit({ auth: accessToken });
// 사용자의 저장소 목록 가져오기
const { data: repos } =
await octokit.rest.repos.listForAuthenticatedUser({
sort: "updated",
per_page: 100,
});
// 저장소 정보 정리
const repositories = repos.map((repo) => ({
fullName: repo.full_name,
name: repo.name,
description: repo.description,
language: repo.language,
stars: repo.stargazers_count,
}));
// 사용자 프로필 정보 정리
const userProfile = {
id: profile.id,
username: profile.username,
displayName: profile.displayName,
avatar: profile.photos[0].value,
repositories,
accessToken,
};
return done(null, userProfile);
} catch (error) {
console.error("GitHub 인증 중 오류 발생:", error);
return done(error, null);
}
}
)
);
// =========================================================================
// API 라우트 설정
// =========================================================================
// 라우트 설정
app.use("/api", apiRoutes);
// 기본 HTML 페이지 제공
app.get("/", (req, res) => {
const html = getMainTemplate(req.isAuthenticated(), req.user);
res.send(html);
});
// GitHub OAuth 라우트
app.get("/auth/github", passport.authenticate("github"));
app.get(
"/auth/github/callback",
passport.authenticate("github", { failureRedirect: "/" }),
(req, res) => {
res.redirect("/");
}
);
app.get("/logout", (req, res) => {
req.logout(() => {
res.redirect("/");
});
});
// =========================================================================
// 서버 시작 및 초기화
// =========================================================================
// 서버 시작 함수
async function startServer() {
try {
// MongoDB 연결
await connectDB();
// 서버 시작
app.listen(config.server.port, () => {
console.log(`🚀 서버가 포트 ${config.server.port}에서 실행 중입니다.`);
console.log(
`💻 웹 브라우저에서 http://localhost:${config.server.port}/ 를 열어 앱을 사용해보세요.`
);
});
} catch (error) {
console.error("❌ 서버 시작 중 오류 발생:", error);
process.exit(1);
}
}
// 서버 시작
startServer();