Skip to content

Commit

Permalink
feat: add getOuterIP,expose , env CONSOLE_LOG
Browse files Browse the repository at this point in the history
  • Loading branch information
kaivean committed Mar 11, 2023
1 parent 59f068f commit e4440c5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
33 changes: 32 additions & 1 deletion packages/cli/src/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion packages/logger/src/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit e4440c5

Please sign in to comment.