Skip to content

Commit

Permalink
Merge pull request #14 from alkem-io/develop
Browse files Browse the repository at this point in the history
Release v0.1.2
  • Loading branch information
valentinyanakiev authored Jun 25, 2024
2 parents a678e86 + 2e95f35 commit aba1559
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 19 deletions.
5 changes: 4 additions & 1 deletion config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,7 @@ settings:
# authentication & authorization queue
auth_queue: ${AUTH_QUEUE}:alkemio-files
# MILLISECONDS wait time for a response after a request on the message queue
response_timeout: ${QUEUE_RESPONSE_TIMEOUT}:10000
response_timeout: ${QUEUE_RESPONSE_TIMEOUT}:10000
# TTL in seconds, how much time a document should be cached for
# The service tell browser clients to cache the resource via the 'Cache-Control' and 'Etag' headers
document_max_age: ${DOCUMENT_MAX_AGE}:86400
14 changes: 12 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "file-service",
"version": "0.1.1",
"version": "0.1.2",
"description": "A file serving microservice for the Alkemio platform",
"main": "main.ts",
"scripts": {
Expand Down Expand Up @@ -28,6 +28,8 @@
"homepage": "https://github.com/alkem-io/file-service#readme",
"dependencies": {
"@fastify/cookie": "^9.3.1",
"@fastify/cors": "^9.0.1",
"@fastify/etag": "^5.2.0",
"@fastify/helmet": "^11.1.1",
"@nestjs/common": "^10.3.8",
"@nestjs/config": "^3.2.2",
Expand Down
20 changes: 20 additions & 0 deletions src/config/config.type.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export interface ConfigType {
rabbitmq: {
/** # Connection in the form of 'amqp://[user]:[password]@[host]:[port]?heartbeat=[heartbeat]' */
connection: {
host: string;
port: number;
Expand All @@ -10,20 +11,39 @@ export interface ConfigType {
};
monitoring: {
logging: {
/** A flag setting whether Winston Console transport will be enabled.
If the flag is set to true logs of the appropriate level (see below) will be outputted to the console
after the application has been bootstrapped.
The NestJS bootstrap process is handled by the internal NestJS logging.
*/
enabled: boolean;
/** Logging level for outputs to console.
Valid values are log|error|warn|debug|verbose. */
level: string;
/** The logging format will be in json - useful for parsing
if disabled - will be in a human-readable form */
json: boolean;
};
};
settings: {
/** */
application: {
/** */
storage: {
/** Absolute path to the local storage of the files */
storage_path: string;
};
/** */
address: string;
/** The port on which the service is running */
port: number;
/** authentication & authorization queue */
auth_queue: string;
/** MILLISECONDS wait time for a response after a request on the message queue */
response_timeout: number;
/** TTL in seconds, how much time a document should be cached for
The service tell browser clients to cache the resource via the 'Cache-Control' and 'Etag' headers */
document_max_age: number;
};
};
}
18 changes: 7 additions & 11 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import {
FastifyAdapter,
NestFastifyApplication,
} from '@nestjs/platform-fastify';
import { ConfigService } from '@nestjs/config';
import helmet from '@fastify/helmet';
import fastifyCookie from '@fastify/cookie';
import { ConfigService } from '@nestjs/config';
import cors from '@fastify/cors';
import etag from '@fastify/etag';
import { AppModule } from './app.module';
import { ConfigType } from './config';

Expand All @@ -29,18 +31,12 @@ const isProd = process.env.NODE_ENV === 'production';
const logger = app.get(WINSTON_MODULE_NEST_PROVIDER);
app.useLogger(logger);

app.enableCors({
origin: '*',
allowedHeaders: [
'Origin,X-Requested-With',
'Content-Type,Accept',
'Authorization',
],
methods: ['GET', 'HEAD', 'PUT', 'PATCH', 'POST', 'DELETE'],
});

await app.register(fastifyCookie);
await app.register(helmet, { contentSecurityPolicy: false });
await app.register(cors, { origin: false });
// set the header manually, because this plugin skips generation for Stream (our case)
// it only supports String and Buffer
await app.register(etag);

const configService: ConfigService<ConfigType, true> = app.get(ConfigService);
const port = configService.get('settings.application.port', { infer: true });
Expand Down
17 changes: 13 additions & 4 deletions src/services/file-reader/file.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,27 @@ import {
Res,
StreamableFile,
} from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston';
import { FastifyReply } from 'fastify';
import { FileService } from './file.service';
import { DocumentData, FileInfoErrorCode } from './types';
import { FileInfoException } from './exceptions';
import { ConfigType } from '../../config';

@Controller('/rest/storage')
export class FileController {
private readonly documentMaxAge: number;
constructor(
private readonly fileService: FileService,
private readonly configService: ConfigService<ConfigType, true>,
@Inject(WINSTON_MODULE_NEST_PROVIDER) private logger: LoggerService,
) {}
) {
this.documentMaxAge = this.configService.get(
'settings.application.document_max_age',
{ infer: true },
);
}

@Get('document/:id')
public async file(
Expand All @@ -46,11 +55,11 @@ export class FileController {

res.headers({
'Content-Type': `${documentData.mimeType}`,
'Cache-Control': 'public, max-age=15552000',
'Cache-Control': `public, max-age=${this.documentMaxAge}`,
Pragma: 'public',
Expires: new Date(Date.now() + 15552000 * 1000).toUTCString(),
Expires: new Date(Date.now() + this.documentMaxAge * 1000).toUTCString(),
etag: id,
});

this.logger.verbose?.(`Serving document ${id}`);

return documentData.file;
Expand Down

0 comments on commit aba1559

Please sign in to comment.