Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

refactor(static): mutualized static assets routes

Corentin Thomassetb99d72d23ba2
diff --git a/apps/papra-server/src/modules/app/server.ts b/apps/papra-server/src/modules/app/server.ts
index 9695bc81..2fed2723 100644
--- a/apps/papra-server/src/modules/app/server.ts
+++ b/apps/papra-server/src/modules/app/server.ts
@@ -6,12 +6,12 @@ import { Hono } from 'hono';
import { secureHeaders } from 'hono/secure-headers';
import { parseConfig } from '../config/config';
import { getAuth } from './auth/auth.services';
-import { registerAssetsMiddleware } from './middlewares/assets.middleware';
import { corsMiddleware } from './middlewares/cors.middleware';
import { registerErrorMiddleware } from './middlewares/errors.middleware';
import { loggerMiddleware } from './middlewares/logger.middleware';
import { timeoutMiddleware } from './middlewares/timeout.middleware';
import { registerRoutes } from './server.routes';
+import { registerStaticAssetsRoutes } from './static-assets/static-assets.routes';
export function createServer({
db,
@@ -38,8 +38,8 @@ export function createServer({
app.use(timeoutMiddleware);
app.use(secureHeaders());
- registerAssetsMiddleware({ app });
registerErrorMiddleware({ app });
+ registerStaticAssetsRoutes({ app });
registerRoutes({ app });
diff --git a/apps/papra-server/src/modules/app/static-assets/static-assets.models.test.ts b/apps/papra-server/src/modules/app/static-assets/static-assets.models.test.ts
new file mode 100644
index 00000000..01b6cf05
--- /dev/null
+++ b/apps/papra-server/src/modules/app/static-assets/static-assets.models.test.ts
@@ -0,0 +1,17 @@
+import { describe, expect, test } from 'vitest';
+import { isApiRoute } from './static-assets.models';
+
+describe('static-assets models', () => {
+ describe('isApiRoute', () => {
+ test('an api route starts with /api/*', () => {
+ expect(isApiRoute({ path: '/api/test' })).to.equal(true);
+ expect(isApiRoute({ path: '/api/' })).to.equal(true);
+ expect(isApiRoute({ path: '/api' })).to.equal(true);
+
+ expect(isApiRoute({ path: '/apitest' })).to.equal(false);
+ expect(isApiRoute({ path: '/' })).to.equal(false);
+ expect(isApiRoute({ path: '' })).to.equal(false);
+ expect(isApiRoute({ path: '/index.html' })).to.equal(false);
+ });
+ });
+});
diff --git a/apps/papra-server/src/modules/app/static-assets/static-assets.models.ts b/apps/papra-server/src/modules/app/static-assets/static-assets.models.ts
new file mode 100644
index 00000000..7bed2852
--- /dev/null
+++ b/apps/papra-server/src/modules/app/static-assets/static-assets.models.ts
@@ -0,0 +1,3 @@
+export function isApiRoute({ path }: { path: string }) {
+ return path.startsWith('/api/') || path === '/api';
+};
diff --git a/apps/papra-server/src/modules/app/middlewares/assets.middleware.ts b/apps/papra-server/src/modules/app/static-assets/static-assets.routes.ts
similarity index 72%
rename from apps/papra-server/src/modules/app/middlewares/assets.middleware.ts
rename to apps/papra-server/src/modules/app/static-assets/static-assets.routes.ts
index a34090c2..9eb45755 100644
--- a/apps/papra-server/src/modules/app/middlewares/assets.middleware.ts
+++ b/apps/papra-server/src/modules/app/static-assets/static-assets.routes.ts
@@ -3,6 +3,7 @@ import { readFile } from 'node:fs/promises';
import { serveStatic } from '@hono/node-server/serve-static';
import { memoize } from 'lodash-es';
import { getConfig } from '../../config/config.models';
+import { isApiRoute } from './static-assets.models';
const getIndexContent = memoize(async () => {
const index = await readFile('public/index.html', 'utf-8');
@@ -10,7 +11,7 @@ const getIndexContent = memoize(async () => {
return index;
});
-export function registerAssetsMiddleware({ app }: { app: ServerInstance }) {
+export function registerStaticAssetsRoutes({ app }: { app: ServerInstance }) {
app
.use(
'*',
@@ -21,10 +22,13 @@ export function registerAssetsMiddleware({ app }: { app: ServerInstance }) {
return next();
}
- return serveStatic({
+ const staticMiddleware = serveStatic({
root: './public',
- index: `unexisting-file-${Math.random().toString(36).substring(2, 15)}`, // Disable index.html fallback to let the next middleware handle it
- })(context, next);
+ // Disable index.html fallback to let the next middleware handle it
+ index: `unexisting-file-${Math.random().toString(36).substring(2)}`,
+ });
+
+ return staticMiddleware(context, next);
},
)
.use(
@@ -36,7 +40,7 @@ export function registerAssetsMiddleware({ app }: { app: ServerInstance }) {
return next();
}
- if (context.req.path.startsWith('/api/')) {
+ if (isApiRoute({ path: context.req.path })) {
return next();
}