Skip to content

Commit

Permalink
logger, port
Browse files Browse the repository at this point in the history
  • Loading branch information
Lms24 committed Nov 4, 2024
1 parent 64f1859 commit 149eefc
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 24 deletions.
3 changes: 3 additions & 0 deletions api-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
10 changes: 8 additions & 2 deletions api-node/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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,
Expand All @@ -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()}`);
}
}
12 changes: 12 additions & 0 deletions api-node/src/common/port.ts
Original file line number Diff line number Diff line change
@@ -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;
}
20 changes: 11 additions & 9 deletions api-node/src/common/registry.service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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 = {};
Expand All @@ -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;
}
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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 [];
}
}
Expand All @@ -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;
}
}
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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;
Expand Down
14 changes: 1 addition & 13 deletions api-node/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
const app = await NestFactory.create(AppModule, {
Expand Down Expand Up @@ -34,17 +33,6 @@ async function bootstrap(): Promise<void> {
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 {
Expand Down

0 comments on commit 149eefc

Please sign in to comment.