-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
286 lines (235 loc) · 10.5 KB
/
server.js
File metadata and controls
286 lines (235 loc) · 10.5 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// server.js
import express from 'express';
import cookieParser from 'cookie-parser';
import cors from 'cors';
import db from './db.js';
const app = express();
// ───────────────────────────────────────────────────────────────
// 기본 미들웨어
// ───────────────────────────────────────────────────────────────
app.use(express.json());
app.use(cookieParser());
// CORS 허용: 정적 프론트(라이브서버 5500) + 예비(3000/5173)
const allowedOrigins = [
'http://localhost:5500',
'http://127.0.0.1:5500',
'http://localhost:3000',
'http://localhost:5173',
];
// 프리플라이트 포함 CORS 처리
const corsOptions = {
origin(origin, cb) {
// curl/서버-서버 호출 등 Origin 없는 경우 허용
if (!origin) return cb(null, true);
if (allowedOrigins.includes(origin)) return cb(null, true);
return cb(new Error(`CORS blocked: ${origin}`), false);
},
credentials: true,
methods: ['GET', 'POST', 'DELETE', 'OPTIONS'],
allowedHeaders: ['Content-Type'],
};
app.use(cors(corsOptions));
app.options('*', cors(corsOptions)); // preflight
// ───────────────────────────────────────────────────────────────
// 유틸 함수
// ───────────────────────────────────────────────────────────────
const getUser = (req) => {
const name = req.cookies?.user || null;
if (!name) return null;
const row = db.prepare('SELECT * FROM users WHERE name = ?').get(name);
return row || null;
};
const ensureUser = (name) => {
db.prepare('INSERT OR IGNORE INTO users(name) VALUES(?)').run(name);
return db.prepare('SELECT * FROM users WHERE name = ?').get(name);
};
// ───────────────────────────────────────────────────────────────
// 인증 관련 API
// ───────────────────────────────────────────────────────────────
app.post('/api/login', (req, res) => {
const { name } = req.body || {};
if (!name) return res.status(400).json({ error: 'name required' });
const user = ensureUser(name);
// 과제/테스트용: 프론트 JS에서 읽을 수 있게 httpOnly: false
// 로컬호스트용이라 secure: false, 크로스탭 기본 호환을 위해 sameSite: 'lax'
res.cookie('user', user.name, {
httpOnly: false,
sameSite: 'lax',
secure: false,
});
res.json({ ok: true, user: user.name });
});
app.post('/api/logout', (req, res) => {
res.clearCookie('user');
res.json({ ok: true });
});
app.get('/api/me', (req, res) => {
const u = getUser(req);
res.json({ user: u ? u.name : null });
});
// ───────────────────────────────────────────────────────────────
// 검색 & 추천
// ───────────────────────────────────────────────────────────────
app.get('/api/search', (req, res) => {
const q = (req.query.q || '').toLowerCase();
const mood = req.query.mood || null;
const rows = db.prepare(`
SELECT t.*, GROUP_CONCAT(tm.mood) AS moods
FROM tracks t
LEFT JOIN track_moods tm ON tm.track_id = t.id
GROUP BY t.id
`).all();
const filtered = rows.filter(r => {
const moods = (r.moods || '').split(',').filter(Boolean);
const matchQ = !q || [r.title, r.original_title, r.artist].join(' ').toLowerCase().includes(q);
const matchM = !mood || moods.includes(mood);
return matchQ && matchM;
});
res.json({ items: filtered });
});
app.get('/api/recs', (req, res) => {
const mood = req.query.mood || null;
const user = getUser(req);
const uid = user?.id;
const rows = db.prepare(`
SELECT t.*, GROUP_CONCAT(tm.mood) AS moods
FROM tracks t
LEFT JOIN track_moods tm ON tm.track_id = t.id
GROUP BY t.id
`).all();
const ratingsMap = {};
if (uid) {
db.prepare('SELECT track_id, mood FROM ratings WHERE user_id = ?')
.all(uid)
.forEach(r => { ratingsMap[r.track_id] = r.mood; });
}
const score = (t) => {
const moods = (t.moods || '').split(',').filter(Boolean);
let s = 0;
if (mood && moods.includes(mood)) s += 2;
if (ratingsMap[t.id] && ratingsMap[t.id] === mood) s += 3;
if (/official|mv|audio/i.test(t.original_title)) s += 0.5;
return s;
};
const sorted = rows.sort((a, b) => score(b) - score(a));
res.json({ items: sorted.slice(0, 20) });
});
// ───────────────────────────────────────────────────────────────
// 감정 라벨 & 시청 기록
// ───────────────────────────────────────────────────────────────
app.post('/api/ratings', (req, res) => {
const user = getUser(req);
const { trackId, mood } = req.body || {};
if (!trackId || !mood) return res.status(400).json({ error: 'trackId & mood required' });
if (!user) {
// 비로그인: DB 저장 없이 성공 응답 (프론트는 로컬에 저장/표시)
return res.json({ ok: true, anonymous: true });
}
db.prepare(`
INSERT INTO ratings (user_id, track_id, mood, created_at)
VALUES (?, ?, ?, strftime('%s','now'))
ON CONFLICT(user_id, track_id)
DO UPDATE SET mood = excluded.mood, created_at = excluded.created_at
`).run(user.id, trackId, mood);
res.json({ ok: true });
});
app.post('/api/watch', (req, res) => {
const user = getUser(req);
const { trackId } = req.body || {};
if (!trackId) return res.status(400).json({ error: 'trackId required' });
if (!user) {
// 비로그인: DB 저장 없이 성공 응답
return res.json({ ok: true, anonymous: true });
}
db.prepare(`
INSERT INTO watch_history (user_id, track_id, played_at)
VALUES (?, ?, strftime('%s','now'))
`).run(user.id, trackId);
res.json({ ok: true });
});
// ───────────────────────────────────────────────────────────────
// 재생목록
// ───────────────────────────────────────────────────────────────
app.get('/api/playlists', (req, res) => {
const user = getUser(req);
if (!user) return res.status(401).json({ error: 'login required' });
const lists = db.prepare(`
SELECT * FROM playlists
WHERE user_id = ?
ORDER BY created_at DESC
`).all(user.id);
const itemsStmt = db.prepare(`
SELECT pi.track_id, t.title, t.artist, t.thumb, t.source
FROM playlist_items pi
JOIN tracks t ON t.id = pi.track_id
WHERE pi.playlist_id = ?
ORDER BY position ASC
`);
const data = lists.map(pl => ({
...pl,
items: itemsStmt.all(pl.id)
}));
res.json({ items: data });
});
app.post('/api/playlists', (req, res) => {
const user = getUser(req);
if (!user) return res.status(401).json({ error: 'login required' });
const { title, isPublic } = req.body || {};
if (!title) return res.status(400).json({ error: 'title required' });
const r = db.prepare(`
INSERT INTO playlists (user_id, title, is_public, created_at)
VALUES (?, ?, ?, strftime('%s','now'))
`).run(user.id, title, isPublic ? 1 : 0);
res.json({ ok: true, id: r.lastInsertRowid });
});
app.post('/api/playlist/items', (req, res) => {
const user = getUser(req);
if (!user) return res.status(401).json({ error: 'login required' });
const { playlistId, trackId, position } = req.body || {};
if (!playlistId || !trackId) {
return res.status(400).json({ error: 'playlistId & trackId required' });
}
db.prepare(`
INSERT OR IGNORE INTO playlist_items (playlist_id, track_id, position)
VALUES (?, ?, ?)
`).run(playlistId, trackId, position || 0);
res.json({ ok: true });
});
app.delete('/api/playlist/items', (req, res) => {
const user = getUser(req);
if (!user) return res.status(401).json({ error: 'login required' });
const { playlistId, trackId } = req.body || {};
if (!playlistId || !trackId) {
return res.status(400).json({ error: 'playlistId & trackId required' });
}
db.prepare(`
DELETE FROM playlist_items
WHERE playlist_id = ? AND track_id = ?
`).run(playlistId, trackId);
res.json({ ok: true });
});
app.get('/api/playlists/public', (req, res) => {
const lists = db.prepare(`
SELECT id, user_id, title, is_public, created_at
FROM playlists
WHERE is_public = 1
ORDER BY created_at DESC
`).all();
res.json({ items: lists });
});
// ───────────────────────────────────────────────────────────────
// 헬스체크 & 에러 핸들러
// ───────────────────────────────────────────────────────────────
app.get('/health', (_req, res) => res.json({ ok: true }));
// 공통 에러 핸들러 (CORS 에러 메시지 등)
app.use((err, _req, res, _next) => {
console.error(err?.stack || err?.message || err);
res.status(500).json({ error: String(err?.message || err || 'internal error') });
});
// ───────────────────────────────────────────────────────────────
// 서버 시작
// ───────────────────────────────────────────────────────────────
const PORT = 3001;
app.listen(PORT, () => {
console.log(`Emotion Playlist API running at http://localhost:${PORT}`);
});