-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathserver.mjs
68 lines (61 loc) · 1.82 KB
/
server.mjs
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
import fs from 'fs';
import https from 'https';
import next from 'next';
import express from 'express';
import { createProxyMiddleware } from 'http-proxy-middleware';
import url from 'url';
async function bootstrap() {
const port = Number.parseInt(process.env.PORT, 10) || 3000;
const dev = process.env.NODE_ENV !== 'production';
const app = next({
dev,
customServer: true,
});
const handle = app.getRequestHandler();
await app.prepare();
const oidcServerUrl = process.env.OIDC_SERVER_URL;
if (!oidcServerUrl) {
console.warn('The env OIDC_SERVER_URL must be configured!')
process.exit();
}
const oidcUrlObj = url.parse(oidcServerUrl);
const _url = `${oidcUrlObj.protocol}//${oidcUrlObj.host}`;
const server = express();
// 代理中间件配置
const bff = '/bff';
const api = '/kubeagi-apis';
server.use(bff, createProxyMiddleware({
target: _url ,
changeOrigin: true,
secure: false, // 关闭 SSL 证书验证
}));
server.use(api, createProxyMiddleware({
target: _url,
changeOrigin: true,
secure: false,
}));
// 处理其他所有请求
server.all('*', (req, res) => {
return handle(req, res);
});
// PWA 测试时, 使用自签名证书, 开启HTTPS
if (process.env.PWA === 'true') {
// 在非开发环境下使用 HTTPS
https.createServer({
key: fs.readFileSync('./localhost-key.pem'),
cert: fs.readFileSync('./localhost.pem'),
}, server).listen(port, err => {
if (err) throw err;
// eslint-disable-next-line no-console
console.log(`> Ready on https://localhost:${port}`);
});
} else {
// 在开发环境下使用 HTTP
server.listen(port, err => {
if (err) throw err;
// eslint-disable-next-line no-console
console.log(`> Ready on http://localhost:${port}`);
});
}
}
bootstrap();