feat: robots.txt

This commit is contained in:
DIYgod 2024-02-21 20:03:47 +08:00
parent 0d606a06c2
commit 0b67491b12
No known key found for this signature in database
5 changed files with 17 additions and 43 deletions

View File

@ -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;

View File

@ -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);

View File

@ -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,

View File

@ -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;

14
lib/v3/robots.txt.ts Normal file
View File

@ -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;