index.ts 3.1 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';
11

12
const host = process.env.HOST || 'localhost';
13
const port = Number.parseInt(process.env.PORT || '', 10) || 8999;
14
const backend = process.env.BACKEND;
15
const delay = Number.parseInt(process.env.DELAY || '', 10);
16
const publicPath = process.env.PUBLIC_PATH || '/';
P
Peter Pan 已提交
17

18
const server = express();
19

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

25 26
    await app.prepare();

27 28 29 30 31 32 33 34
    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, {
35
                publicPath
36 37 38 39
            })
        );
    }

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

56 57 58 59 60 61
    if (publicPath !== '/') {
        server.get('/', (_req, res) => {
            res.redirect(publicPath);
        });
    }

62
    const {default: nextI18Next} = await import('@visualdl/core/utils/i18n');
63 64
    await nextI18Next.initPromise;
    server.use(nextI18NextMiddleware(nextI18Next));
65

66 67 68 69 70
    server.get(/\.wasm/, (_req, res, next) => {
        res.type('application/wasm');
        next();
    });

71 72
    server.get('*', (req, res) => handle(req, res));

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

if (require.main === module) {
88 89 90 91 92 93 94 95 96 97
    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));
    }
98 99 100 101
    start();
}

export default start;
102

103
export {server};