Skip to content

Commit

Permalink
Remove compression and helmet for user
Browse files Browse the repository at this point in the history
  • Loading branch information
SangTran-127 committed Sep 1, 2024
1 parent 170fb92 commit 7f26f65
Show file tree
Hide file tree
Showing 15 changed files with 76 additions and 131 deletions.
23 changes: 14 additions & 9 deletions packages/kompact-cli/templates/project/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import { KompactApp } from 'kompact'
import { MainController, BookController } from './controllers'
import { KompactApp } from 'kompact';
import { MainController, BookController } from './controllers';


const PORT = 3002
const PORT = 3002;

async function main() {
const app = new KompactApp({
controllers: [MainController, BookController],
})

});

/*
// will be custom these param, don't hard code anymore
this.app.use(helmet()); // help secure Express apps by setting HTTP response headers.
this.app.use(compression());
*/

app.start(PORT, () => {
console.log(`running at ${PORT}`)
})
console.log(`running at ${PORT}`);
});
}

main()
main();
3 changes: 0 additions & 3 deletions packages/kompact-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
"name": "@kompact/core",
"version": "0.0.1",
"dependencies": {
"compression": "^1.7.4",
"express": "^4.19.2",
"helmet": "^7.1.0",
"jsonwebtoken": "^9.0.2",
"reflect-metadata": "^0.2.2",
"uuid": "^10.0.0",
"winston": "^3.13.1",
Expand Down
1 change: 0 additions & 1 deletion packages/kompact-core/src/auth/index.ts

This file was deleted.

50 changes: 0 additions & 50 deletions packages/kompact-core/src/auth/utils.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ export const ReasonStatusCode: { [key in StatusCode]: string } = {
[StatusCode.BAD_GATEWAY]: 'Bad Gateway',
[StatusCode.SERVICE_UNAVAILABLE]: 'Service Unavailable',
[StatusCode.GATEWAY_TIMEOUT]: 'Gateway Timeout',
}
};
Original file line number Diff line number Diff line change
@@ -1,55 +1,55 @@
import { ReasonStatusCode, StatusCode } from './constant'
import { ReasonStatusCode, StatusCode } from '../constant';

export class HttpError extends Error {
public status: number
public status: number;
// will be change number to an enum or object later
constructor(message: string, status: number) {
super(message)
this.status = status
super(message);
this.status = status;
}
}

export class ConflictRequestError extends HttpError {
constructor(
message = ReasonStatusCode[StatusCode.CONFLICT],
statusCode = StatusCode.CONFLICT,
statusCode = StatusCode.CONFLICT
) {
super(message, statusCode)
super(message, statusCode);
}
}

export class BadRequestError extends HttpError {
constructor(
message = ReasonStatusCode[StatusCode.BAD_REQUEST],
statusCode = StatusCode.BAD_REQUEST,
statusCode = StatusCode.BAD_REQUEST
) {
super(message, statusCode)
super(message, statusCode);
}
}

export class AuthFailureError extends HttpError {
constructor(
message = ReasonStatusCode[StatusCode.UNAUTHORIZED],
statusCode = StatusCode.UNAUTHORIZED,
statusCode = StatusCode.UNAUTHORIZED
) {
super(message, statusCode)
super(message, statusCode);
}
}

export class NotFoundError extends HttpError {
constructor(
message = ReasonStatusCode[StatusCode.NOT_FOUND],
statusCode = StatusCode.NOT_FOUND,
statusCode = StatusCode.NOT_FOUND
) {
super(message, statusCode)
super(message, statusCode);
}
}

export class ForbiddenError extends HttpError {
constructor(
message = ReasonStatusCode[StatusCode.FORBIDDEN],
statusCode = StatusCode.FORBIDDEN,
statusCode = StatusCode.FORBIDDEN
) {
super(message, statusCode)
super(message, statusCode);
}
}
3 changes: 2 additions & 1 deletion packages/kompact-core/src/core/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './response'
export * from './response';
export * from './http-error';
4 changes: 2 additions & 2 deletions packages/kompact-core/src/core/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class SuccessResponse {
this.metadata = metadata;
}

send(res: Response, header: Record<string, string> = {}): any {
return res.status(this.statusCode).json(this);
send(res: Response, header: Record<string, string> = {}): Response {
return res.header(header).status(this.statusCode).json(this);
}
}
1 change: 0 additions & 1 deletion packages/kompact-core/src/error/index.ts

This file was deleted.

6 changes: 6 additions & 0 deletions packages/kompact-core/src/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
declare namespace Express {
export interface Request {
requestId?: string | string[];
user?: object;
}
}
15 changes: 6 additions & 9 deletions packages/kompact-core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import 'reflect-metadata'
export * from './decorator'
export * from './app'
export * from './interface'
// export * from './database'
export * from './auth'
export * from './error'
export * from './utils'
export * from './core'
import 'reflect-metadata';
export * from './decorator';
export * from './kompact-application';
export * from './interface';
export * from './utils';
export * from './core';
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import compression from 'compression';
import express, { type NextFunction, type Router } from 'express';
import helmet from 'helmet';
import express, { json, Router, type NextFunction } from 'express';
import { v4 as uuidv4 } from 'uuid';
import { HttpError } from './core/http-error';
import {
CONTROLLER_AUTH_METADATA,
CONTROLLER_PATH_METADATA,
ROUTES_METHOD_METADATA,
Singleton,
} from './decorator';
import { HttpError } from './error';
import type {
Class,
Middleware,
Expand Down Expand Up @@ -47,7 +45,7 @@ export class KompactApp {
CONTROLLER_AUTH_METADATA,
Controller
);
const router = express.Router();
const router = Router();
if (auth) {
if (!authenticator) {
// TODO: will custom this problem later
Expand Down Expand Up @@ -93,10 +91,7 @@ export class KompactApp {

public start(port: number, callback?: () => void): void {
// init some utils middleware
this.app.use(express.json()); // json body parser
// will be custom these param, don't hard code anymore
this.app.use(helmet()); // help secure Express apps by setting HTTP response headers.
this.app.use(compression()); // compress file
this.app.use(json()); // json body parser
this.middlewares.forEach((middleware) => {
this.app.use(middleware);
});
Expand Down
50 changes: 25 additions & 25 deletions packages/kompact-core/src/logger/winston.logger.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
import {
createLogger,
format,
type Logger as WinstonLogger,
transports,
} from 'winston'
import 'winston-daily-rotate-file'
import { Singleton } from '../decorator'
type Logger as WinstonLogger,
} from 'winston';
import 'winston-daily-rotate-file';
import { Singleton } from '../decorator';

export type LogParams = {
context: string
requestId: string | string[]
metadata: any
}
context: string;
requestId: string | string[];
metadata: unknown;
};
@Singleton()
class Logger {
private readonly logger: WinstonLogger
private readonly logger: WinstonLogger;

constructor() {
const formatPrint = format.printf(
({ level, message, context, requestId, timestamp, metadata }) => {
return `${timestamp}::${level}::${context}::${requestId}::${message}::${JSON.stringify(
metadata,
)}`
},
)
metadata
)}`;
}
);
this.logger = createLogger({
level: 'debug',
format: format.combine(
format.timestamp({ format: 'DD-MM-YY HH:mm::ss' }),
format.timestamp({ format: 'DD-MM-YY HH:mm::ss' })
),
transports: [
new transports.DailyRotateFile({
Expand All @@ -39,7 +39,7 @@ class Logger {
maxFiles: '14d',
format: format.combine(
format.timestamp({ format: 'DD-MM-YY HH:mm::ss' }),
formatPrint,
formatPrint
),
level: 'info',
}),
Expand All @@ -52,12 +52,12 @@ class Logger {
maxFiles: '14d',
format: format.combine(
format.timestamp({ format: 'DD-MM-YY HH:mm::ss' }),
formatPrint,
formatPrint
),
level: 'error',
}),
],
})
});
}

private formatParams({ requestId, context, metadata }: LogParams): any {
Expand All @@ -66,25 +66,25 @@ class Logger {
requestId,
context,
metadata,
}
};
}

log(message: string, params: LogParams): void {
const paramLog = this.formatParams(params)
const paramLog = this.formatParams(params);
const logObject = {
message,
...paramLog,
}
this.logger.info(logObject)
};
this.logger.info(logObject);
}

error(message: string, params: LogParams): void {
const paramLog = this.formatParams(params)
const paramLog = this.formatParams(params);
const logObject = {
message,
...paramLog,
}
this.logger.error(logObject)
};
this.logger.error(logObject);
}
}
export const logger = new Logger()
export const logger = new Logger();
1 change: 1 addition & 0 deletions packages/kompact-core/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitOverride": true,
Expand Down
7 changes: 1 addition & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3591,7 +3591,7 @@ compressible@~2.0.16:
dependencies:
mime-db ">= 1.43.0 < 2"

[email protected], compression@^1.7.4:
[email protected]:
version "1.7.4"
resolved "https://registry.yarnpkg.com/compression/-/compression-1.7.4.tgz#95523eff170ca57c29a0ca41e6fe131f41e5bb8f"
integrity sha512-jaSIDzP9pZVS4ZfQ+TzvtiWhdpFhE2RDHz8QJkpX9SIpLq88VueF5jJw6t+6CUQcAoA6t+x89MLrWAqpfDE8iQ==
Expand Down Expand Up @@ -4955,11 +4955,6 @@ hasown@^2.0.0, hasown@^2.0.2:
dependencies:
function-bind "^1.1.2"

helmet@^7.1.0:
version "7.1.0"
resolved "https://registry.yarnpkg.com/helmet/-/helmet-7.1.0.tgz#287279e00f8a3763d5dccbaf1e5ee39b8c3784ca"
integrity sha512-g+HZqgfbpXdCkme/Cd/mZkV0aV3BZZZSugecH03kl38m/Kmdx8jKjBikpDj2cr+Iynv4KpYEviojNdTJActJAg==

hosted-git-info@^7.0.0:
version "7.0.2"
resolved "https://registry.yarnpkg.com/hosted-git-info/-/hosted-git-info-7.0.2.tgz#9b751acac097757667f30114607ef7b661ff4f17"
Expand Down

0 comments on commit 7f26f65

Please sign in to comment.