mirror of
https://github.com/DIYgod/RSSHub.git
synced 2026-08-31 01:01:11 +08:00
feat: assets routes
This commit is contained in:
|
Before Width: | Height: | Size: 2.6 KiB After Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 12 KiB After Width: | Height: | Size: 12 KiB |
+2
-19
@@ -1,6 +1,6 @@
|
||||
import '@/utils/request-wrapper';
|
||||
|
||||
import { Handler, Hono } from 'hono';
|
||||
import { Hono } from 'hono';
|
||||
|
||||
import cache from '@/middleware/cache';
|
||||
import template from '@/middleware/template';
|
||||
@@ -13,8 +13,6 @@ import parameter from '@/middleware/parameter';
|
||||
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) => {
|
||||
@@ -32,22 +30,7 @@ app.use('*', antiHotlink);
|
||||
app.use('*', parameter);
|
||||
app.use('*', cache);
|
||||
|
||||
for (const name in routes) {
|
||||
const subApp = app.basePath(`/${name}`);
|
||||
routes[name]({
|
||||
get: (path, handler) => {
|
||||
const wrapedHandler: Handler = async (ctx, ...args) => {
|
||||
if (!ctx.get('data')) {
|
||||
await handler(ctx, ...args);
|
||||
}
|
||||
};
|
||||
subApp.get(path, wrapedHandler);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
app.get('/', index);
|
||||
app.get('/robots.txt', robotstxt);
|
||||
routes(app);
|
||||
|
||||
app.onError(errorHandler);
|
||||
|
||||
|
||||
@@ -68,16 +68,15 @@ describe('router', () => {
|
||||
});
|
||||
|
||||
// robots.txt
|
||||
// TODO
|
||||
// it('/robots.txt', async () => {
|
||||
// config.disallowRobot = false;
|
||||
// const response404 = await app.request('/robots.txt');
|
||||
// expect(response404.status).toBe(404);
|
||||
it('/robots.txt', async () => {
|
||||
config.disallowRobot = false;
|
||||
const response404 = await app.request('/robots.txt');
|
||||
expect(response404.status).toBe(404);
|
||||
|
||||
// config.disallowRobot = true;
|
||||
// const response = await app.request('/robots.txt');
|
||||
// expect(response.status).toBe(200);
|
||||
// expect(await response.text()).toBe('User-agent: *\nDisallow: /');
|
||||
// expect(response.headers.get('content-type')).toBe('text/plain');
|
||||
// });
|
||||
config.disallowRobot = true;
|
||||
const response = await app.request('/robots.txt');
|
||||
expect(response.status).toBe(200);
|
||||
expect(await response.text()).toBe('User-agent: *\nDisallow: /');
|
||||
expect(response.headers.get('content-type')).toBe('text/plain');
|
||||
});
|
||||
});
|
||||
+34
-3
@@ -1,5 +1,9 @@
|
||||
import { directoryImport } from 'directory-import';
|
||||
import type { Handler } from 'hono';
|
||||
import type { Hono, Handler } from 'hono';
|
||||
import { serveStatic } from '@hono/node-server/serve-static';
|
||||
|
||||
import index from '@/v3/index';
|
||||
import robotstxt from '@/v3/robots.txt';
|
||||
|
||||
type Root = {
|
||||
get: (path: string, handler: Handler) => void;
|
||||
@@ -14,7 +18,34 @@ const routes: Record<string, (root: Root) => void> = {};
|
||||
|
||||
for (const path in imports) {
|
||||
const name = path.split('/').find(Boolean);
|
||||
routes[name] = imports[path] as (root: Root) => void;
|
||||
if (name) {
|
||||
routes[name] = imports[path] as (root: Root) => void;
|
||||
}
|
||||
}
|
||||
|
||||
export default routes;
|
||||
export default function (app: Hono) {
|
||||
for (const name in routes) {
|
||||
const subApp = app.basePath(`/${name}`);
|
||||
routes[name]({
|
||||
get: (path, handler) => {
|
||||
const wrapedHandler: Handler = async (ctx, ...args) => {
|
||||
if (!ctx.get('data')) {
|
||||
await handler(ctx, ...args);
|
||||
}
|
||||
};
|
||||
subApp.get(path, wrapedHandler);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// routes without rss data
|
||||
app.get('/', index);
|
||||
app.get('/robots.txt', robotstxt);
|
||||
|
||||
app.use(
|
||||
'/*',
|
||||
serveStatic({
|
||||
root: './assets',
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
+1
-1
@@ -36,7 +36,7 @@
|
||||
<body>
|
||||
<div class="content">
|
||||
<p>
|
||||
<img src="https://i.imgur.com/KmgWkHm.png" alt="RSSHub" width="120" loading="lazy" />
|
||||
<img src="/logo.png" alt="RSSHub" width="120" loading="lazy" />
|
||||
</p>
|
||||
|
||||
<h1>Looks like something went wrong</h1>
|
||||
|
||||
@@ -79,7 +79,7 @@
|
||||
<body>
|
||||
<div class="content">
|
||||
<p>
|
||||
<img src="https://i.imgur.com/KmgWkHm.png" alt="RSSHub" width="100" loading="lazy" />
|
||||
<img src="/logo.png" alt="RSSHub" width="100" loading="lazy" />
|
||||
</p>
|
||||
|
||||
<h1>Welcome to
|
||||
|
||||
Reference in New Issue
Block a user