Skip to content

Commit

Permalink
added gateway microservice
Browse files Browse the repository at this point in the history
  • Loading branch information
idraganovmm committed Jun 19, 2024
1 parent afba9ca commit cb3bb1a
Show file tree
Hide file tree
Showing 13 changed files with 389 additions and 37 deletions.
9 changes: 9 additions & 0 deletions nest-ms-pg-auth0/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,14 @@ AUTH0_CLIENT_ID=CLIENT_ID
AUTH0_AUDIENCE=AUDIENCE
AUTH0_CLIENT_SECRET=CLIENT_SECRET

# Microservices URLs
AUTH_API_HOST=auth-dev
AUTH_API_PORT=3001
AUTH_API_URL=http://${AUTH_API_HOST}:${AUTH_API_PORT}/
TODOS_API_HOST=todos-dev
TODOS_API_PORT=3002
TODOS_API_URL=http://${TODOS_API_HOST}:${TODOS_API_PORT}/

# RabbitMQ URI and queues
RABBIT_MQ_URI=amqp://rabbitmq:5672
RABBIT_MQ_AUTH_QUEUE=auth
14 changes: 10 additions & 4 deletions nest-ms-pg-auth0/apps/auth/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@ async function bootstrap() {
app.enableCors();

// add security HTTP headers
app.register(helmet);
app.register(
helmet as unknown as Parameters<NestFastifyApplication['register']>[0],
);

// compresses response bodies
app.register(compression);
app.register(
compression as unknown as Parameters<NestFastifyApplication['register']>[0],
);

// enable validation globally
app.useGlobalPipes(new ValidationPipe({ transform: true, whitelist: true }));
Expand Down Expand Up @@ -62,8 +66,10 @@ async function bootstrap() {
app.useGlobalFilters(new ErrorLoggingFilter(httpAdapterHost));
}

await app.listen(port, configService.HOST, () => {
console.log(`App is running on http://${configService.HOST}:${port}`);
const openPort = process.env.AUTH_API_PORT ?? port;

await app.listen(openPort, configService.HOST, () => {
console.log(`App is running on http://${configService.HOST}:${openPort}`);
});
}
bootstrap();
39 changes: 39 additions & 0 deletions nest-ms-pg-auth0/apps/gateway/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import fastify from 'fastify';
import fastifyProxy, { FastifyHttpProxyOptions } from '@fastify/http-proxy';

interface MicroserviceData {
uri: string;
prefix: string;
}

async function bootstrap() {
const fastifyServer = fastify({ logger: { level: 'trace' } });

const proxyData: FastifyHttpProxyOptions[] = [
{ uri: `${process.env.AUTH_API_URL}`, prefix: 'auth' },
{ uri: `${process.env.TODOS_API_URL}`, prefix: 'todos' },
].map(
({ uri, prefix }: MicroserviceData): FastifyHttpProxyOptions => ({
upstream: uri,
prefix: `/:version/${prefix}`,
rewritePrefix: `/:version/${prefix}`,
}),
);

fastifyServer.get('/', async () => {
return 'Hello Gateway API';
});

for (const proxy of proxyData) {
fastifyServer.register(fastifyProxy, proxy);
}

fastifyServer.listen(
{ port: +`${process.env.PORT ?? 3000}`, host: '0.0.0.0' },
(err, address) => {
if (err) throw err;
fastifyServer.log.info(`Server listening on ${address}`);
},
);
}
bootstrap();
24 changes: 24 additions & 0 deletions nest-ms-pg-auth0/apps/gateway/test/app.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import * as request from 'supertest';
import { GatewayModule } from './../src/gateway.module';

describe('GatewayController (e2e)', () => {
let app: INestApplication;

beforeEach(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [GatewayModule],
}).compile();

app = moduleFixture.createNestApplication();
await app.init();
});

it('/ (GET)', () => {
return request(app.getHttpServer())
.get('/')
.expect(200)
.expect('Hello World!');
});
});
9 changes: 9 additions & 0 deletions nest-ms-pg-auth0/apps/gateway/test/jest-e2e.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"moduleFileExtensions": ["js", "json", "ts"],
"rootDir": ".",
"testEnvironment": "node",
"testRegex": ".e2e-spec.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
}
}
9 changes: 9 additions & 0 deletions nest-ms-pg-auth0/apps/gateway/tsconfig.app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"declaration": false,
"outDir": "../../dist/apps/gateway"
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist", "test", "**/*spec.ts"]
}
14 changes: 10 additions & 4 deletions nest-ms-pg-auth0/apps/todos/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,14 @@ async function bootstrap() {
app.enableCors();

// add security HTTP headers
app.register(helmet);
app.register(
helmet as unknown as Parameters<NestFastifyApplication['register']>[0],
);

// compresses response bodies
app.register(compression);
app.register(
compression as unknown as Parameters<NestFastifyApplication['register']>[0],
);

// enable validation globally
app.useGlobalPipes(new ValidationPipe({ transform: true, whitelist: true }));
Expand Down Expand Up @@ -63,9 +67,11 @@ async function bootstrap() {
app.useGlobalFilters(new ErrorLoggingFilter(httpAdapterHost));
}

const openPort = process.env.TODOS_API_PORT ?? port;

// start server
await app.listen(port, configService.HOST, () => {
console.log(`App is running on http://${configService.HOST}:${port}`);
await app.listen(openPort, configService.HOST, () => {
console.log(`App is running on http://${configService.HOST}:${openPort}`);
});
}
bootstrap();
58 changes: 52 additions & 6 deletions nest-ms-pg-auth0/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,50 @@ services:
extends:
file: ./docker-services/rabbitmq-service.yml
service: rabbitmq
networks:
- app-tier

db:
extends:
file: ./docker-services/postgres-service.yml
service: db
ports:
- '5433:${PGPORT}'
networks:
- app-tier
depends_on:
- rabbitmq

gateway:
extends:
file: ./docker-services/gateway-service.yml
service: gateway
ports:
- '3000:${PORT}'
depends_on:
- db
- rabbitmq

gateway-dev:
extends:
file: ./docker-services/gateway-service.yml
service: gateway-dev
ports:
- '3000:${PORT}'
networks:
- app-tier
depends_on:
- db
- rabbitmq

auth:
extends:
file: ./docker-services/auth-service.yml
service: auth
ports:
- '3000:${PORT}'
- '${AUTH_API_PORT}:${AUTH_API_PORT}'
expose:
- '${AUTH_API_PORT}'
depends_on:
- db
- rabbitmq
Expand All @@ -31,7 +59,11 @@ services:
file: ./docker-services/auth-service.yml
service: auth-dev
ports:
- '3000:${PORT}'
- '${AUTH_API_PORT}:${AUTH_API_PORT}'
expose:
- '${AUTH_API_PORT}'
networks:
- app-tier
depends_on:
- db
- rabbitmq
Expand All @@ -41,7 +73,9 @@ services:
file: ./docker-services/todos-service.yml
service: todos
ports:
- '3001:${PORT}'
- '${TODOS_API_PORT}:${TODOS_API_PORT}'
expose:
- '${TODOS_API_PORT}'
depends_on:
- db
- rabbitmq
Expand All @@ -51,14 +85,26 @@ services:
file: ./docker-services/todos-service.yml
service: todos-dev
ports:
- '3001:${PORT}'
- '${TODOS_API_PORT}:${TODOS_API_PORT}'
expose:
- '${TODOS_API_PORT}'
networks:
- app-tier
depends_on:
- db
- rabbitmq

volumes:
todos:
todos-node_modules:
gateway:
gateway-node_modules:
auth:
auth-node_modules:
todos:
todos-node_modules:
db-data:

networks:
app-tier:
driver: bridge
ipam:
driver: default
52 changes: 52 additions & 0 deletions nest-ms-pg-auth0/docker-services/gateway-service.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
services:

gateway:
build:
context: ../
dockerfile: Dockerfile
target: prod
args:
- SERVICE_NAME=gateway
container_name: gateway
image: gateway
env_file:
- ../.env
environment:
# This overrides the .env file
- HOST=0.0.0.0
# Mount host directory to docker container to support watch mode
volumes:
- gateway
profiles:
- prod



gateway-dev:
build:
context: ../
dockerfile: Dockerfile
target: dev
args:
- DOCKER_BUILDKIT=1
- SERVICE_NAME=gateway
container_name: gateway-dev
image: gateway-dev
env_file:
- ../.env
environment:
# This overrides the .env file
- HOST=0.0.0.0
# Mount host directory to docker container to support watch mode
volumes:
- ../apps/gateway:/usr/src/app/apps/gateway
- ../libs:/usr/src/app/libs
# This ensures that the NestJS container manages the node_modules folder
# rather than synchronizes it with the host machine
- gateway-node_modules:/usr/src/app/node_modules
profiles:
- dev
command: >
sh -c "cd /usr/src/app
&& npm run db:migrate:latest
&& npm run start:dev -- gateway"
9 changes: 9 additions & 0 deletions nest-ms-pg-auth0/nest-cli.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@
"compilerOptions": {
"tsConfigPath": "libs/common/tsconfig.lib.json"
}
},
"gateway": {
"type": "application",
"root": "apps/gateway",
"entryFile": "main",
"sourceRoot": "apps/gateway/src",
"compilerOptions": {
"tsConfigPath": "apps/gateway/tsconfig.app.json"
}
}
}
}
Loading

0 comments on commit cb3bb1a

Please sign in to comment.