From 149eefc6ed3a4ca9f20f820c481c65f8642786af Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Mon, 4 Nov 2024 16:15:04 +0100 Subject: [PATCH] logger, port --- api-node/package.json | 3 +++ api-node/src/app.module.ts | 10 ++++++++-- api-node/src/common/port.ts | 12 ++++++++++++ api-node/src/common/registry.service.ts | 20 +++++++++++--------- api-node/src/main.ts | 14 +------------- 5 files changed, 35 insertions(+), 24 deletions(-) create mode 100644 api-node/src/common/port.ts diff --git a/api-node/package.json b/api-node/package.json index e6d4ebd77..7d6b97fe3 100644 --- a/api-node/package.json +++ b/api-node/package.json @@ -27,6 +27,9 @@ "python-api:build": "docker build -t registry-api-server ../", "python-api:start": "docker start $(docker ps -aq --filter ancestor=registry-api-server) && echo 'Flask API started successfully' || (docker run -d -p 5031:5030 registry-api-server && echo 'Flask API started successfully')", "python-api:stop": "docker stop $(docker ps -q --filter ancestor=registry-api-server)", + "docker:build": "docker build -t registry-api-nestjs-server -f Dockerfile.nestjs .", + "docker:stop": "docker stop $(docker ps -q --filter ancestor=registry-api-nestjs-server)", + "docker:run": "docker run -p 5032:5030 registry-api-nestjs-server", "start:all": "yarn build && yarn start:dev & yarn python-api:start", "sentry:sourcemaps": "sentry-cli sourcemaps inject --org sentry --project release-registry-nestjs ./dist && sentry-cli sourcemaps upload --org sentry --project release-registry-nestjs ./dist" }, diff --git a/api-node/src/app.module.ts b/api-node/src/app.module.ts index de24e5c1c..dd659bcd4 100644 --- a/api-node/src/app.module.ts +++ b/api-node/src/app.module.ts @@ -1,4 +1,4 @@ -import { Module, Provider } from '@nestjs/common'; +import { Logger, Module, Provider } from '@nestjs/common'; import { PackagesController } from './packages/packages.controller'; import { HealthCheckController } from './health/healthCheck.controller'; import { MarketingController } from './marketing/marketing.controller'; @@ -12,6 +12,7 @@ import { CacheModule } from '@nestjs/cache-manager'; import { CACHE_DEFAULT_SETTINGS } from './common/cache'; import { AppVersionInterceptor } from './apps/appVersion.interceptor'; import { HttpClientExceptionFilter } from './common/httpClient.exceptionFilter'; +import { getPort } from './common/port'; const providers: Provider[] = [ RegistryService, @@ -38,4 +39,9 @@ const providers: Provider[] = [ ], providers, }) -export class AppModule {} +export class AppModule { + private readonly logger = new Logger(AppModule.name); + constructor() { + this.logger.log(`Server is running on port ${getPort()}`); + } +} diff --git a/api-node/src/common/port.ts b/api-node/src/common/port.ts new file mode 100644 index 000000000..557fe3459 --- /dev/null +++ b/api-node/src/common/port.ts @@ -0,0 +1,12 @@ +const DEFAULT_PORT = 3000; + +export function getPort(): number { + try { + if (process.env.PORT) { + return parseInt(process.env.PORT); + } + } catch (error) { + console.error('Invalid port number', error); + } + return DEFAULT_PORT; +} diff --git a/api-node/src/common/registry.service.ts b/api-node/src/common/registry.service.ts index 4f631e2b3..c52dc1a50 100644 --- a/api-node/src/common/registry.service.ts +++ b/api-node/src/common/registry.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@nestjs/common'; +import { Injectable, Logger } from '@nestjs/common'; import * as path from 'path'; import * as fs from 'fs'; import type { SdkEntry, Sdks, SdkVersions } from '../sdks/types'; @@ -24,6 +24,8 @@ const NAMESPACE_FILE_MARKER = '__NAMESPACE__'; @Injectable() export class RegistryService { + private readonly logger = new Logger(RegistryService.name); + // SDKs getSdks(strict: boolean = false): Sdks { const sdks: Sdks = {}; @@ -50,7 +52,7 @@ export class RegistryService { } } } catch (error) { - console.error('Error reading SDKs directory:', error); + this.logger.error('Error reading SDKs directory:', error); } return sdks; } @@ -61,7 +63,7 @@ export class RegistryService { const { canonical } = JSON.parse(fs.readFileSync(sdkFilePath, 'utf8')); return this.getPackage(canonical, version); } catch (error) { - console.error('Error reading SDK file:', error); + this.logger.error('Error reading SDK file:', error); } } @@ -105,8 +107,8 @@ export class RegistryService { return semver.parse(a).compare(semver.parse(b)); }); } catch (e) { - console.error(`Failed to read package versions: ${packageName}`); - console.error(e); + this.logger.error(`Failed to read package versions: ${packageName}`); + this.logger.error(e); return []; } } @@ -120,10 +122,10 @@ export class RegistryService { const versionFilePath = path.join(packageDir, `${version}.json`); return JSON.parse(fs.readFileSync(versionFilePath).toString()); } catch (e) { - console.error( + this.logger.error( `Failed to read package ${packageName} for version ${version}`, ); - console.error(e); + this.logger.error(e); return null; } } @@ -146,7 +148,7 @@ export class RegistryService { }, {}); } catch (error) { // Handle error (e.g., log it or throw a custom exception) - console.error('Error reading apps directory:', error); + this.logger.error('Error reading apps directory:', error); } } @@ -217,7 +219,7 @@ export class RegistryService { } } } catch (error) { - console.error('Error reading AWS Lambda Layers directory:', error); + this.logger.error('Error reading AWS Lambda Layers directory:', error); } return layers; diff --git a/api-node/src/main.ts b/api-node/src/main.ts index 62c9b063b..b1d9f2d81 100644 --- a/api-node/src/main.ts +++ b/api-node/src/main.ts @@ -3,8 +3,7 @@ import './instrument'; import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; import { AlignHeadersInterceptor } from './common/alignHeaders.interceptor'; - -const DEFAULT_PORT = 3000; +import { getPort } from './common/port'; async function bootstrap(): Promise { const app = await NestFactory.create(AppModule, { @@ -34,17 +33,6 @@ async function bootstrap(): Promise { await app.listen(getPort()); } -function getPort(): number { - try { - if (process.env.PORT) { - return parseInt(process.env.PORT); - } - } catch (error) { - console.error('Invalid port number', error); - } - return DEFAULT_PORT; -} - if (process.argv.includes('--smoke')) { console.log('Smoke test successful!'); } else {