diff --git a/lib/config.ts b/lib/config.ts index fc5ac8b25..2bbd417bf 100644 --- a/lib/config.ts +++ b/lib/config.ts @@ -13,7 +13,6 @@ type Config = { chromiumExecutablePath?: string; connect: { port: number; - socket: string | null; }; listenInaddrAny: boolean; requestRetry: number; @@ -347,7 +346,6 @@ const calculateValue = () => { // network connect: { port: toInt(envs.PORT, 1200), // 监听端口 - socket: envs.SOCKET || null, // 监听 Unix Socket, null 为禁用 }, listenInaddrAny: toBoolean(envs.LISTEN_INADDR_ANY, true), // 是否允许公网连接,取值 0 1 requestRetry: toInt(envs.REQUEST_RETRY, 2), // 请求失败重试次数 diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index 730a73c09..000000000 --- a/lib/index.js +++ /dev/null @@ -1,35 +0,0 @@ -const app = require('./app'); -const config = require('./config').value; -const fs = require('fs'); -const logger = require('./utils/logger'); - -const cluster = require('cluster'); -const numCPUs = require('os').cpus().length; - -if (config.enableCluster && cluster.isMaster && process.env.NODE_ENV !== 'test' && process.env.NODE_ENV !== 'dev') { - for (let i = 0; i < numCPUs; i++) { - cluster.fork(); - } -} else { - let server; - if (config.connect.socket) { - if (fs.existsSync(config.connect.socket)) { - fs.unlinkSync(config.connect.socket); - } - server = app.listen(config.connect.socket, Number.parseInt(config.listenInaddrAny) ? null : '127.0.0.1'); - logger.info('Listening Unix Socket ' + config.connect.socket); - process.on('SIGINT', () => { - fs.unlinkSync(config.connect.socket); - process.exit(); - }); - } - if (config.connect.port) { - server = app.listen(config.connect.port, Number.parseInt(config.listenInaddrAny) ? null : '127.0.0.1'); - logger.info('Listening Port ' + config.connect.port); - } - - logger.info('🎉 RSSHub start! Cheers!'); - logger.info('💖 Can you help keep this open source project alive? Please sponsor 👉 https://docs.rsshub.app/support'); - - module.exports = server; -} diff --git a/lib/index.ts b/lib/index.ts index ff31db74b..1abea49b7 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -9,10 +9,16 @@ import debug from '@/middleware/debug' import header from '@/middleware/header' import antiHotlink from '@/middleware/anti-hotlink' import parameter from '@/middleware/parameter' +import logger from '@/utils/logger' import routes from '@/routes' +import index from '@/v3/index' import { config } from '@/config' +process.on('uncaughtException', (e) => { + logger.error('uncaughtException: ' + e); +}); + const app = new Hono() app.use('*', onerror); @@ -38,12 +44,14 @@ for (const name in routes) { }) } -app.get('/', (c) => { - return c.text('Hello Hono!') -}) +app.get('/', index) + +console.log(app) const port = config.connect.port -console.log(`Server is running on port ${port}`) + +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'); serve({ fetch: app.fetch, diff --git a/lib/middleware/debug.ts b/lib/middleware/debug.ts index e02e6ea73..71e914987 100644 --- a/lib/middleware/debug.ts +++ b/lib/middleware/debug.ts @@ -1,17 +1,17 @@ import { MiddlewareHandler } from "hono"; import { getRouteNameFromPath } from '@/utils/helpers'; -const middleware: MiddlewareHandler = async (ctx, next) => { - const debug = Object.assign({ - hitCache: 0, - request: 0, - etag: 0, - paths: [], - routes: [], - errorPaths: [], - errorRoutes: [], - }, ctx.get('debug')); +const debug = { + hitCache: 0, + request: 0, + etag: 0, + paths: [], + routes: [], + errorPaths: [], + errorRoutes: [], +} +const middleware: MiddlewareHandler = async (ctx, next) => { if (!debug.paths[ctx.req.path]) { debug.paths[ctx.req.path] = 0; } @@ -33,11 +33,11 @@ const middleware: MiddlewareHandler = async (ctx, next) => { debug.hitCache++; } - ctx.set('debuged', true); - if (ctx.res.status === 304) { debug.etag++; } }; -export default middleware; \ No newline at end of file +export default middleware; + +export const getDebugInfo = () => debug diff --git a/lib/middleware/parameter.ts b/lib/middleware/parameter.ts index 8a090e15f..0749f1b84 100644 --- a/lib/middleware/parameter.ts +++ b/lib/middleware/parameter.ts @@ -56,7 +56,7 @@ const middleware: MiddlewareHandler = async (ctx, next) => { const data = ctx.get('data') as Data; if (!data) { - throw new Error('wrong path'); + // throw new Error('wrong path'); } else { if ((!data.item || data.item.length === 0) && !data.allowEmpty) { throw new Error('this route is empty, please check the original site or create an issue'); @@ -194,7 +194,7 @@ const middleware: MiddlewareHandler = async (ctx, next) => { regex instanceof RE2JS ? regex.matcher(title).find() || regex.matcher(description).find() || regex.matcher(author).find() || category.some((c) => regex.matcher(c).find()) : title.match(regex) || description.match(regex) || author.match(regex) || category.some((c) => c.match(regex)); - + return isFilter; }); } @@ -377,4 +377,4 @@ const middleware: MiddlewareHandler = async (ctx, next) => { } }; -export default middleware; \ No newline at end of file +export default middleware; diff --git a/lib/static/logo.png b/lib/static/logo.png index dc5e7dbf9..df91fde1f 100644 Binary files a/lib/static/logo.png and b/lib/static/logo.png differ diff --git a/lib/v3/index.ts b/lib/v3/index.ts new file mode 100644 index 000000000..e606d7dd0 --- /dev/null +++ b/lib/v3/index.ts @@ -0,0 +1,106 @@ +import type { Handler } from 'hono'; +import { config } from '@/config'; +import art from 'art-template'; +import * as path from 'node:path'; +import gitRevSync from 'git-rev-sync'; +import { getDebugInfo } from '@/middleware/debug'; + +let gitHash = process.env.HEROKU_SLUG_COMMIT?.slice(0, 7) || process.env.VERCEL_GIT_COMMIT_SHA?.slice(0, 7) +if (!gitHash) { + try { + gitHash = gitRevSync.short(); + } catch { + gitHash = 'unknown'; + } +} + +const startTime = Date.now(); + +const handler: Handler = (ctx) => { + ctx.header('Content-Type', 'text/html; charset=UTF-8') + ctx.header('Cache-Control', 'no-cache') + + const debug = getDebugInfo(); + const routes = Object.keys(debug.routes).sort((a, b) => debug.routes[b] - debug.routes[a]); + const hotRoutes = routes.slice(0, 30); + const hotRoutesValue = hotRoutes.map((item) => `${debug.routes[item]} ${item}
`).join(''); + + const paths = Object.keys(debug.paths).sort((a, b) => debug.paths[b] - debug.paths[a]); + const hotPaths = paths.slice(0, 30); + + const hotPathsValue = hotPaths.map((item) => `${debug.paths[item]} ${item}
`).join(''); + + let hotErrorRoutesValue = ''; + if (debug.errorRoutes) { + const errorRoutes = Object.keys(debug.errorRoutes).sort((a, b) => debug.errorRoutes[b] - debug.errorRoutes[a]); + const hotErrorRoutes = errorRoutes.slice(0, 30); + hotErrorRoutesValue = hotErrorRoutes.map((item) => `${debug.errorRoutes[item]} ${item}
`).join(''); + } + + let hotErrorPathsValue = ''; + if (debug.errorPaths) { + const errorPaths = Object.keys(debug.errorPaths).sort((a, b) => debug.errorPaths[b] - debug.errorPaths[a]); + const hotErrorPaths = errorPaths.slice(0, 30); + hotErrorPathsValue = hotErrorPaths.map((item) => `${debug.errorPaths[item]} ${item}
`).join(''); + } + + const showDebug = !config.debugInfo || config.debugInfo === 'false' ? false : config.debugInfo === 'true' || config.debugInfo === ctx.req.query('debug'); + const { disallowRobot, nodeName } = config; + + const duration = Date.now() - startTime; + + return ctx.body(art(path.resolve(__dirname, '../views/welcome.art'), { + showDebug, + disallowRobot, + debug: [ + nodeName + ? { + name: 'Node Name', + value: nodeName, + } + : null, + { + name: 'Git Hash', + value: gitHash, + }, + { + name: 'Request Amount', + value: debug.request, + }, + { + name: 'Request Frequency', + value: ((debug.request / (duration / 1000)) * 60).toFixed(3) + ' times/minute', + }, + { + name: 'Cache Hit Ratio', + value: debug.request ? (debug.hitCache / debug.request).toFixed(3) : 0, + }, + { + name: 'ETag Matched', + value: debug.etag, + }, + { + name: 'Run Time', + value: (duration / 3_600_000).toFixed(2) + ' hour(s)', + }, + { + name: 'Hot Routes', + value: hotRoutesValue, + }, + { + name: 'Hot Paths', + value: hotPathsValue, + }, + { + name: 'Hot Error Routes', + value: hotErrorRoutesValue, + }, + { + name: 'Hot Error Paths', + value: hotErrorPathsValue, + }, + ], + })); +}; + +export default handler; diff --git a/lib/views/welcome.art b/lib/views/welcome.art index 544cf2335..448fe9f96 100644 --- a/lib/views/welcome.art +++ b/lib/views/welcome.art @@ -79,7 +79,7 @@

- RSSHub + RSSHub

Welcome to