From 8ed52c5d74efaa45059df8faece96444182f7251 Mon Sep 17 00:00:00 2001 From: zhumeisongsong Date: Wed, 13 Nov 2024 14:22:40 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=E2=99=BB=EF=B8=8F=20add=20protocol?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.sample | 8 +-- apps/gateway/src/app/app.module.ts | 2 +- apps/gateway/src/main.ts | 10 ++-- apps/users-application/src/main.ts | 8 +-- .../src/lib/applications.config.spec.ts | 52 +++++++++++++------ libs/config/src/lib/applications.config.ts | 6 ++- 6 files changed, 55 insertions(+), 31 deletions(-) diff --git a/.env.sample b/.env.sample index 215f993..23d26e2 100644 --- a/.env.sample +++ b/.env.sample @@ -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 diff --git a/apps/gateway/src/app/app.module.ts b/apps/gateway/src/app/app.module.ts index 1e0e9c7..80c1e35 100644 --- a/apps/gateway/src/app/app.module.ts +++ b/apps/gateway/src/app/app.module.ts @@ -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`, }, ], }), diff --git a/apps/gateway/src/main.ts b/apps/gateway/src/main.ts index 10666c2..1e16adf 100644 --- a/apps/gateway/src/main.ts +++ b/apps/gateway/src/main.ts @@ -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(); diff --git a/apps/users-application/src/main.ts b/apps/users-application/src/main.ts index f7c50ce..e10bcd8 100644 --- a/apps/users-application/src/main.ts +++ b/apps/users-application/src/main.ts @@ -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(); diff --git a/libs/config/src/lib/applications.config.spec.ts b/libs/config/src/lib/applications.config.spec.ts index b35fc0d..5628a4b 100644 --- a/libs/config/src/lib/applications.config.spec.ts +++ b/libs/config/src/lib/applications.config.spec.ts @@ -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']; }); @@ -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']; }); diff --git a/libs/config/src/lib/applications.config.ts b/libs/config/src/lib/applications.config.ts index d8655dd..7aafa77 100644 --- a/libs/config/src/lib/applications.config.ts +++ b/libs/config/src/lib/applications.config.ts @@ -1,7 +1,7 @@ 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', @@ -9,11 +9,13 @@ const DEFAULT_PORT = { }; 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',