Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: ♻️ Create a ConfigModule that exposes a ConfigService which loads the appropriate .env file #28

Merged
merged 8 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .env.sample
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
PROTOCOL=http

GATEWAY_HOST=localhost
GATEWAY_PORT=3333

USER_HOST=localhost
USER_PORT=15001

TASK_HOST=localhost
TASK_PORT=15002
TASK_PORT=15002

DATABASE_HOST=127.0.0.1
DATABASE_PORT=3306
30 changes: 24 additions & 6 deletions apps/gateway/src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,38 @@
import { IntrospectAndCompose } from '@apollo/gateway';
import { gatewayConfig, userAppConfig } from '@libs/config';
import { ApolloGatewayDriver, ApolloGatewayDriverConfig } from '@nestjs/apollo';
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { GraphQLModule } from '@nestjs/graphql';
import { subgraphsConfig } from '@graphql-federation-workspace/applications-config';

import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
imports: [
GraphQLModule.forRoot<ApolloGatewayDriverConfig>({
ConfigModule.forRoot({
isGlobal: true,
load: [gatewayConfig, userAppConfig],
}),
GraphQLModule.forRootAsync<ApolloGatewayDriverConfig>({
imports: [ConfigModule],
inject: [ConfigService],
driver: ApolloGatewayDriver,
gateway: {
supergraphSdl: new IntrospectAndCompose({
subgraphs: subgraphsConfig,
}),
useFactory: (configService: ConfigService) => {
const userAppConfig = configService.get('userApp');
return {
driver: ApolloGatewayDriver,
gateway: {
supergraphSdl: new IntrospectAndCompose({
subgraphs: [
{
name: userAppConfig.name,
url: `${userAppConfig.protocol}://${userAppConfig.host}:${userAppConfig.port}/graphql`,
},
],
}),
},
};
zhumeisongsong marked this conversation as resolved.
Show resolved Hide resolved
},
}),
],
Expand Down
12 changes: 8 additions & 4 deletions apps/gateway/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@
*/

import { Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { NestFactory } from '@nestjs/core';
import { gatewayConfig } from '@graphql-federation-workspace/applications-config';

import { AppModule } from './app/app.module';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
const port = gatewayConfig.port;

await app.listen(port);
Logger.log(`🚀 Application is running on: ${gatewayConfig.host}:${port}`);
const configService = app.get(ConfigService);
const config = configService.get('gateway');

await app.listen(config.port);
Logger.log(
`🚀 Application is running on: ${config.protocol}://${config.host}:${config.port}`,
);
}

bootstrap();
10 changes: 9 additions & 1 deletion apps/users-application/src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import { userAppConfig } from '@libs/config';
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';

import { AppController } from './app.controller';
import { AppService } from './app.service';
import { UsersModule } from '../users/users.module';

@Module({
imports: [UsersModule],
imports: [
ConfigModule.forRoot({
isGlobal: true,
load: [userAppConfig],
}),
UsersModule,
],
controllers: [AppController],
providers: [AppService],
})
Expand Down
12 changes: 8 additions & 4 deletions apps/users-application/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@
*/

import { Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { NestFactory } from '@nestjs/core';
import { userSubGraph } from '@graphql-federation-workspace/applications-config';

import { AppModule } from './app/app.module';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
const port = userSubGraph.port;

await app.listen(port);
Logger.log(`🚀 Application is running on: ${userSubGraph.host}:${port}`);
const configService = app.get(ConfigService);
const config = configService.get('userApp');

await app.listen(config.port);
Logger.log(
` 🚀 Application is running on: ${config.protocol}://${config.host}:${config.port}`,
);
}

bootstrap();
9 changes: 0 additions & 9 deletions libs/application-config/project.json

This file was deleted.

2 changes: 0 additions & 2 deletions libs/application-config/src/index.ts

This file was deleted.

73 changes: 0 additions & 73 deletions libs/application-config/src/lib/applications-config.spec.ts

This file was deleted.

33 changes: 0 additions & 33 deletions libs/application-config/src/lib/applications-config.ts

This file was deleted.

12 changes: 0 additions & 12 deletions libs/application-config/src/lib/subgraphs-config.spec.ts

This file was deleted.

10 changes: 0 additions & 10 deletions libs/application-config/src/lib/subgraphs-config.ts

This file was deleted.

File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export default {
displayName: 'applications-config',
displayName: 'config',
preset: '../../jest.preset.js',
testEnvironment: 'node',
transform: {
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../coverage/libs/application-config',
coverageDirectory: '../../coverage/libs/config',
zhumeisongsong marked this conversation as resolved.
Show resolved Hide resolved
};
9 changes: 9 additions & 0 deletions libs/config/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "config",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "libs/config/src",
"projectType": "library",
"tags": [],
"// targets": "to see all targets run: nx show project config --web",
"targets": {}
}
2 changes: 2 additions & 0 deletions libs/config/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './lib/applications.config';
export * from './lib/database.config';
61 changes: 61 additions & 0 deletions libs/config/src/lib/applications.config.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { gatewayConfig, userAppConfig } from './applications.config';

describe('Config Tests', () => {
describe('gatewayConfig', () => {
it('should return default gateway config when no environment variables are set', () => {
const config = gatewayConfig();
expect(config).toEqual({
protocol: 'http',
host: 'localhost',
port: 3333,
});
});

it('should return gateway config with environment variables', () => {
process.env['PROTOCOL'] = 'https';
process.env['GATEWAY_HOST'] = 'gateway.example.com';
process.env['GATEWAY_PORT'] = '4444';

const config = gatewayConfig();
expect(config).toEqual({
protocol: 'https',
host: 'gateway.example.com',
port: 4444,
});

delete process.env['PROTOCOL'];
delete process.env['GATEWAY_HOST'];
delete process.env['GATEWAY_PORT'];
});
});

describe('userAppConfig', () => {
it('should return default user app config when no environment variables are set', () => {
const config = userAppConfig();
expect(config).toEqual({
protocol: 'http',
host: 'localhost',
port: 15001,
name: 'user',
});
});

it('should return user app config with environment variables', () => {
process.env['PROTOCOL'] = 'https';
process.env['USER_HOST'] = 'user.example.com';
process.env['USER_PORT'] = '5555';

const config = userAppConfig();
expect(config).toEqual({
protocol: 'https',
host: 'user.example.com',
port: 5555,
name: 'user',
});

delete process.env['PROTOCOL'];
delete process.env['USER_HOST'];
delete process.env['USER_PORT'];
});
});
});
Loading