Browse Source

feat: add middleware to serve static file and redirect root to dashboard

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/5444/head
Pranav C 2 years ago
parent
commit
5d195f72cd
  1. 9
      packages/nocodb-nest/src/app.module.ts
  2. 7
      packages/nocodb-nest/src/middlewares/public/public.middleware.spec.ts
  3. 18
      packages/nocodb-nest/src/middlewares/public/public.middleware.ts

9
packages/nocodb-nest/src/app.module.ts

@ -1,12 +1,11 @@
import { join } from 'path';
import { Module, RequestMethod } from '@nestjs/common'; import { Module, RequestMethod } from '@nestjs/common';
import { APP_FILTER } from '@nestjs/core'; import { APP_FILTER } from '@nestjs/core';
import { ServeStaticModule } from '@nestjs/serve-static';
import { Connection } from './connection/connection'; import { Connection } from './connection/connection';
import { GlobalExceptionFilter } from './filters/global-exception/global-exception.filter'; import { GlobalExceptionFilter } from './filters/global-exception/global-exception.filter';
import NcPluginMgrv2 from './helpers/NcPluginMgrv2'; import NcPluginMgrv2 from './helpers/NcPluginMgrv2';
import { GlobalMiddleware } from './middlewares/global/global.middleware'; import { GlobalMiddleware } from './middlewares/global/global.middleware';
import { GuiMiddleware } from './middlewares/gui/gui.middleware'; import { GuiMiddleware } from './middlewares/gui/gui.middleware';
import { PublicMiddleware } from './middlewares/public/public.middleware'
import { DatasModule } from './modules/datas/datas.module'; import { DatasModule } from './modules/datas/datas.module';
import { AuthService } from './services/auth.service'; import { AuthService } from './services/auth.service';
import { UsersModule } from './modules/users/users.module'; 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 { LocalStrategy } from './strategies/local.strategy';
import { AuthTokenStrategy } from './strategies/authtoken.strategy/authtoken.strategy'; import { AuthTokenStrategy } from './strategies/authtoken.strategy/authtoken.strategy';
import { BaseViewStrategy } from './strategies/base-view.strategy/base-view.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 NcUpgrader from './version-upgrader/NcUpgrader';
import { MetasModule } from './modules/metas/metas.module'; import { MetasModule } from './modules/metas/metas.module';
import type { import type {
@ -28,9 +26,6 @@ import NocoCache from './cache/NocoCache';
@Module({ @Module({
imports: [ imports: [
ServeStaticModule.forRoot({
rootPath: join(process.cwd(), 'public'),
}),
GlobalModule, GlobalModule,
UsersModule, UsersModule,
...(process.env['PLAYWRIGHT_TEST'] === 'true' ? [TestModule] : []), ...(process.env['PLAYWRIGHT_TEST'] === 'true' ? [TestModule] : []),
@ -60,6 +55,8 @@ export class AppModule implements OnApplicationBootstrap {
consumer consumer
.apply(GuiMiddleware) .apply(GuiMiddleware)
.forRoutes({ path: '*', method: RequestMethod.GET }) .forRoutes({ path: '*', method: RequestMethod.GET })
.apply(PublicMiddleware)
.forRoutes({ path: '*', method: RequestMethod.GET })
.apply(GlobalMiddleware) .apply(GlobalMiddleware)
.forRoutes({ path: '*', method: RequestMethod.ALL }); .forRoutes({ path: '*', method: RequestMethod.ALL });
} }

7
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();
});
});

18
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);
}
}
Loading…
Cancel
Save