diff --git a/packages/cli/src/start.ts b/packages/cli/src/start.ts index c51557d..998cfb5 100644 --- a/packages/cli/src/start.ts +++ b/packages/cli/src/start.ts @@ -4,7 +4,7 @@ */ /* eslint-disable @typescript-eslint/await-thenable */ - +import os from 'os'; import Fastify, {FastifyReply, FastifyRequest, RouteOptions} from 'fastify'; import isDocker from 'is-docker'; import {existsSync} from 'fs'; @@ -17,6 +17,12 @@ import {showHelpForCommand} from './util'; import {warmup} from './start/warmup'; import closeWithGrace from 'close-with-grace'; +declare module 'fastify' { + interface FastifyInstance { + readonly $addedRoutes: RouteOptions[]; + } +} + const listenAddressDocker = '0.0.0.0'; // eslint-disable-next-line @typescript-eslint/init-declarations @@ -60,6 +66,23 @@ function initFinalLogger(logger: pino.Logger) { }); } +function getOuterIP() { + let interfaces = os.networkInterfaces(); + for (const devName of Object.keys(interfaces)) { + let iface = interfaces[devName]; + if (!iface) { + continue; + } + for (let i = 0, len = iface.length; i < len; i++) { + let alias = iface[i]; + if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) { + return alias.address; + } + } + } + return ''; +} + async function runFastify(opts: Args) { loadFastify(); @@ -169,11 +192,19 @@ async function runFastify(opts: Args) { address = await fastifyInstance.listen(opts.port); } + const showAddress = address === `0.0.0.0:${opts.port}` ? `${getOuterIP()}:${opts.port}` : address; + + for (const route of routes) { + console.log('——', `${showAddress}${route.url} (${route.method})`); + } + console.log(`Server listening on ${address}.`); for (const route of routes) { console.log('——', `${address}${route.url} (${route.method})`); } + fastifyInstance.decorate('$addedRoutes', routes); + // for pm2 graceful start if (process.send) { process.send('ready'); diff --git a/packages/logger/src/stream.ts b/packages/logger/src/stream.ts index 1dacb3c..c158cf0 100644 --- a/packages/logger/src/stream.ts +++ b/packages/logger/src/stream.ts @@ -56,7 +56,7 @@ export default function (streamsArray) { } /* istanbul ignore else */ - if (isDevelopment) { + if (isDevelopment || process.env.CONSOLE_LOG === 'true') { const arr = info.result.split(':'); arr[0] = (['FATAL', 'ERROR'].includes(arr[0])) ? chalk.red(arr[0]) : (arr[0] === 'WARN')