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

3
import config from '@visualdl/core/next.config';
4 5
import express from 'express';
import next from 'next';
6
import nextI18NextMiddleware from '@visualdl/i18n/middleware';
7
import path from 'path';
8
import {setConfig} from 'next/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 || '/';
P
Peter Pan 已提交
18

19
const server = express();
20

21
async function start() {
22 23 24 25
    setConfig(config);
    const app = next({dev: isDev, conf: config});
    const handle = app.getRequestHandler();

26 27
    await app.prepare();

28 29 30 31 32 33 34 35
    if (isDev) {
        const {default: webpack} = await import('webpack');
        const {default: webpackDevMiddleware} = await import('webpack-dev-middleware');
        const {default: webpackConfig} = await import('./webpack.config');

        const compiler = webpack(webpackConfig);
        server.use(
            webpackDevMiddleware(compiler, {
36
                publicPath
37 38 39 40
            })
        );
    }

41
    if (backend) {
P
Peter Pan 已提交
42
        const {createProxyMiddleware} = await import('http-proxy-middleware');
43 44 45 46 47 48 49
        server.use(
            config.env.API_URL,
            createProxyMiddleware({
                target: backend,
                changeOrigin: true
            })
        );
P
Peter Pan 已提交
50 51 52
    } else if (isDemo) {
        const {default: demo} = await import('@visualdl/demo');
        server.use(config.env.API_URL, demo);
P
Peter Pan 已提交
53
    } else if (isDev) {
54 55
        const {default: mock} = await import('@visualdl/mock');
        server.use(config.env.API_URL, mock({delay: delay ? () => Math.random() * delay : 0}));
56 57
    } else {
        console.warn('Server is running in production mode but no backend address specified.');
58 59
    }

60 61 62 63 64 65
    if (publicPath !== '/') {
        server.get('/', (_req, res) => {
            res.redirect(publicPath);
        });
    }

P
Peter Pan 已提交
66 67 68 69 70 71 72 73 74 75 76 77
    if (
        process.env.PING_URL &&
        process.env.PING_URL !== '/' &&
        process.env.PING_URL !== publicPath &&
        process.env.PING_URL.startsWith('/')
    ) {
        server.get(process.env.PING_URL, (_req, res) => {
            res.type('text/plain');
            res.status(200).send('OK!');
        });
    }

78
    const {default: nextI18Next} = await import('@visualdl/core/utils/i18n');
79 80
    await nextI18Next.initPromise;
    server.use(nextI18NextMiddleware(nextI18Next));
81

82 83 84 85 86
    server.get(/\.wasm/, (_req, res, next) => {
        res.type('application/wasm');
        next();
    });

87 88
    server.get('*', (req, res) => handle(req, res));

89 90 91 92
    const s = server.listen(port, host, () => {
        process.send?.('ready');
        console.log(`> Ready on http://${host}:${port}`);
        process.on('SIGINT', () => {
93
            s.close((err: Error | undefined) => {
94 95 96 97 98 99 100
                if (err) {
                    throw err;
                }
                process.exit(0);
            });
        });
    });
101 102 103
}

if (require.main === module) {
104 105 106 107 108 109 110 111 112 113
    const core = require.resolve('@visualdl/core');
    // after webpack building, we dont need to chdir
    if ('string' === typeof core) {
        const cwd = process.cwd();
        const wd = path.dirname(core);
        process.chdir(wd);
        process.on('exit', () => process.chdir(cwd));
        process.on('uncaughtException', () => process.chdir(cwd));
        process.on('unhandledRejection', () => process.chdir(cwd));
    }
114 115 116 117
    start();
}

export default start;
118

119
export {server};