From 829b5f0e5244d853b11dd32a85cb29682d6bdc8d Mon Sep 17 00:00:00 2001 From: DIYgod Date: Thu, 22 Feb 2024 01:38:16 +0800 Subject: [PATCH] feat: assets routes --- {lib => assets}/favicon.png | Bin {lib/static => assets}/logo.png | Bin lib/app.ts | 21 ++------------ lib/{router.test.ts => routes.test.ts} | 21 +++++++------- lib/routes.ts | 37 +++++++++++++++++++++++-- lib/views/error.art | 2 +- lib/views/welcome.art | 2 +- 7 files changed, 48 insertions(+), 35 deletions(-) rename {lib => assets}/favicon.png (100%) rename {lib/static => assets}/logo.png (100%) rename lib/{router.test.ts => routes.test.ts} (82%) diff --git a/lib/favicon.png b/assets/favicon.png similarity index 100% rename from lib/favicon.png rename to assets/favicon.png diff --git a/lib/static/logo.png b/assets/logo.png similarity index 100% rename from lib/static/logo.png rename to assets/logo.png diff --git a/lib/app.ts b/lib/app.ts index a52f020e3..7a887eec6 100644 --- a/lib/app.ts +++ b/lib/app.ts @@ -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); diff --git a/lib/router.test.ts b/lib/routes.test.ts similarity index 82% rename from lib/router.test.ts rename to lib/routes.test.ts index 944aab0b2..8fd2a4955 100644 --- a/lib/router.test.ts +++ b/lib/routes.test.ts @@ -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'); + }); }); diff --git a/lib/routes.ts b/lib/routes.ts index 5cb2616ee..7a2e7630a 100644 --- a/lib/routes.ts +++ b/lib/routes.ts @@ -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 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', + }) + ); +} diff --git a/lib/views/error.art b/lib/views/error.art index 8c83de401..e128d63fc 100644 --- a/lib/views/error.art +++ b/lib/views/error.art @@ -36,7 +36,7 @@

- RSSHub + RSSHub

Looks like something went wrong

diff --git a/lib/views/welcome.art b/lib/views/welcome.art index 448fe9f96..7d8c0b802 100644 --- a/lib/views/welcome.art +++ b/lib/views/welcome.art @@ -79,7 +79,7 @@

- RSSHub + RSSHub

Welcome to