Skip to content

Commit

Permalink
add question service
Browse files Browse the repository at this point in the history
  • Loading branch information
cyri113 committed Sep 16, 2024
1 parent 6da2cfa commit 8c4caf6
Show file tree
Hide file tree
Showing 14 changed files with 155 additions and 11 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ COPY . .
RUN npm run build bot
RUN npm run build event-store
RUN npm run build graph-store
RUN npm run build question-service

FROM node:alpine AS production

Expand Down
3 changes: 2 additions & 1 deletion apps/bot/src/bot.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import { Services } from '@app/common';
}),
RmqModule.register(Services.EventStore),
RmqModule.register(Services.GraphStore),
RmqModule.register(Services.TGQuestionService),
],
providers: [BotService],
})
export class BotModule {}
export class BotModule { }
3 changes: 3 additions & 0 deletions apps/bot/src/bot.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export class BotService implements OnModuleInit {
constructor(
@Inject(Services.EventStore.name) private readonly eventClient: ClientProxy,
@Inject(Services.GraphStore.name) private readonly graphClient: ClientProxy,
@Inject(Services.TGQuestionService.name)
private readonly tgQuestionClient: ClientProxy,
private readonly configService: ConfigService,
) {
this.bot = new Bot(configService.get<string>('telegram.token'));
Expand All @@ -27,6 +29,7 @@ export class BotService implements OnModuleInit {
this.logger.log(`Received ${event} from ${ctx.chat.id}`);
this.eventClient.emit(event, ctx);
this.graphClient.emit(event, ctx);
this.tgQuestionClient.emit(event, ctx);
});
});
}
Expand Down
13 changes: 13 additions & 0 deletions apps/question-service/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { NestFactory } from '@nestjs/core';
import { QuestionServiceModule } from './question-service.module';
import { RmqService, Services } from '@app/common';

async function bootstrap() {
const app = await NestFactory.create(QuestionServiceModule);
const rmqService = app.get<RmqService>(RmqService);
app.connectMicroservice(
rmqService.getOptions(Services.TGQuestionService.queue),
);
await app.startAllMicroservices();
}
bootstrap();
24 changes: 24 additions & 0 deletions apps/question-service/src/question-service.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Test, TestingModule } from '@nestjs/testing';
import { QuestionServiceController } from './question-service.controller';
import { QuestionServiceService } from './question-service.service';

describe('QuestionServiceController', () => {
let questionServiceController: QuestionServiceController;

Check failure on line 6 in apps/question-service/src/question-service.controller.spec.ts

View workflow job for this annotation

GitHub Actions / lint

'questionServiceController' is assigned a value but never used

beforeEach(async () => {
const app: TestingModule = await Test.createTestingModule({
controllers: [QuestionServiceController],
providers: [QuestionServiceService],
}).compile();

questionServiceController = app.get<QuestionServiceController>(
QuestionServiceController,
);
});

// describe('root', () => {
// it('should return "Hello World!"', () => {
// expect(questionServiceController.getHello()).toBe('Hello World!');
// });
// });
});
19 changes: 19 additions & 0 deletions apps/question-service/src/question-service.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Controller, Logger } from '@nestjs/common';
import { QuestionServiceService } from './question-service.service';
import { MessagePattern, Payload } from '@nestjs/microservices';
import { UpdateEvent } from '@app/common';

@Controller()
export class QuestionServiceController {
private readonly logger = new Logger(QuestionServiceController.name);
constructor(
private readonly questionServiceService: QuestionServiceService,
) { }

@MessagePattern(UpdateEvent.MESSAGE)
async message(@Payload() data): Promise<void> {
const { update } = data;
const { message } = update;
this.logger.log('Message received', message);
}
}
19 changes: 19 additions & 0 deletions apps/question-service/src/question-service.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Module } from '@nestjs/common';
import { QuestionServiceController } from './question-service.controller';
import { QuestionServiceService } from './question-service.service';
import { schemaConfig, rmqConfig, mongoConfig, RmqModule } from '@app/common';
import { ConfigModule } from '@nestjs/config';

@Module({
imports: [
ConfigModule.forRoot({
validationSchema: schemaConfig,
load: [rmqConfig, mongoConfig],
isGlobal: true,
}),
RmqModule,
],
controllers: [QuestionServiceController],
providers: [QuestionServiceService],
})
export class QuestionServiceModule { }
8 changes: 8 additions & 0 deletions apps/question-service/src/question-service.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Injectable } from '@nestjs/common';

@Injectable()
export class QuestionServiceService {
getHello(): string {
return 'Hello World!';
}
}
24 changes: 24 additions & 0 deletions apps/question-service/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 { QuestionServiceModule } from './../src/question-service.module';

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

beforeEach(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [QuestionServiceModule],
}).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 apps/question-service/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 apps/question-service/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/question-service"
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist", "test", "**/*spec.ts"]
}
21 changes: 11 additions & 10 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
version: '3.9'

x-telegram-common:
&telegram-common
x-telegram-common: &telegram-common
build:
context: .
dockerfile: Dockerfile
target: production
target: development
restart: unless-stopped
env_file:
- ./.env
Expand Down Expand Up @@ -69,27 +66,31 @@ services:
- NEO4J_dbms_security_procedures_unrestricted=apoc.*,gds.*
- NEO4J_AUTH=neo4j/password
healthcheck:
test: ["CMD" ,"wget", "http://localhost:7474"]
test: [ "CMD", "wget", "http://localhost:7474" ]
interval: 1m30s
timeout: 10s
retries: 2
start_period: 40s

telegram-bot:
<<: [*telegram-common]
<<: [ *telegram-common ]
container_name: telegram-bot
command: npm run start bot
telegram-events:
<<: [*telegram-common]
<<: [ *telegram-common ]
container_name: telegram-events
command: npm run start event-store
telegram-graph:
<<: [*telegram-common]
<<: [ *telegram-common ]
container_name: telegram-graph
command: npm run start graph-store
telegram-question:
<<: [ *telegram-common ]
container_name: telegram-question
command: npm run start question-service

volumes:
mongo_data:
neo4j_data:
neo4j_import:
neo4j_plugins:
neo4j_plugins:
4 changes: 4 additions & 0 deletions libs/common/src/constants/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ export const Services = {
name: 'graph_store_service',
queue: 'graph_store_queue',
},
TGQuestionService: {
name: 'tg_question_service',
queue: 'tg_question_queue',
},
};
9 changes: 9 additions & 0 deletions nest-cli.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@
"compilerOptions": {
"tsConfigPath": "apps/graph-store/tsconfig.app.json"
}
},
"question-service": {
"type": "application",
"root": "apps/question-service",
"entryFile": "main",
"sourceRoot": "apps/question-service/src",
"compilerOptions": {
"tsConfigPath": "apps/question-service/tsconfig.app.json"
}
}
}
}

0 comments on commit 8c4caf6

Please sign in to comment.