-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
224 lines (192 loc) · 5.39 KB
/
index.js
File metadata and controls
224 lines (192 loc) · 5.39 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
#!/usr/bin/env node
const express = require('express');
const fs = require('fs');
const path = require('path');
const { matchID } = require('./src/match');
const logger = require('./src/logger');
require('dotenv').config();
// 读取package.json获取版本号
const packageJson = require('./package.json');
// 创建Express应用
const app = express();
// 中间件
app.use(express.json());
// 静态文件服务
app.use(express.static('public'));
// 内部API路由
app.get('/inner/modules', (req, res) => {
try {
const modulesPath = path.join(__dirname, 'modules');
const files = fs.readdirSync(modulesPath);
const modules = files
.filter(file => file.endsWith('.js'))
.map(file => file.replace('.js', ''));
res.json({
code: 200,
data: {
modules: modules
}
});
} catch (error) {
res.json({
code: 500,
data: {
modules: []
},
message: error.message
});
}
});
app.get('/inner/version', (req, res) => {
res.json({
code: 200,
data: {
version: packageJson.version
}
});
});
app.get('/match', async (req, res) => {
const id = req.query.id;
const source = req.query.source;
if (!id) {
return res.json({
code: 400,
message: 'Missing id parameter',
data: null
});
}
try {
const result = await matchID(id, source);
res.json(result);
} catch (error) {
res.json({
code: 500,
message: error.message,
data: null
});
}
});
app.post('/match', async (req, res) => {
const id = req.body.id || req.query.id;
const source = req.body.source || req.query.source;
if (!id) {
return res.json({
code: 400,
message: 'Missing id parameter',
data: null
});
}
try {
const result = await matchID(id, source);
res.json(result);
} catch (error) {
res.json({
code: 500,
message: error.message,
data: null
});
}
});
// 根路径
app.get('/', (req, res) => {
res.sendFile(__dirname + '/public/index.html');
});
// 导出Express app 作为默认导出(Vercel需要)
module.exports = app;
// 导出matchID函数供直接调用
module.exports.matchID = matchID;
// 如果是直接运行此文件,则启动服务器
if (require.main === module) {
// 解析命令行参数
const args = process.argv.slice(2);
// 如果有 --help 或 -h 参数,显示帮助信息
if (args.includes('--help') || args.includes('-h')) {
console.log(`
unblockmusic-utils - 解锁网易云音乐内容的API服务
用法:
npx . [选项]
选项:
--port, -p <端口号> 指定服务器端口 (默认: 3000)
--help, -h 显示此帮助信息
示例:
npx . # 使用默认端口3000启动
npx . --port 8080 # 使用端口8080启动
`);
process.exit(0);
}
// 从命令行参数或环境变量获取端口
let PORT = process.env.PORT || 3000;
const portArgIndex = args.findIndex(arg => arg === '--port' || arg === '-p');
if (portArgIndex !== -1 && args[portArgIndex + 1]) {
const portFromArgs = parseInt(args[portArgIndex + 1]);
if (!isNaN(portFromArgs)) {
PORT = portFromArgs;
}
}
// 显示启动加载动画
const spinnerChars = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠷', '⠾', '⠧', '⠇', '⠏'];
let spinnerIndex = 0;
let spinnerInterval;
function startSpinner(text) {
process.stdout.write('\r' + spinnerChars[spinnerIndex] + ' ' + text);
spinnerIndex = (spinnerIndex + 1) % spinnerChars.length;
}
function stopSpinner(success = true, msg = '') {
if (spinnerInterval) {
clearInterval(spinnerInterval);
spinnerInterval = null;
}
if (success) {
console.log('\r✓ ' + msg);
}
}
// 启动加载动画
startSpinner('Loading modules...');
spinnerInterval = setInterval(() => startSpinner('Loading modules...'), 80);
// 预加载模块(让动画显示出来)
const modulesPath = path.join(__dirname, 'modules');
const moduleFiles = fs.readdirSync(modulesPath).filter(f => f.endsWith('.js'));
for (const file of moduleFiles) {
try {
require(path.join(modulesPath, file));
} catch (e) {
// 忽略错误
}
}
// 切换动画并启动服务器
startSpinner('Starting server...');
// 启动服务器
const server = app.listen(PORT, () => {
stopSpinner(true, 'Server is running on port ' + PORT);
console.log(' Visit http://localhost:' + PORT + ' to use the API');
console.log(' Press Ctrl+C to stop the server');
});
// 处理端口冲突等错误
server.on('error', (err) => {
if (err.code === 'EADDRINUSE') {
const newPort = PORT + 1;
if (newPort > 3010) { // 避免无限尝试
logger.error('All ports from ' + PORT + ' to ' + (newPort-1) + ' are busy');
process.exit(1);
}
console.log('\r⚠ Port ' + PORT + ' is busy, trying port ' + newPort + '...');
setTimeout(() => {
app.listen(newPort, () => {
stopSpinner(true, 'Server is running on port ' + newPort);
console.log(' Visit http://localhost:' + newPort + ' to use the API');
});
}, 1000);
} else {
stopSpinner(false, '');
logger.error('Server error: ' + err.message);
}
});
// 处理 Ctrl+C 退出
process.on('SIGINT', () => {
logger.info('Shutting down server...');
server.close(() => {
logger.info('Server closed');
process.exit(0);
});
});
}