Skip to content

Commit

Permalink
refactor: ♻️ add protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
zhumeisongsong committed Nov 13, 2024
1 parent ae34fe3 commit 8ed52c5
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 31 deletions.
8 changes: 5 additions & 3 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
GATEWAY_HOST=http://localhost
PROTOCOL=http

GATEWAY_HOST=localhost
GATEWAY_PORT=3333

USER_HOST=http://localhost
USER_HOST=localhost
USER_PORT=15001

TASK_HOST=http://localhost
TASK_HOST=localhost
TASK_PORT=15002

DATABASE_USER=test
Expand Down
2 changes: 1 addition & 1 deletion apps/gateway/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { AppService } from './app.service';
subgraphs: [
{
name: userAppConfig.name,
url: `${userAppConfig.host}:${userAppConfig.port}/graphql`,
url: `${userAppConfig.protocol}://${userAppConfig.host}:${userAppConfig.port}/graphql`,
},
],
}),
Expand Down
10 changes: 5 additions & 5 deletions apps/gateway/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import { AppModule } from './app/app.module';

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

const configService = app.get(ConfigService);
const config = configService.get('gateway');
const host = config.host;
const port = config.port;

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

bootstrap();
8 changes: 4 additions & 4 deletions apps/users-application/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ async function bootstrap() {

const configService = app.get(ConfigService);
const config = configService.get('userApp');
const host = config.host;
const port = config.port;

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

bootstrap();
52 changes: 36 additions & 16 deletions libs/config/src/lib/applications.config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,26 @@ describe('Config Tests', () => {
describe('gatewayConfig', () => {
it('should return default values when environment variables are not set', () => {
const config = gatewayConfig();
expect(config.host).toBe('http://localhost');
expect(config.port).toBe('3333');
expect(config).toEqual({
protocol: 'http',
host: 'localhost',
port: '3333',
});
});

it('should return environment values when environment variables are set', () => {
process.env['GATEWAY_HOST'] = 'http://gateway-host';
process.env['GATEWAY_PORT'] = '4000';
it('should return environment variable values when they are set', () => {
process.env['PROTOCOL'] = 'https';
process.env['GATEWAY_HOST'] = 'gateway.example.com';
process.env['GATEWAY_PORT'] = '4444';

const config = gatewayConfig();
expect(config.host).toBe('http://gateway-host');
expect(config.port).toBe('4000');
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'];
});
Expand All @@ -22,18 +32,28 @@ describe('Config Tests', () => {
describe('userAppConfig', () => {
it('should return default values when environment variables are not set', () => {
const config = userAppConfig();
expect(config.host).toBe('http://localhost');
expect(config.port).toBe('15001');
expect(config.name).toBe('user');
expect(config).toEqual({
protocol: 'http',
host: 'localhost',
port: '15001',
name: 'user',
});
});

it('should return environment values when environment variables are set', () => {
process.env['USER_HOST'] = 'http://user-host';
process.env['USER_PORT'] = '5000';
it('should return environment variable values when they are set', () => {
process.env['PROTOCOL'] = 'https';
process.env['USER_HOST'] = 'user.example.com';
process.env['USER_PORT'] = '5555';

const config = userAppConfig();
expect(config.host).toBe('http://user-host');
expect(config.port).toBe('5000');
expect(config.name).toBe('user');
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'];
});
Expand Down
6 changes: 4 additions & 2 deletions libs/config/src/lib/applications.config.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import { registerAs } from '@nestjs/config';

const DEFAULT_HOST = 'http://localhost';

const DEFAULT_PROTOCOL = 'http';
const DEFAULT_HOST = 'localhost';
const DEFAULT_PORT = {
user: '15001',
task: '15002',
gateway: '3333',
};

export const gatewayConfig = registerAs('gateway', () => ({
protocol: process.env['PROTOCOL'] ?? DEFAULT_PROTOCOL,
host: process.env['GATEWAY_HOST'] ?? DEFAULT_HOST,
port: process.env['GATEWAY_PORT'] ?? DEFAULT_PORT.gateway,
}));

export const userAppConfig = registerAs('userApp', () => ({
protocol: process.env['PROTOCOL'] ?? DEFAULT_PROTOCOL,
host: process.env['USER_HOST'] ?? DEFAULT_HOST,
port: process.env['USER_PORT'] ?? DEFAULT_PORT.user,
name: 'user',
Expand Down

0 comments on commit 8ed52c5

Please sign in to comment.