From 5d195f72cd7c21e488c80d78056d4a0d13e3b3e3 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Tue, 18 Apr 2023 11:24:54 +0530 Subject: [PATCH] feat: add middleware to serve static file and redirect root to dashboard Signed-off-by: Pranav C --- packages/nocodb-nest/src/app.module.ts | 9 +++------ .../public/public.middleware.spec.ts | 7 +++++++ .../middlewares/public/public.middleware.ts | 18 ++++++++++++++++++ 3 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 packages/nocodb-nest/src/middlewares/public/public.middleware.spec.ts create mode 100644 packages/nocodb-nest/src/middlewares/public/public.middleware.ts diff --git a/packages/nocodb-nest/src/app.module.ts b/packages/nocodb-nest/src/app.module.ts index 9d79ca6d0a..fed3813689 100644 --- a/packages/nocodb-nest/src/app.module.ts +++ b/packages/nocodb-nest/src/app.module.ts @@ -1,12 +1,11 @@ -import { join } from 'path'; import { Module, RequestMethod } from '@nestjs/common'; import { APP_FILTER } from '@nestjs/core'; -import { ServeStaticModule } from '@nestjs/serve-static'; import { Connection } from './connection/connection'; import { GlobalExceptionFilter } from './filters/global-exception/global-exception.filter'; import NcPluginMgrv2 from './helpers/NcPluginMgrv2'; import { GlobalMiddleware } from './middlewares/global/global.middleware'; import { GuiMiddleware } from './middlewares/gui/gui.middleware'; +import { PublicMiddleware } from './middlewares/public/public.middleware' import { DatasModule } from './modules/datas/datas.module'; import { AuthService } from './services/auth.service'; import { UsersModule } from './modules/users/users.module'; @@ -17,7 +16,6 @@ import { GlobalModule } from './modules/global/global.module'; import { LocalStrategy } from './strategies/local.strategy'; import { AuthTokenStrategy } from './strategies/authtoken.strategy/authtoken.strategy'; import { BaseViewStrategy } from './strategies/base-view.strategy/base-view.strategy'; -import { GoogleStrategy } from './strategies/google.strategy/google.strategy'; import NcUpgrader from './version-upgrader/NcUpgrader'; import { MetasModule } from './modules/metas/metas.module'; import type { @@ -28,9 +26,6 @@ import NocoCache from './cache/NocoCache'; @Module({ imports: [ - ServeStaticModule.forRoot({ - rootPath: join(process.cwd(), 'public'), - }), GlobalModule, UsersModule, ...(process.env['PLAYWRIGHT_TEST'] === 'true' ? [TestModule] : []), @@ -60,6 +55,8 @@ export class AppModule implements OnApplicationBootstrap { consumer .apply(GuiMiddleware) .forRoutes({ path: '*', method: RequestMethod.GET }) + .apply(PublicMiddleware) + .forRoutes({ path: '*', method: RequestMethod.GET }) .apply(GlobalMiddleware) .forRoutes({ path: '*', method: RequestMethod.ALL }); } diff --git a/packages/nocodb-nest/src/middlewares/public/public.middleware.spec.ts b/packages/nocodb-nest/src/middlewares/public/public.middleware.spec.ts new file mode 100644 index 0000000000..7b5e460f52 --- /dev/null +++ b/packages/nocodb-nest/src/middlewares/public/public.middleware.spec.ts @@ -0,0 +1,7 @@ +import { PublicMiddleware } from './public.middleware'; + +describe('PublicMiddleware', () => { + it('should be defined', () => { + expect(new PublicMiddleware()).toBeDefined(); + }); +}); diff --git a/packages/nocodb-nest/src/middlewares/public/public.middleware.ts b/packages/nocodb-nest/src/middlewares/public/public.middleware.ts new file mode 100644 index 0000000000..c14bcd2792 --- /dev/null +++ b/packages/nocodb-nest/src/middlewares/public/public.middleware.ts @@ -0,0 +1,18 @@ +import { join } from 'path'; +import { Injectable } from '@nestjs/common'; +import express from 'express'; +import type { NestMiddleware } from '@nestjs/common'; + +@Injectable() +export class PublicMiddleware implements NestMiddleware { + use(req: any, res: any, next: () => void) { + // redirect root to dashboard + if (req.path === '/') { + const dashboardPath = process.env.NC_DASHBOARD_URL || '/dashboard'; + return res.redirect(dashboardPath); + } + + // serve static files from public folder + express.static(join(process.cwd(), 'public'))(req, res, next); + } +}