index.ts 2.6 KB
Newer Older
1 2
/* eslint-disable no-console */

3
import {config} from 'dotenv';
4
import express from 'express';
5
import path from 'path';
6 7 8
import resolve from 'enhanced-resolve';

config();
9

10
const isDev = process.env.NODE_ENV === 'development';
P
Peter Pan 已提交
11
const isDemo = !!process.env.DEMO;
12

13
const host = process.env.HOST || 'localhost';
14
const port = Number.parseInt(process.env.PORT || '', 10) || 8999;
15
const backend = process.env.BACKEND;
16
const delay = Number.parseInt(process.env.DELAY || '', 10);
17
const publicPath = process.env.PUBLIC_PATH || '/';
18 19
const apiUrl = process.env.API_URL || `${process.env.PUBLIC_PATH || ''}/api`;
const pingUrl = process.env.PING_URL;
P
Peter Pan 已提交
20

21
const root = path.dirname(resolve.sync(__dirname, '@visualdl/core'));
22

23
const app = express();
24

25
async function start() {
26
    if (backend) {
P
Peter Pan 已提交
27
        const {createProxyMiddleware} = await import('http-proxy-middleware');
28 29
        app.use(
            apiUrl,
30 31 32 33 34
            createProxyMiddleware({
                target: backend,
                changeOrigin: true
            })
        );
P
Peter Pan 已提交
35
    } else if (isDemo) {
P
Peter Pan 已提交
36 37 38 39 40 41
        try {
            const {default: demo} = await import('@visualdl/demo');
            app.use(apiUrl, demo);
        } catch {
            console.warn('Demo is not installed. Please rebuild server.');
        }
P
Peter Pan 已提交
42
    } else if (isDev) {
43 44
        const {middleware: mock} = await import('@visualdl/mock');
        app.use(apiUrl, mock({delay: delay ? () => Math.random() * delay : 0}));
45 46
    } else {
        console.warn('Server is running in production mode but no backend address specified.');
47 48
    }

49
    if (publicPath !== '/') {
50
        app.get('/', (_req, res) => {
51 52 53 54
            res.redirect(publicPath);
        });
    }

55 56
    if (pingUrl && pingUrl !== '/' && pingUrl !== publicPath && pingUrl.startsWith('/')) {
        app.get(pingUrl, (_req, res) => {
P
Peter Pan 已提交
57 58 59 60 61
            res.type('text/plain');
            res.status(200).send('OK!');
        });
    }

62
    app.use(publicPath, express.static(root, {index: false}));
63

64
    app.get(/\.wasm/, (_req, res, next) => {
65 66 67 68
        res.type('application/wasm');
        next();
    });

69 70 71
    app.get('*', (_req, res) => {
        res.sendFile('index.html', {root});
    });
72

73
    const server = app.listen(port, host, () => {
74 75 76
        process.send?.('ready');
        console.log(`> Ready on http://${host}:${port}`);
        process.on('SIGINT', () => {
77
            server.close(err => {
78 79 80 81 82 83 84
                if (err) {
                    throw err;
                }
                process.exit(0);
            });
        });
    });
85 86 87 88 89 90 91
}

if (require.main === module) {
    start();
}

export default start;
92

93
export {app};