Skip to content

Commit

Permalink
feat: init webhooks app
Browse files Browse the repository at this point in the history
  • Loading branch information
TomKortyka committed May 8, 2024
1 parent 310eb12 commit 010d6ee
Show file tree
Hide file tree
Showing 18 changed files with 334 additions and 4 deletions.
18 changes: 18 additions & 0 deletions apps/azkaban-webhooks/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
}
]
}
1 change: 1 addition & 0 deletions apps/azkaban-webhooks/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
8 changes: 4 additions & 4 deletions apps/azkaban-webhooks/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ ENV PORT=3000

WORKDIR /app

RUN addgroup --system azkaban-gateway && \
adduser --system -G azkaban-gateway azkaban-gateway
RUN addgroup --system azkaban-webhooks && \
adduser --system -G azkaban-webhooks azkaban-webhooks

COPY dist/apps/azkaban-gateway .
RUN chown -R azkaban-gateway:azkaban-gateway .
COPY dist/apps/azkaban-webhooks .
RUN chown -R azkaban-webhooks:azkaban-webhooks .

RUN npm i --omit=dev --omit=optional
RUN npm i --save @toxictoast/azkaban-base-types
Expand Down
11 changes: 11 additions & 0 deletions apps/azkaban-webhooks/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* eslint-disable */
export default {
displayName: 'azkaban-webhooks',
preset: '../../jest.preset.js',
testEnvironment: 'node',
transform: {
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../coverage/apps/azkaban-webhooks',
};
38 changes: 38 additions & 0 deletions apps/azkaban-webhooks/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "azkaban-webhooks",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "apps/azkaban-webhooks/src",
"projectType": "application",
"tags": [],
"targets": {
"serve": {
"executor": "@nx/js:node",
"defaultConfiguration": "development",
"options": {
"buildTarget": "azkaban-webhooks:build"
},
"configurations": {
"development": {
"buildTarget": "azkaban-webhooks:build:development"
},
"production": {
"buildTarget": "azkaban-webhooks:build:production"
}
}
},
"test": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"options": {
"jestConfig": "apps/azkaban-webhooks/jest.config.ts"
}
},
"docker-build": {
"dependsOn": ["build"],
"command": "docker build -f apps/azkaban-webhooks/Dockerfile . -t azkaban2015/webhooks:{args.VERSION}"
},
"docker-push": {
"command": "docker push azkaban2015/webhooks:{args.VERSION}"
}
}
}
28 changes: 28 additions & 0 deletions apps/azkaban-webhooks/src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Module } from '@nestjs/common';
import { HealthModule } from './health/health.module';
import { MetricsModule } from './metrics/metrics.module';
import { VersionModule } from './version/version.module';
import { RouterModule } from '@nestjs/core';

@Module({
imports: [
HealthModule,
MetricsModule,
VersionModule,
RouterModule.register([
{
path: 'health',
module: HealthModule,
},
{
path: 'metrics',
module: MetricsModule,
},
{
path: 'version',
module: VersionModule,
},
]),
],
})
export class AppModule {}
40 changes: 40 additions & 0 deletions apps/azkaban-webhooks/src/app/health/health.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Controller, Get, Inject } from '@nestjs/common';
import {
HealthCheck,
HealthCheckService,
MemoryHealthIndicator,
MicroserviceHealthIndicator,
TypeOrmHealthIndicator,
} from '@nestjs/terminus';
import { Transport } from '@nestjs/microservices';

@Controller()
export class HealthController {
constructor(
@Inject('MEMORY_HEAP_TRESHOLD') private readonly heapTreshold: number,
@Inject('MEMORY_RSS_TRESHOLD') private readonly rssTreshold: number,
@Inject('BROKER_CONNECTION_STRING')
private readonly brokerConnectionString: string,
private readonly service: HealthCheckService,
private readonly memory: MemoryHealthIndicator,
private readonly microservices: MicroserviceHealthIndicator,
private readonly database: TypeOrmHealthIndicator
) {}

@Get()
@HealthCheck()
check() {
return this.service.check([
() => this.memory.checkHeap('memory_heap', this.heapTreshold),
() => this.memory.checkRSS('memory_rss', this.rssTreshold),
() =>
this.microservices.pingCheck('rabbitmq', {
transport: Transport.RMQ,
options: {
urls: [this.brokerConnectionString],
},
}),
() => this.database.pingCheck('postgres'),
]);
}
}
27 changes: 27 additions & 0 deletions apps/azkaban-webhooks/src/app/health/health.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Module } from '@nestjs/common';
import { TerminusModule } from '@nestjs/terminus';
import { HealthController } from './health.controller';

@Module({
imports: [
TerminusModule.forRoot({
errorLogStyle: 'pretty',
}),
],
controllers: [HealthController],
providers: [
{
provide: 'MEMORY_HEAP_TRESHOLD',
useValue: process.env.MEMORY_HEAP_TRESHOLD,
},
{
provide: 'MEMORY_RSS_TRESHOLD',
useValue: process.env.MEMORY_RSS_TRESHOLD,
},
{
provide: 'BROKER_CONNECTION_STRING',
useValue: `amqp://${process.env.BROKER_USERNAME}:${process.env.BROKER_PASSWORD}@${process.env.BROKER_HOST}:${process.env.BROKER_PORT}`,
},
],
})
export class HealthModule {}
17 changes: 17 additions & 0 deletions apps/azkaban-webhooks/src/app/metrics/metrics.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Module } from '@nestjs/common';
import { PrometheusModule } from '@willsoto/nestjs-prometheus';

@Module({
imports: [
PrometheusModule.register({
defaultMetrics: {
enabled: true,
},
defaultLabels: {
app: 'azkaban-webhooks',
},
path: '/metrics',
}),
],
})
export class MetricsModule {}
12 changes: 12 additions & 0 deletions apps/azkaban-webhooks/src/app/version/version.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Controller, UseGuards } from '@nestjs/common';
import { ThrottlerGuard } from '@nestjs/throttler';
import { VersionService } from './version.service';

@Controller()
export class VersionController {
constructor(private readonly service: VersionService) {}

async getVersion() {
return this.service.getVersion();
}
}
15 changes: 15 additions & 0 deletions apps/azkaban-webhooks/src/app/version/version.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Module } from '@nestjs/common';
import { VersionController } from './version.controller';
import { VersionService } from './version.service';

@Module({
controllers: [VersionController],
providers: [
VersionService,
{
provide: 'APP_VERSION',
useValue: process.env.APP_VERSION,
},
],
})
export class VersionModule {}
10 changes: 10 additions & 0 deletions apps/azkaban-webhooks/src/app/version/version.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Inject, Injectable } from '@nestjs/common';

@Injectable()
export class VersionService {
constructor(@Inject('APP_VERSION') private readonly appVersion: string) {}

getVersion(): string {
return this.appVersion;
}
}
Empty file.
52 changes: 52 additions & 0 deletions apps/azkaban-webhooks/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { INestApplication, Logger } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { Transport } from '@nestjs/microservices';
import { AppModule } from './app/app.module';

async function createApp(): Promise<INestApplication> {
return await NestFactory.create(AppModule, {
cors: true,
snapshot: true,
rawBody: true,
});
}

async function createMicroservice(app: INestApplication): Promise<void> {
const queue = 'azkaban_webhook_queue';
const connectionString = `amqp://${process.env.BROKER_USERNAME}:${process.env.BROKER_PASSWORD}@${process.env.BROKER_HOST}:${process.env.BROKER_PORT}`;
const noAck = process.env.BROKER_ACK === 'yes' ? true : false;
//
app.connectMicroservice({
transport: Transport.RMQ,
options: {
queue: queue,
queueOptions: {
durable: true,
},
urls: [connectionString],
noAck: noAck,
},
});
}

function configureApp(app: INestApplication): void {
const globalPrefix = 'api';
app.setGlobalPrefix(globalPrefix);
app.enableShutdownHooks();
}

async function startApp(app: INestApplication): Promise<void> {
const port = process.env.PORT ?? 3000;
await app.listen(port);
Logger.log(`🚀 Listening on Port: ${port}`);
}

async function bootstrap() {
const app = await createApp();
configureApp(app);
await createMicroservice(app);
await startApp(app);
Logger.log(`🚀 Azkaban-Webhooks is running`);
Logger.log(`🚀 Version: ${process.env.APP_VERSION}`);
}
bootstrap().catch((err) => Logger.error(err));
12 changes: 12 additions & 0 deletions apps/azkaban-webhooks/tsconfig.app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"module": "commonjs",
"types": ["node"],
"emitDecoratorMetadata": true,
"target": "es2021"
},
"exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"],
"include": ["src/**/*.ts"]
}
16 changes: 16 additions & 0 deletions apps/azkaban-webhooks/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"extends": "../../tsconfig.base.json",
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.app.json"
},
{
"path": "./tsconfig.spec.json"
}
],
"compilerOptions": {
"esModuleInterop": true
}
}
14 changes: 14 additions & 0 deletions apps/azkaban-webhooks/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"
]
}
19 changes: 19 additions & 0 deletions apps/azkaban-webhooks/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const { NxAppWebpackPlugin } = require('@nx/webpack/app-plugin');
const { join } = require('path');

module.exports = {
output: {
path: join(__dirname, '../../dist/apps/azkaban-webhooks'),
},
plugins: [
new NxAppWebpackPlugin({
target: 'node',
compiler: 'tsc',
main: './src/main.ts',
tsConfig: './tsconfig.app.json',
assets: ['./src/assets'],
optimization: false,
outputHashing: 'none',
}),
],
};

0 comments on commit 010d6ee

Please sign in to comment.