Skip to content

Commit

Permalink
feat: add some basic logging
Browse files Browse the repository at this point in the history
  • Loading branch information
pkarolyi committed May 4, 2024
1 parent c4e6438 commit 8376c9a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 4 deletions.
9 changes: 9 additions & 0 deletions src/artifacts/artifacts.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Get,
Head,
HttpCode,
Logger,
NotFoundException,
Param,
Post,
Expand All @@ -18,10 +19,13 @@ import { ArtifactQueryTeamPipe } from "./artifacts.pipe";

@Controller({ path: "artifacts", version: "8" })
export class ArtifactsController {
private readonly logger = new Logger(ArtifactsController.name);

constructor(private readonly storageService: StorageService) {}

@Get("status")
getStatus(): StatusRO {
this.logger.log("GET /status");
return { status: "enabled" };
}

Expand All @@ -30,6 +34,7 @@ export class ArtifactsController {
@Param("hash") hash: string,
@Query(new ArtifactQueryTeamPipe()) team: string,
): Promise<void> {
this.logger.log(`HEAD /${hash} team: ${team}`);
const exists = await this.storageService.exists(team, hash);
if (!exists) throw new NotFoundException("Artifact not found");
}
Expand All @@ -39,6 +44,7 @@ export class ArtifactsController {
@Param("hash") hash: string,
@Query(new ArtifactQueryTeamPipe()) team: string,
): Promise<GetArtifactRO> {
this.logger.log(`GET /${hash} team: ${team}`);
const exists = await this.storageService.exists(team, hash);
if (!exists) throw new NotFoundException("Artifact not found");

Expand All @@ -52,20 +58,23 @@ export class ArtifactsController {
@Query(new ArtifactQueryTeamPipe()) team: string,
@Body() body: Buffer,
): Promise<PutArtifactRO> {
this.logger.log(`PUT /${hash} team: ${team} body.length: ${body.length}`);
await this.storageService.write(team, hash, Readable.from(body));
return { urls: [`${team}/${hash}`] };
}

@Post()
@HttpCode(501)
queryArtifact(): void {
this.logger.log(`POST /`);
// Documented in OpenAPI but currently unused
return;
}

@Post("events")
@HttpCode(200)
postEvents(): void {
this.logger.log(`POST /events`);
// We currently dont't record any events
return;
}
Expand Down
5 changes: 4 additions & 1 deletion src/artifacts/artifacts.pipe.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { BadRequestException, PipeTransform } from "@nestjs/common";
import { BadRequestException, Logger, PipeTransform } from "@nestjs/common";

export class ArtifactQueryTeamPipe implements PipeTransform<any, string> {
private readonly logger = new Logger(ArtifactQueryTeamPipe.name);

transform(value: any) {
this.logger.debug(`value: ${JSON.stringify(value)}`);
const team = value.teamId ?? value.slug; // sometimes slug is sent instead of teamId
if (!team) throw new BadRequestException("Missing teamId or slug");
return team;
Expand Down
7 changes: 6 additions & 1 deletion src/storage/providers/local.driver.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 { createReadStream, createWriteStream } from "fs";
import { access, mkdir } from "fs/promises";
import { join } from "path";
Expand All @@ -8,9 +8,12 @@ import { StorageDriver } from "../storage.interface";

@Injectable()
export class LocalStorageDriver implements StorageDriver {
private readonly logger = new Logger(LocalStorageDriver.name);

constructor(private readonly basePath: string) {}

async write(team: string, hash: string, contents: Readable): Promise<void> {
this.logger.debug(`write path: ${join(this.basePath, team, hash)}`);
await mkdir(join(this.basePath, team), { recursive: true });
await pipeline(
contents,
Expand All @@ -19,10 +22,12 @@ export class LocalStorageDriver implements StorageDriver {
}

async read(team: string, hash: string): Promise<Readable> {
this.logger.debug(`read path: ${join(this.basePath, team, hash)}`);
return createReadStream(join(this.basePath, team, hash));
}

async exists(team: string, hash: string): Promise<boolean> {
this.logger.debug(`exists path: ${join(this.basePath, team, hash)}`);
return access(join(this.basePath, team, hash))
.then(() => true)
.catch(() => false);
Expand Down
7 changes: 6 additions & 1 deletion src/storage/providers/s3.driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import {
S3Client,
} from "@aws-sdk/client-s3";
import { Upload } from "@aws-sdk/lib-storage";
import { Injectable } from "@nestjs/common";
import { Injectable, Logger } from "@nestjs/common";
import { Readable } from "stream";
import { StorageDriver } from "../storage.interface";

@Injectable()
export class S3StorageDriver implements StorageDriver {
private readonly logger = new Logger(S3StorageDriver.name);

constructor(
private readonly bucket: string,
private readonly s3Client: S3Client,
Expand All @@ -21,6 +23,7 @@ export class S3StorageDriver implements StorageDriver {
Bucket: this.bucket,
Key: `${team}/${hash}`,
};
this.logger.debug(`write params: ${JSON.stringify(params)}`);
const upload = new Upload({ client: this.s3Client, params });
await upload.done();
}
Expand All @@ -30,6 +33,7 @@ export class S3StorageDriver implements StorageDriver {
Bucket: this.bucket,
Key: `${team}/${hash}`,
};
this.logger.debug(`read params: ${JSON.stringify(params)}`);
const getCommand = new GetObjectCommand(params);
const response = await this.s3Client.send(getCommand);
return response.Body as Readable;
Expand All @@ -40,6 +44,7 @@ export class S3StorageDriver implements StorageDriver {
Bucket: this.bucket,
Key: `${team}/${hash}`,
};
this.logger.debug(`exists params: ${JSON.stringify(params)}`);
const headCommand = new HeadObjectCommand(params);
try {
await this.s3Client.send(headCommand);
Expand Down
7 changes: 6 additions & 1 deletion src/storage/storage.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { S3Client } from "@aws-sdk/client-s3";
import { Injectable } from "@nestjs/common";
import { Injectable, Logger } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { ConfigurationSchema } from "src/config/configuration";
import { Readable } from "stream";
Expand All @@ -9,12 +9,14 @@ import { StorageDriver } from "./storage.interface";

@Injectable()
export class StorageService {
private readonly logger = new Logger(StorageService.name);
private readonly driver: StorageDriver;

constructor(
readonly configService: ConfigService<ConfigurationSchema, true>,
) {
const storageConfig = configService.get("storage", { infer: true });
this.logger.debug(`storageConfig: ${JSON.stringify(storageConfig)}`);

if (storageConfig.provider === "local") {
const { basePath } = storageConfig;
Expand All @@ -33,14 +35,17 @@ export class StorageService {
}

write(team: string, hash: string, contents: Readable): Promise<void> {
this.logger.debug(`write team: ${team} hash: ${hash}`);
return this.driver.write(team, hash, contents);
}

read(team: string, hash: string): Promise<Readable> {
this.logger.debug(`read team: ${team} hash: ${hash}`);
return this.driver.read(team, hash);
}

exists(team: string, hash: string): Promise<boolean> {
this.logger.debug(`exists team: ${team} hash: ${hash}`);
return this.driver.exists(team, hash);
}
}

0 comments on commit 8376c9a

Please sign in to comment.