Browse Source

feat: add a global middleware to load ncSiteUrl

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

1
packages/nocodb-nest/src/Noco.ts

@ -8,6 +8,7 @@ import Store from './models/Store';
export default class Noco { export default class Noco {
private static _this: Noco; private static _this: Noco;
private static ee: boolean; private static ee: boolean;
public static readonly env: string = '_noco';
public static get dashboardUrl(): string { public static get dashboardUrl(): string {
let siteUrl = `http://localhost:${process.env.PORT || 8080}`; let siteUrl = `http://localhost:${process.env.PORT || 8080}`;

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

@ -2,15 +2,14 @@ import { MiddlewareConsumer, Module, RequestMethod } from '@nestjs/common';
import { APP_FILTER } from '@nestjs/core'; import { APP_FILTER } from '@nestjs/core';
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 { GlobalMiddleware } from './middlewares/global/global.middleware';
import { AuthModule } from './modules/auth/auth.module'; import { AuthModule } from './modules/auth/auth.module';
import { ExtractProjectIdMiddleware } from './middlewares/extract-project-id/extract-project-id.middleware'; import { ExtractProjectIdMiddleware } from './middlewares/extract-project-id/extract-project-id.middleware';
import { UsersModule } from './modules/users/users.module'; import { UsersModule } from './modules/users/users.module';
import { MetaService } from './meta/meta.service'; import { MetaService } from './meta/meta.service';
// import { LocalStrategy } from './strategies/local.strategy';
import { UtilsModule } from './modules/utils/utils.module'; import { UtilsModule } from './modules/utils/utils.module';
import { ProjectsModule } from './modules/projects/projects.module'; import { ProjectsModule } from './modules/projects/projects.module';
import { JwtStrategy } from './strategies/jwt.strategy'; import { JwtStrategy } from './strategies/jwt.strategy';
// import { AuthGuard } from '@nestjs/passport';
import { TablesModule } from './modules/tables/tables.module'; import { TablesModule } from './modules/tables/tables.module';
import { ViewsModule } from './modules/views/views.module'; import { ViewsModule } from './modules/views/views.module';
import { FiltersModule } from './modules/filters/filters.module'; import { FiltersModule } from './modules/filters/filters.module';
@ -106,8 +105,8 @@ import { PluginsModule } from './modules/plugins/plugins.module';
}) })
export class AppModule { export class AppModule {
configure(consumer: MiddlewareConsumer) { configure(consumer: MiddlewareConsumer) {
// consumer consumer
// .apply(ExtractProjectIdMiddleware) .apply(GlobalMiddleware)
// .forRoutes({ path: '*', method: RequestMethod.ALL }); .forRoutes({ path: '*', method: RequestMethod.ALL });
} }
} }

7
packages/nocodb-nest/src/middlewares/global/global.middleware.spec.ts

@ -0,0 +1,7 @@
import { GlobalMiddleware } from './global.middleware';
describe('GlobalMiddleware', () => {
it('should be defined', () => {
expect(new GlobalMiddleware()).toBeDefined();
});
});

14
packages/nocodb-nest/src/middlewares/global/global.middleware.ts

@ -0,0 +1,14 @@
import { Injectable, NestMiddleware } from '@nestjs/common';
import Noco from '../../Noco';
@Injectable()
export class GlobalMiddleware implements NestMiddleware {
use(req: any, res: any, next: () => void) {
req.ncSiteUrl =
Noco.config?.envs?.[Noco.env]?.publicUrl ||
Noco.config?.publicUrl ||
req.protocol + '://' + req.get('host');
req.ncFullUrl = req.protocol + '://' + req.get('host') + req.originalUrl;
next();
}
}
Loading…
Cancel
Save