-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from zhumeisongsong/feature/add-env-config
✨ add applicaiton config
- Loading branch information
Showing
25 changed files
with
215 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,3 +40,6 @@ Thumbs.db | |
|
||
.nx/cache | ||
.nx/workspace-data | ||
|
||
.env | ||
.env.local |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
apps/users-application-e2e/src/users-application/users-application.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const baseConfig = require('../../eslint.config.js'); | ||
|
||
module.exports = [...baseConfig]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './lib/applications-config'; |
67 changes: 67 additions & 0 deletions
67
libs/application-config/src/lib/applications-config.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters