Skip to content

Commit

Permalink
Merge pull request #7 from zhumeisongsong/feature/add-env-config
Browse files Browse the repository at this point in the history
✨ add applicaiton config
  • Loading branch information
zhumeisongsong authored Nov 7, 2024
2 parents a7c5712 + c0d5da7 commit aa18c86
Show file tree
Hide file tree
Showing 25 changed files with 215 additions and 27 deletions.
8 changes: 8 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
GATEWAY_HOST=localhost
GATEWAY_PORT=3333

USER_HOST=localhost
USER_PORT=15001

TASK_HOST=localhost
TASK_PORT=15002
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,6 @@ Thumbs.db

.nx/cache
.nx/workspace-data

.env
.env.local
6 changes: 3 additions & 3 deletions apps/gateway-e2e/src/gateway/gateway.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import axios from 'axios';

describe('GET /api', () => {
describe('GET /', () => {
it('should return a message', async () => {
const res = await axios.get(`/api`);
const res = await axios.get(`/`);

expect(res.status).toBe(200);
expect(res.data).toEqual({ message: 'Hello API' });
expect(res.data).toEqual({ message: 'Hello Gateway' });
});
});
2 changes: 1 addition & 1 deletion apps/gateway-e2e/src/support/test-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import axios from 'axios';
module.exports = async function () {
// Configure axios for tests to use.
const host = process.env.HOST ?? 'localhost';
const port = process.env.PORT ?? '3000';
const port = process.env.PORT ?? '3333';
axios.defaults.baseURL = `http://${host}:${port}`;
};
4 changes: 2 additions & 2 deletions apps/gateway/src/app/app.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ describe('AppController', () => {
});

describe('getData', () => {
it('should return "Hello API"', () => {
it('should return "Hello Gateway"', () => {
const appController = app.get<AppController>(AppController);
expect(appController.getData()).toEqual({ message: 'Hello API' });
expect(appController.getData()).toEqual({ message: 'Hello Gateway' });
});
});
});
4 changes: 2 additions & 2 deletions apps/gateway/src/app/app.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ describe('AppService', () => {
});

describe('getData', () => {
it('should return "Hello API"', () => {
expect(service.getData()).toEqual({ message: 'Hello API' });
it('should return "Hello Gateway"', () => {
expect(service.getData()).toEqual({ message: 'Hello Gateway' });
});
});
});
2 changes: 1 addition & 1 deletion apps/gateway/src/app/app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import { Injectable } from '@nestjs/common';
@Injectable()
export class AppService {
getData(): { message: string } {
return { message: 'Hello API' };
return { message: 'Hello Gateway' };
}
}
8 changes: 4 additions & 4 deletions apps/gateway/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@

import { Logger } from '@nestjs/common';
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 globalPrefix = 'api';
app.setGlobalPrefix(globalPrefix);
const port = process.env.PORT || 3000;
const port = gatewayConfig.port;

await app.listen(port);
Logger.log(
`🚀 Application is running on: http://localhost:${port}/${globalPrefix}`
`🚀 Application is running on: http://${gatewayConfig.host}:${port}`
);
}

Expand Down
2 changes: 1 addition & 1 deletion apps/users-application-e2e/src/support/test-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import axios from 'axios';
module.exports = async function () {
// Configure axios for tests to use.
const host = process.env.HOST ?? 'localhost';
const port = process.env.PORT ?? '3000';
const port = process.env.PORT ?? '15001';
axios.defaults.baseURL = `http://${host}:${port}`;
};
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import axios from 'axios';

describe('GET /api', () => {
describe('GET /', () => {
it('should return a message', async () => {
const res = await axios.get(`/api`);
const res = await axios.get(`/`);

expect(res.status).toBe(200);
expect(res.data).toEqual({ message: 'Hello API' });
expect(res.data).toEqual({ message: 'Hello User Service' });
});
});
4 changes: 2 additions & 2 deletions apps/users-application/src/app/app.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ describe('AppController', () => {
});

describe('getData', () => {
it('should return "Hello API"', () => {
it('should return "Hello User Service"', () => {
const appController = app.get<AppController>(AppController);
expect(appController.getData()).toEqual({ message: 'Hello API' });
expect(appController.getData()).toEqual({ message: 'Hello User Service' });
});
});
});
4 changes: 2 additions & 2 deletions apps/users-application/src/app/app.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ describe('AppService', () => {
});

describe('getData', () => {
it('should return "Hello API"', () => {
expect(service.getData()).toEqual({ message: 'Hello API' });
it('should return "Hello User Service"', () => {
expect(service.getData()).toEqual({ message: 'Hello User Service' });
});
});
});
2 changes: 1 addition & 1 deletion apps/users-application/src/app/app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import { Injectable } from '@nestjs/common';
@Injectable()
export class AppService {
getData(): { message: string } {
return { message: 'Hello API' };
return { message: 'Hello User Service' };
}
}
8 changes: 4 additions & 4 deletions apps/users-application/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@

import { Logger } from '@nestjs/common';
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 globalPrefix = 'api';
app.setGlobalPrefix(globalPrefix);
const port = process.env.PORT || 3000;
const port = userSubGraph.port;

await app.listen(port);
Logger.log(
`🚀 Application is running on: http://localhost:${port}/${globalPrefix}`
`🚀 Application is running on: http://${userSubGraph.host}:${port}`
);
}

Expand Down
7 changes: 7 additions & 0 deletions libs/application-config/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# applications-config

This library was generated with [Nx](https://nx.dev).

## Running unit tests

Run `nx test applications-config` to execute the unit tests via [Jest](https://jestjs.io).
3 changes: 3 additions & 0 deletions libs/application-config/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const baseConfig = require('../../eslint.config.js');

module.exports = [...baseConfig];
10 changes: 10 additions & 0 deletions libs/application-config/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default {
displayName: 'applications-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',
};
9 changes: 9 additions & 0 deletions libs/application-config/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "applications-config",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "libs/application-config/src",
"projectType": "library",
"tags": [],
"// targets": "to see all targets run: nx show project applications-config --web",
"targets": {}
}
1 change: 1 addition & 0 deletions libs/application-config/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './lib/applications-config';
67 changes: 67 additions & 0 deletions libs/application-config/src/lib/applications-config.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
describe('Service Configurations', () => {
const originalEnv = process.env;

beforeEach(() => {
jest.resetModules();
process.env = { ...originalEnv };
});

afterEach(() => {
process.env = originalEnv;
});

it('should use default values when environment variables are not set', () => {
const {
gatewayConfig,
userSubGraph,
taskSubGraph,
} = require('./applications-config');

expect(gatewayConfig).toEqual({
host: 'localhost',
port: '3333',
});

expect(userSubGraph).toEqual({
host: 'localhost',
port: '15001',
});

expect(taskSubGraph).toEqual({
host: 'localhost',
port: '15002',
});
});

it('should use environment variables when they are set', () => {
process.env['GATEWAY_HOST'] = 'gateway.example.com';
process.env['GATEWAY_PORT'] = '4000';
process.env['USER_HOST'] = 'user.example.com';
process.env['USER_PORT'] = '5000';
process.env['TASK_HOST'] = 'task.example.com';
process.env['TASK_PORT'] = '6000';

jest.resetModules();

const {
gatewayConfig,
userSubGraph,
taskSubGraph,
} = require('./applications-config');

expect(gatewayConfig).toEqual({
host: 'gateway.example.com',
port: '4000',
});

expect(userSubGraph).toEqual({
host: 'user.example.com',
port: '5000',
});

expect(taskSubGraph).toEqual({
host: 'task.example.com',
port: '6000',
});
});
});
29 changes: 29 additions & 0 deletions libs/application-config/src/lib/applications-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
interface ApplicationConfig {
host: string;
port: string;
}

const DEFAULT_HOST = 'localhost';

const DEFAULT_PORT = {
user: '15001',
task: '15002',
gateway: '3333',
};

// Gateway
export const gatewayConfig: ApplicationConfig = {
host: process.env['GATEWAY_HOST'] ?? DEFAULT_HOST,
port: process.env['GATEWAY_PORT'] ?? DEFAULT_PORT.gateway,
};

// Graphql
export const userSubGraph: ApplicationConfig = {
host: process.env['USER_HOST'] ?? DEFAULT_HOST,
port: process.env['USER_PORT'] ?? DEFAULT_PORT.user,
};

export const taskSubGraph: ApplicationConfig = {
host: process.env['TASK_HOST'] ?? DEFAULT_HOST,
port: process.env['TASK_PORT'] ?? DEFAULT_PORT.task,
};
22 changes: 22 additions & 0 deletions libs/application-config/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"module": "commonjs",
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitOverride": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"noPropertyAccessFromIndexSignature": true
},
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
},
{
"path": "./tsconfig.spec.json"
}
]
}
11 changes: 11 additions & 0 deletions libs/application-config/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "commonjs",
"outDir": "../../dist/out-tsc",
"declaration": true,
"types": ["node"]
},
"exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"],
"include": ["src/**/*.ts"]
}
14 changes: 14 additions & 0 deletions libs/application-config/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"module": "commonjs",
"types": ["jest", "node"]
},
"include": [
"jest.config.ts",
"src/**/*.test.ts",
"src/**/*.spec.ts",
"src/**/*.d.ts"
]
}
6 changes: 5 additions & 1 deletion tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
"skipLibCheck": true,
"skipDefaultLibCheck": true,
"baseUrl": ".",
"paths": {}
"paths": {
"@graphql-federation-workspace/applications-config": [
"libs/application-config/src/index.ts"
]
}
},
"exclude": ["node_modules", "tmp"]
}

0 comments on commit aa18c86

Please sign in to comment.