Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#28] Access logging on HTTP server #32

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/config/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ http:
host: '127.0.0.1'
port: 8080
logger:
level: 'info' # Log only if less than or equal to this level
level: 'http' # Log only if less than or equal to this level
format: 'text'
16 changes: 16 additions & 0 deletions api/package-lock.json

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

2 changes: 2 additions & 0 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"@types/jest": "^29.5.1",
"@types/js-yaml": "^4.0.5",
"@types/node": "^18.15.12",
"@types/request-ip": "^0.0.37",
"@typescript-eslint/eslint-plugin": "^5.58.0",
"@typescript-eslint/parser": "^5.58.0",
"eslint": "^8.38.0",
Expand All @@ -40,6 +41,7 @@
"config": "^3.3.9",
"express": "^4.18.2",
"js-yaml": "^4.1.0",
"request-ip": "^3.3.0",
"rimraf": "^5.0.0",
"tsc-alias": "^1.8.5",
"tsconfig-paths": "^4.2.0",
Expand Down
28 changes: 28 additions & 0 deletions api/src/controller/http/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { NextFunction, Request, Response } from 'express';
import { Logger } from 'winston';

export class Middleware {
constructor(public logger: Logger) {}

public accessLog = (req: Request, res: Response, next: NextFunction) => {
const start = new Date().getTime();
res.on('finish', () => {
const end = new Date().getTime();
this.logger.http('Access log', {
method: req.method,
url: req.originalUrl,
addr: req.clientIp,
proto: `${req.protocol}/${req.httpVersion}`,
contentLength: req.headers['content-length'],
userAgent: req.headers['user-agent'],
statusCode: res.statusCode,
bodyBytes: res.getHeader('Content-Length'),
elapsedMs: end - start,
contentType: res.getHeader('Content-Type')?.toString().split(';')[0],
error: res.locals.error?.message
});
});

next();
};
}
13 changes: 10 additions & 3 deletions api/src/controller/http/server.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
import express from 'express';
import http from 'http';
import requestIp from 'request-ip';
import { Logger } from 'winston';

import { HttpConfig } from '@src/controller/http/types';

import { DevV1Controller } from '@controller/http/dev/dev.v1.controller';
import { Middleware } from '@controller/http/middleware';
import { HttpConfig } from '@controller/http/types';

export class HttpServer {
middleware: Middleware;
server!: http.Server;

constructor(
private readonly logger: Logger,
private readonly config: HttpConfig
) {}
) {
this.middleware = new Middleware(this.logger);
}

public start = (): Promise<void> => {
return new Promise((resolve) => {
const app = express();
app.use(requestIp.mw());
app.use(this.middleware.accessLog);
app.use('/api', this.getRouters());

this.server = app.listen(this.config.port, () => {
this.logger.info(`HTTP server started on ${this.config.port}`);
resolve();
Expand Down
4 changes: 2 additions & 2 deletions api/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ async function main() {
const logger = loggerInitialize(buildLoggerConfig(config));
const httpServer = new HttpServer(logger, buildHttpConfig(config));
await httpServer.start();
await httpServer.close();
logger.info('Server shutdowned');
// await httpServer.close();
// logger.info('Server shutdowned');
}

if (require.main === module) {
Expand Down