From 0b67491b122d2b13b24402b11934c0a26ea604d3 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Wed, 21 Feb 2024 20:03:47 +0800 Subject: [PATCH] feat: robots.txt --- lib/api-router.js | 25 ------------------------- lib/app.ts | 2 ++ lib/config.ts | 2 +- lib/core-router.js | 17 ----------------- lib/v3/robots.txt.ts | 14 ++++++++++++++ 5 files changed, 17 insertions(+), 43 deletions(-) delete mode 100644 lib/api-router.js delete mode 100644 lib/core-router.js create mode 100644 lib/v3/robots.txt.ts diff --git a/lib/api-router.js b/lib/api-router.js deleted file mode 100644 index bed73e393..000000000 --- a/lib/api-router.js +++ /dev/null @@ -1,25 +0,0 @@ -const Router = require('@koa/router'); -const router = new Router(); - -router.get('/routes/:name?', (ctx) => { - const result = {}; - let counter = 0; - - const maintainer = require('./maintainer'); - for (const path of Object.keys(maintainer)) { - const top = path.split('/')[1]; - - if (!ctx.params.name || top === ctx.params.name) { - if (result[top]) { - result[top].routes.push(path); - } else { - result[top] = { routes: [path] }; - } - counter++; - } - } - - ctx.body = { counter, result }; -}); - -module.exports = router; diff --git a/lib/app.ts b/lib/app.ts index 6078fd814..a52f020e3 100644 --- a/lib/app.ts +++ b/lib/app.ts @@ -14,6 +14,7 @@ import logger from '@/utils/logger'; import routes from '@/routes'; import index from '@/v3/index'; +import robotstxt from '@/v3/robots.txt'; import { errorHandler } from '@/errors'; process.on('uncaughtException', (e) => { @@ -46,6 +47,7 @@ for (const name in routes) { } app.get('/', index); +app.get('/robots.txt', robotstxt); app.onError(errorHandler); diff --git a/lib/config.ts b/lib/config.ts index b92d9f4c0..6ebf1e795 100644 --- a/lib/config.ts +++ b/lib/config.ts @@ -332,7 +332,7 @@ const calculateValue = () => { const _value = { // app config - disallowRobot: envs.DISALLOW_ROBOT !== '0' && envs.DISALLOW_ROBOT !== 'false', + disallowRobot: toBoolean(envs.DISALLOW_ROBOT, false), enableCluster: envs.ENABLE_CLUSTER, isPackage: !!envs.IS_PACKAGE, nodeName: envs.NODE_NAME, diff --git a/lib/core-router.js b/lib/core-router.js deleted file mode 100644 index 823485a70..000000000 --- a/lib/core-router.js +++ /dev/null @@ -1,17 +0,0 @@ -const Router = require('@koa/router'); -const config = require('@/config').value; -const router = new Router(); - -// Load Core Route -router.get('/', require('./routes/index')); - -router.get('/robots.txt', (ctx) => { - if (config.disallowRobot) { - ctx.set('Content-Type', 'text/plain'); - ctx.body = 'User-agent: *\nDisallow: /'; - } else { - ctx.throw(404, 'Not Found'); - } -}); - -module.exports = router; diff --git a/lib/v3/robots.txt.ts b/lib/v3/robots.txt.ts new file mode 100644 index 000000000..0f0e8ab2a --- /dev/null +++ b/lib/v3/robots.txt.ts @@ -0,0 +1,14 @@ +import type { Handler } from 'hono'; +import { config } from '@/config'; + +const handler: Handler = (ctx) => { + if (config.disallowRobot) { + ctx.header('Content-Type', 'text/plain'); + return ctx.body('User-agent: *\nDisallow: /'); + } else { + ctx.status(404); + return ctx.body(''); + } +}; + +export default handler;