-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathapp.js
More file actions
120 lines (109 loc) · 4.72 KB
/
Copy pathapp.js
File metadata and controls
120 lines (109 loc) · 4.72 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
const express = require('express');
const path = require('path');
const fs = require('fs');
const os = require('os');
const app = express();
// 解析 JSON 和 URL-encoded 请求体
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
console.log("===============正在启动服务器...=============");
// ==================== 配置文件初始化 ====================
// 如果运行时配置不存在,则从 default 目录拷贝
if (!fs.existsSync('config/config.yaml')) {
fs.copyFileSync('config/default/config.yaml', 'config/config.yaml');
console.log('已从默认配置创建 config/config.yaml');
}
if (!fs.existsSync('config/webapi.js')) {
fs.copyFileSync('config/default/webapi.js', 'config/webapi.js');
console.log('已从默认配置创建 config/webapi.js');
}
// 将 webapi.js 拷贝到 public 目录供前端使用
fs.copyFileSync('config/webapi.js', 'src/public/webapi.js');
// 读取服务端配置
const yaml = require('yaml');
const configText = fs.readFileSync('config/config.yaml', 'utf8');
const config = yaml.parse(configText);
// 读取 webapi 配置
const webapiConfig = require('./config/webapi.js');
// 静态文件(挂载在 BASE_PATH 基础路径下)
const BASE_PATH = webapiConfig.BASE_PATH || '/order';
app.use(BASE_PATH, express.static(path.join(__dirname, 'src/public')));
// ==================== 按需启动集成服务 ====================
/**
* 判断服务地址是否为集成挂载路径
* 挂载路径:以 "/" 开头的路径(如 "/bili-api"),由主服务直接挂载
* 独立服务:完整的远程地址,主服务不启动该服务
* - 带协议:http://localhost:3300、https://api.example.com
* - 协议相对://localhost:3300
* - 无协议主机:localhost:3300、127.0.0.1:3300、example.com:3300
*/
function isMountPath(url) {
if (!url || typeof url !== 'string') return false;
// 以 "/" 开头且不是 "//" 开头(// 开头是协议相对URL,属于外部地址)
return url.startsWith('/') && !url.startsWith('//');
}
// B站开放平台 API
if (isMountPath(webapiConfig.bili_api)) {
const encrypt = require('./src/utils/encrypt');
const biliRouter = require('./src/routers/bili-router');
// 设置秘钥
encrypt.access_key_id = config.access_key_id || '';
encrypt.access_key_secred = config.access_key_secred || '';
// 设置服务
const biliPath = webapiConfig.bili_api || '/bili-api';
app.use(BASE_PATH + biliPath, biliRouter);
console.log(`B站开放平台API服务已挂载:http://localhost:${config.web_server_port}${BASE_PATH}${biliPath}`);
} else {
console.log(`B站API服务为独立服务:${webapiConfig.bili_api}`);
}
// 网易云音乐 API
if (isMountPath(webapiConfig.netease_api)) {
const NeteaseCloudMusicApi = require('NeteaseCloudMusicApi');
const neteasePath = webapiConfig.netease_api || '/netease_api';
app.use(BASE_PATH + neteasePath, async (req, res) => {
try {
let apiPath = req.path.replace(/^\//, '').replace(/\//g, '_');
const apiFunc = NeteaseCloudMusicApi[apiPath];
if (!apiFunc) {
return res.status(404).json({ error: `未知的网易云API: ${apiPath}` });
}
const query = { ...req.query, ...req.body };
const result = await apiFunc(query);
res.status(result.status).json(result.body);
} catch (error) {
console.error('网易云API调用失败:', error);
res.status(500).json({ error: error.message });
}
});
console.log(`网易云音乐API服务已挂载:http://localhost:${config.web_server_port}${BASE_PATH}${neteasePath}`);
} else {
console.log(`网易云音乐API服务为独立服务:${webapiConfig.netease_api}`);
}
// 获取本机所有局域网地址
function getLocalIPs() {
const interfaces = os.networkInterfaces();
const ips = [];
for (const name of Object.keys(interfaces)) {
for (const iface of interfaces[name]) {
if (iface.family === 'IPv4' && !iface.internal) {
ips.push(iface.address);
}
}
}
return ips;
}
// 监听端口
const host = config.web_server_host || '0.0.0.0';
const server = app.listen(config.web_server_port, host, () => {
console.log("=================服务已启动==================");
console.log(`本地地址:http://localhost:${config.web_server_port}${BASE_PATH}`);
if (host === '0.0.0.0') {
const ips = getLocalIPs();
for (const ip of ips) {
console.log(`网络地址:http://${ip}:${config.web_server_port}${BASE_PATH}`);
}
} else {
console.log(`服务已启动:http://${host}:${config.web_server_port}${BASE_PATH}`);
}
console.log("");
});