mirror of
https://github.com/directus/directus.git
synced 2026-04-25 03:00:53 -04:00
Cleanup server file a bit
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
|
||||
import * as http from 'http';
|
||||
import * as https from 'https';
|
||||
import qs from 'qs';
|
||||
@@ -9,38 +8,38 @@ import logger from './logger';
|
||||
import emitter from './emitter';
|
||||
import database from './database';
|
||||
import app from './app';
|
||||
import { once } from 'lodash';
|
||||
|
||||
const server = http.createServer(app);
|
||||
|
||||
server.on('request', function(req: http.IncomingMessage & Request, res: http.ServerResponse) {
|
||||
|
||||
server.on('request', function (req: http.IncomingMessage & Request, res: http.ServerResponse) {
|
||||
const startTime = process.hrtime();
|
||||
|
||||
const complete = once(function(finished: boolean) {
|
||||
const complete = once(function (finished: boolean) {
|
||||
const elapsedTime = process.hrtime(startTime);
|
||||
const elapsedNanoseconds = (elapsedTime[0] * 1e9) + elapsedTime[1];
|
||||
const elapsedNanoseconds = elapsedTime[0] * 1e9 + elapsedTime[1];
|
||||
const elapsedMilliseconds = elapsedNanoseconds / 1e6;
|
||||
|
||||
const previousIn = req.connection._metrics?.in || 0;
|
||||
const previousOut = req.connection._metrics?.out || 0;
|
||||
const previousIn = (req.connection as any)._metrics?.in || 0;
|
||||
const previousOut = (req.connection as any)._metrics?.out || 0;
|
||||
|
||||
const metrics = {
|
||||
in: req.connection.bytesRead - previousIn,
|
||||
out: req.connection.bytesWritten - previousOut,
|
||||
};
|
||||
|
||||
req.connection._metrics = {
|
||||
(req.connection as any)._metrics = {
|
||||
in: req.connection.bytesRead,
|
||||
out: req.connection.bytesWritten,
|
||||
};
|
||||
|
||||
// Compatibility when supporting serving with certificates
|
||||
let protocol = 'http';
|
||||
if (server instanceof https.Server) {
|
||||
protocol = 'https';
|
||||
}
|
||||
const protocol = server instanceof https.Server ? 'https' : 'http';
|
||||
|
||||
const url = new URL((req.originalUrl || req.url) as string, `${protocol}://${req.headers.host}`);
|
||||
const url = new URL(
|
||||
(req.originalUrl || req.url) as string,
|
||||
`${protocol}://${req.headers.host}`
|
||||
);
|
||||
const query = url.search.startsWith('?') ? url.search.substr(1) : url.search;
|
||||
|
||||
const info = {
|
||||
@@ -62,7 +61,10 @@ server.on('request', function(req: http.IncomingMessage & Request, res: http.Ser
|
||||
size: metrics.out,
|
||||
headers: res.getHeaders(),
|
||||
},
|
||||
ip: req.headers['x-forwarded-for'] || req.connection?.remoteAddress || req.socket?.remoteAddress,
|
||||
ip:
|
||||
req.headers['x-forwarded-for'] ||
|
||||
req.connection?.remoteAddress ||
|
||||
req.socket?.remoteAddress,
|
||||
duration: elapsedMilliseconds.toFixed(),
|
||||
};
|
||||
|
||||
@@ -85,17 +87,6 @@ createTerminus(server, terminusOptions);
|
||||
|
||||
export default server;
|
||||
|
||||
function once(callback: Function): Function {
|
||||
let triggered = false;
|
||||
return function() {
|
||||
if (triggered) {
|
||||
return;
|
||||
}
|
||||
triggered = true;
|
||||
callback(...arguments);
|
||||
};
|
||||
}
|
||||
|
||||
async function beforeShutdown() {
|
||||
await emitter.emitAsync('server.stop.before', { server });
|
||||
|
||||
|
||||
9
api/src/shims.d.ts
vendored
9
api/src/shims.d.ts
vendored
@@ -1,9 +0,0 @@
|
||||
|
||||
declare module "http" {
|
||||
}
|
||||
|
||||
declare module "net" {
|
||||
interface Socket {
|
||||
_metrics: any;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user