feat: expose server URLs in the CLI (#14646)

* feat: expose server URL in the CLI

* style: use arraw function

---------
This commit is contained in:
Raven-tu
2024-03-04 22:54:40 +08:00
committed by GitHub
parent ae7758191c
commit be1620074f
3 changed files with 23 additions and 2 deletions
+6
View File
@@ -1,12 +1,18 @@
import { serve } from '@hono/node-server';
import logger from '@/utils/logger';
import { getLocalhostAddress } from '@/utils/common-utils';
import { config } from '@/config';
import app from '@/app';
const port = config.connect.port;
const hostIPList = getLocalhostAddress();
logger.info(`🎉 RSSHub is running on port ${port}! Cheers!`);
logger.info('💖 Can you help keep this open source project alive? Please sponsor 👉 https://docs.rsshub.app/support');
logger.info(`🔗 Local: 👉 http://localhost:${port}`);
for (const ip of hostIPList) {
logger.info(`🔗 Network: 👉 http://${ip}:${port}`);
}
const server = serve({
fetch: app.fetch,
+5 -1
View File
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest';
import { toTitleCase, collapseWhitespace, convertDateToISO8601 } from '@/utils/common-utils';
import { toTitleCase, collapseWhitespace, convertDateToISO8601, getLocalhostAddress } from '@/utils/common-utils';
describe('common-utils', () => {
it('toTitleCase', () => {
@@ -34,4 +34,8 @@ describe('common-utils', () => {
expect(collapseWhitespace('a string already collapsed')).toBe('a string already collapsed');
expect(collapseWhitespace(' \n a lot of whitespaces and \n\n\n\n linebreaks \n\n ')).toBe('a lot of whitespaces and linebreaks');
});
it('getLocalhostAddress', () => {
expect(getLocalhostAddress()).toBeInstanceOf(Array);
});
});
+12 -1
View File
@@ -1,5 +1,6 @@
import { parseDate } from '@/utils/parse-date';
import title from 'title';
import os from 'os';
// convert a string into title case
const toTitleCase = (str: string) => title(str);
@@ -31,4 +32,14 @@ const getSubPath = (ctx) => {
return subPath;
};
export { toTitleCase, collapseWhitespace, convertDateToISO8601, getSubPath };
const getLocalhostAddress = () => {
const interfaces = os.networkInterfaces();
const address = Object.keys(interfaces)
.flatMap((name) => interfaces[name] ?? [])
.filter((iface) => iface?.family === 'IPv4' && !iface.internal)
.map((iface) => iface?.address)
.filter(Boolean);
return address;
};
export { toTitleCase, collapseWhitespace, convertDateToISO8601, getSubPath, getLocalhostAddress };