Skip to content

Commit 460b256

Browse files
committed
fear(users-microservice): implemented
1 parent 9d1f489 commit 460b256

File tree

18 files changed

+467
-7
lines changed

18 files changed

+467
-7
lines changed
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
import { Controller, Get } from "@nestjs/common";
1+
import { Controller, Get, Inject } from "@nestjs/common";
2+
import { ClientProxy } from "@nestjs/microservices";
23

3-
import { AppService } from "./app.service";
4+
import { NATS_MESSAGES, SERVICE_NAMES } from "@app/api-core";
45

5-
@Controller()
6+
@Controller("users")
67
export class AppController {
7-
constructor(private readonly appService: AppService) {}
8+
constructor(@Inject(SERVICE_NAMES.NATS) private natsClient: ClientProxy) {}
89

910
@Get()
10-
getHello(): string {
11-
return this.appService.getHello();
11+
getHello() {
12+
return this.natsClient.send({ cmd: NATS_MESSAGES.USERS.GET }, {});
1213
}
1314
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const nestConfig = require('@app/eslint-config-nest');
2+
3+
module.exports = {
4+
...nestConfig,
5+
parserOptions: {
6+
...nestConfig.parserOptions,
7+
project: './tsconfig.json',
8+
},
9+
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"singleQuote": true,
3+
"trailingComma": "all"
4+
}

apps/users-microservice/Dockerfile

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Use an official Node.js runtime as the base image. Base stage to have pnpm installed
2+
FROM node:20.17-alpine AS base
3+
ENV PNPM_HOME="/pnpm"
4+
ENV PATH="$PNPM_HOME:$PATH"
5+
RUN corepack enable pnpm
6+
7+
# Install Python and build tools
8+
RUN apk add --no-cache python3 make g++ build-base
9+
10+
# DEFINING DEVELOPMENT STAGE
11+
FROM base AS dev
12+
13+
WORKDIR /app
14+
15+
COPY package.json pnpm-lock.yaml pnpm-workspace.yaml ./
16+
COPY apps/users-microservice ./apps/users-microservice
17+
COPY tooling ./tooling
18+
COPY packages ./packages
19+
20+
RUN pnpm install && \
21+
pnpm -r prebuild && \
22+
pnpm -r build && \
23+
pnpm i && \
24+
pnpm prune --prod
25+
26+
# DEFINING PRODUCTION STAGE
27+
FROM base AS prod
28+
29+
WORKDIR /app
30+
COPY --from=dev app/ .
31+
32+
USER node
33+
34+
CMD ["node", "apps/users-microservice/dist/main"]

apps/users-microservice/README.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<p align="center">
2+
<a href="http://nestjs.com/" target="blank"><img src="https://nestjs.com/img/logo-small.svg" width="120" alt="Nest Logo" /></a>
3+
</p>
4+
5+
[circleci-image]: https://img.shields.io/circleci/build/github/nestjs/nest/master?token=abc123def456
6+
[circleci-url]: https://circleci.com/gh/nestjs/nest
7+
8+
<p align="center">A progressive <a href="http://nodejs.org" target="_blank">Node.js</a> framework for building efficient and scalable server-side applications.</p>
9+
<p align="center">
10+
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/v/@nestjs/core.svg" alt="NPM Version" /></a>
11+
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/l/@nestjs/core.svg" alt="Package License" /></a>
12+
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/dm/@nestjs/common.svg" alt="NPM Downloads" /></a>
13+
<a href="https://circleci.com/gh/nestjs/nest" target="_blank"><img src="https://img.shields.io/circleci/build/github/nestjs/nest/master" alt="CircleCI" /></a>
14+
<a href="https://coveralls.io/github/nestjs/nest?branch=master" target="_blank"><img src="https://coveralls.io/repos/github/nestjs/nest/badge.svg?branch=master#9" alt="Coverage" /></a>
15+
<a href="https://discord.gg/G7Qnnhy" target="_blank"><img src="https://img.shields.io/badge/discord-online-brightgreen.svg" alt="Discord"/></a>
16+
<a href="https://opencollective.com/nest#backer" target="_blank"><img src="https://opencollective.com/nest/backers/badge.svg" alt="Backers on Open Collective" /></a>
17+
<a href="https://opencollective.com/nest#sponsor" target="_blank"><img src="https://opencollective.com/nest/sponsors/badge.svg" alt="Sponsors on Open Collective" /></a>
18+
<a href="https://paypal.me/kamilmysliwiec" target="_blank"><img src="https://img.shields.io/badge/Donate-PayPal-ff3f59.svg" alt="Donate us"/></a>
19+
<a href="https://opencollective.com/nest#sponsor" target="_blank"><img src="https://img.shields.io/badge/Support%20us-Open%20Collective-41B883.svg" alt="Support us"></a>
20+
<a href="https://twitter.com/nestframework" target="_blank"><img src="https://img.shields.io/twitter/follow/nestframework.svg?style=social&label=Follow" alt="Follow us on Twitter"></a>
21+
</p>
22+
<!--[![Backers on Open Collective](https://opencollective.com/nest/backers/badge.svg)](https://opencollective.com/nest#backer)
23+
[![Sponsors on Open Collective](https://opencollective.com/nest/sponsors/badge.svg)](https://opencollective.com/nest#sponsor)-->
24+
25+
## Description
26+
27+
[Nest](https://github.com/nestjs/nest) framework TypeScript starter repository.
28+
29+
## Project setup
30+
31+
```bash
32+
$ pnpm install
33+
```
34+
35+
## Compile and run the project
36+
37+
```bash
38+
# development
39+
$ pnpm run start
40+
41+
# watch mode
42+
$ pnpm run start:dev
43+
44+
# production mode
45+
$ pnpm run start:prod
46+
```
47+
48+
## Run tests
49+
50+
```bash
51+
# unit tests
52+
$ pnpm run test
53+
54+
# e2e tests
55+
$ pnpm run test:e2e
56+
57+
# test coverage
58+
$ pnpm run test:cov
59+
```
60+
61+
## Resources
62+
63+
Check out a few resources that may come in handy when working with NestJS:
64+
65+
- Visit the [NestJS Documentation](https://docs.nestjs.com) to learn more about the framework.
66+
- For questions and support, please visit our [Discord channel](https://discord.gg/G7Qnnhy).
67+
- To dive deeper and get more hands-on experience, check out our official video [courses](https://courses.nestjs.com/).
68+
- Visualize your application graph and interact with the NestJS application in real-time using [NestJS Devtools](https://devtools.nestjs.com).
69+
- Need help with your project (part-time to full-time)? Check out our official [enterprise support](https://enterprise.nestjs.com).
70+
- To stay in the loop and get updates, follow us on [X](https://x.com/nestframework) and [LinkedIn](https://linkedin.com/company/nestjs).
71+
- Looking for a job, or have a job to offer? Check out our official [Jobs board](https://jobs.nestjs.com).
72+
73+
## Support
74+
75+
Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support).
76+
77+
## Stay in touch
78+
79+
- Author - [Kamil Myśliwiec](https://twitter.com/kammysliwiec)
80+
- Website - [https://nestjs.com](https://nestjs.com/)
81+
- Twitter - [@nestframework](https://twitter.com/nestframework)
82+
83+
## License
84+
85+
Nest is [MIT licensed](https://github.com/nestjs/nest/blob/master/LICENSE).
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"$schema": "https://json.schemastore.org/nest-cli",
3+
"collection": "@nestjs/schematics",
4+
"sourceRoot": "src",
5+
"compilerOptions": {
6+
"deleteOutDir": true
7+
}
8+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
{
2+
"name": "@app/users-microservice",
3+
"version": "0.0.1",
4+
"private": true,
5+
"description": "",
6+
"license": "UNLICENSED",
7+
"author": "",
8+
"scripts": {
9+
"prebuild": "rm -rf dist",
10+
"build": "pnpm with-env nest build",
11+
"clean": "rm -rf .turbo node_modules dist",
12+
"dev": "pnpm with-env nest start --watch",
13+
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
14+
"lint": "eslint ./src",
15+
"lint:fix": "eslint ./src --fix",
16+
"start": "pnpm with-env nest start",
17+
"start:debug": "pnpm with-env nest start --debug --watch",
18+
"start:prod": "pnpm with-env node dist/main",
19+
"test": "pnpm with-env jest",
20+
"test:cov": "pnpm with-env jest --coverage",
21+
"test:debug": "pnpm with-env node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
22+
"test:e2e": "pnpm with-env jest --config ./test/jest-e2e.json",
23+
"test:watch": "pnpm with-env jest --watch",
24+
"typecheck": "tsc --noEmit --emitDeclarationOnly false",
25+
"with-env": "dotenv -e ../../.env --"
26+
},
27+
"prettier": "@app/prettier-config",
28+
"jest": {
29+
"collectCoverageFrom": [
30+
"**/*.(t|j)s"
31+
],
32+
"coverageDirectory": "../coverage",
33+
"moduleFileExtensions": [
34+
"js",
35+
"json",
36+
"ts"
37+
],
38+
"rootDir": "src",
39+
"testEnvironment": "node",
40+
"testRegex": ".*\\.spec\\.ts$",
41+
"transform": {
42+
"^.+\\.(t|j)s$": "ts-jest"
43+
}
44+
},
45+
"dependencies": {
46+
"@fastify/multipart": "catalog:nestjs",
47+
"@fastify/static": "catalog:nestjs",
48+
"@nestjs/common": "catalog:nestjs",
49+
"@nestjs/config": "catalog:nestjs",
50+
"@nestjs/core": "catalog:nestjs",
51+
"@nestjs/microservices": "catalog:nestjs",
52+
"@nestjs/platform-fastify": "catalog:nestjs",
53+
"class-transformer": "catalog:nestjs",
54+
"class-validator": "catalog:nestjs",
55+
"dotenv-cli": "catalog:",
56+
"nats": "catalog:nestjs",
57+
"reflect-metadata": "catalog:nestjs",
58+
"rxjs": "catalog:nestjs",
59+
"zod": "catalog:"
60+
},
61+
"devDependencies": {
62+
"@app/api-core": "workspace:*",
63+
"@app/constants": "workspace:*",
64+
"@app/eslint-config-nest": "workspace:*",
65+
"@app/tsconfig": "workspace:*",
66+
"@nestjs/cache-manager": "catalog:nestjs",
67+
"@nestjs/cli": "catalog:nestjs",
68+
"@nestjs/schematics": "catalog:nestjs",
69+
"@nestjs/testing": "catalog:nestjs",
70+
"@types/jest": "catalog:",
71+
"@types/node": "catalog:",
72+
"@types/supertest": "catalog:nestjs",
73+
"cache-manager": "catalog:nestjs",
74+
"cache-manager-redis-store": "catalog:nestjs",
75+
"eslint": "catalog:nestjs",
76+
"jest": "catalog:",
77+
"prettier": "catalog:",
78+
"redis": "catalog:nestjs",
79+
"source-map-support": "catalog:nestjs",
80+
"supertest": "catalog:nestjs",
81+
"ts-jest": "catalog:nestjs",
82+
"ts-loader": "catalog:nestjs",
83+
"ts-node": "catalog:nestjs",
84+
"tsconfig-paths": "catalog:nestjs",
85+
"typescript": "catalog:"
86+
}
87+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Controller } from "@nestjs/common";
2+
import { MessagePattern } from "@nestjs/microservices";
3+
4+
import { NATS_MESSAGES } from "@app/api-core";
5+
6+
@Controller()
7+
export class AppController {
8+
@MessagePattern({ cmd: NATS_MESSAGES.USERS.GET })
9+
getHello(): string {
10+
return "hello";
11+
}
12+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Module } from "@nestjs/common";
2+
3+
import { APICoreModule } from "@app/api-core";
4+
5+
import { AppController } from "./app.controller";
6+
7+
@Module({
8+
imports: [APICoreModule],
9+
controllers: [AppController],
10+
})
11+
export class AppModule {}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { ConfigService } from "@nestjs/config";
2+
import { NestFactory } from "@nestjs/core";
3+
import { MicroserviceOptions, Transport } from "@nestjs/microservices";
4+
5+
import { AppModule } from "./app.module";
6+
7+
async function bootstrap() {
8+
const appContext = await NestFactory.createApplicationContext(AppModule);
9+
const configService = appContext.get(ConfigService);
10+
const natsPort = configService.get<number>("app.NATS_PORT");
11+
const natsHost = configService.get<string>("app.NATS_HOST");
12+
13+
const app = await NestFactory.createMicroservice<MicroserviceOptions>(
14+
AppModule,
15+
{
16+
transport: Transport.NATS,
17+
options: {
18+
servers: [`nats://${natsHost}:${natsPort}`],
19+
},
20+
},
21+
);
22+
23+
await app.listen();
24+
}
25+
bootstrap();

0 commit comments

Comments
 (0)