-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
205 lines (177 loc) · 5.35 KB
/
main.js
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
197
198
199
200
201
202
203
204
205
/**
*
* main.js
* Back-end server
*
* Created: 2024-01-12
* Last modified: -
*
*/
const DEBUG = true;
const express = require('express');
const axios = require('axios');
const { authConfig } = require('./config/config');
const { sql, poolPromise } = require('./config/server');
const { SqlConnector } = require('./db');
const { default_goals } = require('./default_goals');
const bodyParser = require('body-parser');
const app = express();
const PORT = 3000;
/**
* @type {SqlConnector}
*/
let sqlConn;
// Enable CORS for all routes
app.use(bodyParser.json());
app.use(async (_, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
next();
});
// Run server
app.listen(PORT, async () => {
sqlConn = new SqlConnector(await poolPromise);
if (DEBUG) sqlConn.setUser('[email protected]');
console.log('Connected to TastyNav database.');
console.log(`Listening to port ${PORT}...`);
});
//루트 페이지
//로그인 버튼을 누르면 GET /login으로 이동
app.get('/', (_, res) => {
res.send(`<a href="/auth">Log in</a>
<a href="/signup">Sign up</a>`);
});
// 로그인 버튼을 누르면 도착하는 목적지 라우터
// 모든 로직을 처리한 뒤 구글 인증서버인 https://accounts.google.com/o/oauth2/v2/auth
// 으로 redirect 되는데, 이 url에 첨부할 몇가지 QueryString들이 필요
app.get('/auth', (_, res) => {
let url = 'https://accounts.google.com/o/oauth2/v2/auth';
url += `?client_id=${authConfig.clientID}`;
url += `&redirect_uri=${authConfig.redirectUri}`;
url += '&response_type=code';
url += '&scope=email profile';
res.redirect(url);
});
app.get('/auth/google', async (req, res) => {
const { code } = req.query;
console.log(`code: ${code}`);
const resp = await axios.post(authConfig.tokenUrl, {
code,
client_id: authConfig.clientID,
client_secret: authConfig.clientSecret,
redirect_uri: authConfig.redirectUri,
grant_type: 'authorization_code',
});
const resp2 = await axios.get(authConfig.userinfoUrl, {
headers: { Authorization: `Bearer ${resp.data.access_token}` },
});
res.json(resp2.data['email']);
sqlConn.setUser(resp2.data['email']);
try {
for (let i = 1; i < 13; i++) {
await sqlConn.addGoal(i, 1, default_goals[i]);
}
} catch (err) {
res.status(500).send();
}
});
app.get('/signup', (req, res) => {
let url = 'https://accounts.google.com/o/oauth2/v2/auth';
url += `?client_id=${authConfig.clientID}`;
url += `&redirect_uri=${authConfig.signupUri}`;
url += '&response_type=code';
url += '&scope=email profile';
res.redirect(url);
});
// 회원가입
app.get('/signup/google', async (req, res) => {
const { code } = req.query;
console.log(`code: ${code}`);
const resp = await axios.post(authConfig.tokenUrl, {
code,
client_id: authConfig.clientID,
client_secret: authConfig.clientSecret,
redirect_uri: authConfig.signupUri,
grant_type: 'authorization_code',
});
const resp2 = await axios.get(authConfig.userinfoUrl, {
headers: {
Authorization: `Bearer ${resp.data.access_token}`,
},
});
res.json(resp2.data);
});
//목록에 추가
app.post('/addgoal', async (req, res) => {
try {
const add_info = req.body;
await sqlConn.addGoal(add_info.month, add_info.id, add_info.goal);
res.status(200).send();
} catch (err) {
res.status(500).send(req.body);
}
});
//목록 수정
app.post('/editgoal', async (req, res) => {
try {
const edit_info = req.body;
await sqlConn.modifyGoal(
edit_info.month,
edit_info.id,
edit_info.newGoal
);
res.status(200).send();
} catch (err) {
res.status(500).send();
}
});
//목록 삭제
app.delete('/deletegoal/:month/:id', async (req, res) => {
try {
let { id, month } = req.params;
await sqlConn.deleteGoal(month, id);
res.status(200).send();
} catch (err) {
res.status(500).send();
}
});
//목록 읽기
app.get('/getgoal/:month', async (req, res) => {
try {
let { month } = req.params;
const result = await sqlConn.getGoals(sqlConn.user, month);
res.send(result);
} catch (err) {
res.status(500).send();
}
});
app.get('/user/search/:query', async (req, res) => {
try {
let { query } = req.params;
const friends = await sqlConn.searchFriends(query);
if (friends.length === 0) res.status(404).send();
else res.send(friends);
} catch (err) {
res.status(500).send();
}
});
app.post('/user/follow', async (req, res) => {
try {
const user_info = req.body;
await sqlConn.addFollowing(user_info.user);
res.status(200).send();
} catch (err) {
res.status(500).send();
}
});
app.get('/user/followings', async (req, res) => {
try {
// getFollowerings 함수를 이용
const friendsList = await sqlConn.getFollowings();
// 프론트엔드에 목록을 응답
res.send(friendsList);
} catch (error) {
res.status(500).json({ error: 'Internal Server Error' });
}
});