Skip to content

Commit 8fc0dd7

Browse files
authored
refactor(webui): Complete migration to new Fastify Architecture by removing log-viewer-webui Fastify app. (#1106)
1 parent c1ef21b commit 8fc0dd7

File tree

32 files changed

+138
-178
lines changed

32 files changed

+138
-178
lines changed

components/webui/server/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
"scripts": {
77
"build": "tsc",
88
"build:watch": "npm run build -- -w",
9-
"dev:start": "fastify start --ignore-watch=.ts$ -w -l info -P dist/server/src/fastify-v2/app.js",
9+
"dev:start": "fastify start --ignore-watch=.ts$ -w -l info -P dist/server/src/app.js",
1010
"dev": "npm run build && concurrently -k -p \"[{name}]\" -n \"TypeScript,App\" -c \"yellow.bold,cyan.bold\" \"npm:build:watch\" \"npm:dev:start\"",
11-
"start": "npm run build && fastify start -l info dist/server/src/fastify-v2/app.js",
11+
"start": "npm run build && fastify start -l info dist/server/src/app.js",
1212
"standalone": "npm run build && node --env-file=.env dist/server/src/main.js",
1313
"lint:check": "eslint . --max-warnings 0",
1414
"lint:fix": "npm run lint:check -- --fix",
15-
"test": "NODE_ENV=test tap --include='**/*test.ts'"
15+
"test": "tap --include='**/*test.ts'"
1616
},
1717
"author": "YScope Inc. <dev@yscope.com>",
1818
"license": "Apache-2.0",

components/webui/server/src/app.ts

Lines changed: 96 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,106 @@
1+
// Reference: https://github.com/fastify/demo/blob/main/src/app.ts
2+
3+
import path from "node:path";
4+
5+
import {fastifyAutoload} from "@fastify/autoload";
16
import {
27
FastifyInstance,
3-
FastifyPluginAsync,
8+
FastifyPluginOptions,
49
} from "fastify";
10+
import {StatusCodes} from "http-status-codes";
511

6-
import staticRoutes from "./routes/static.js";
712

13+
const RATE_LIMIT_MAX_REQUESTS = 3;
14+
const RATE_LIMIT_TIME_WINDOW_MS = 500;
815

916
/**
10-
* Creates the Fastify app with the given options.
11-
*
12-
* TODO: Once old webui code is refactored to new modlular fastify style, this plugin should be
13-
* removed.
17+
* Registers all plugins and routes.
1418
*
1519
* @param fastify
16-
* @return
20+
* @param opts
1721
*/
18-
const FastifyV1App: FastifyPluginAsync = async (
19-
fastify: FastifyInstance
20-
) => {
21-
// Register the routes
22-
await fastify.register(staticRoutes);
23-
};
24-
25-
export default FastifyV1App;
22+
// eslint-disable-next-line max-lines-per-function
23+
export default async function serviceApp (
24+
fastify: FastifyInstance,
25+
opts: FastifyPluginOptions
26+
) {
27+
// Option only serves testing purpose. It's used in testing to expose all decorators to the
28+
// test app. Some decorators may not be exposed in production.
29+
delete opts.skipOverride;
30+
31+
// Loads all external plugins. Registered first as application plugins might depend on them.
32+
await fastify.register(fastifyAutoload, {
33+
dir: path.join(import.meta.dirname, "plugins/external"),
34+
options: {...opts},
35+
});
36+
37+
// Loads all application plugins.
38+
fastify.register(fastifyAutoload, {
39+
dir: path.join(import.meta.dirname, "plugins/app"),
40+
options: {...opts},
41+
});
42+
43+
// Loads all routes.
44+
fastify.register(fastifyAutoload, {
45+
autoHooks: true,
46+
cascadeHooks: true,
47+
dir: path.join(import.meta.dirname, "routes"),
48+
options: {...opts},
49+
});
50+
51+
fastify.setErrorHandler((err, request, reply) => {
52+
fastify.log.error(
53+
{
54+
err: err,
55+
request: {
56+
method: request.method,
57+
url: request.url,
58+
query: request.query,
59+
params: request.params,
60+
},
61+
},
62+
"Unhandled error occurred"
63+
);
64+
65+
if ("undefined" !== typeof err.statusCode &&
66+
Number(StatusCodes.INTERNAL_SERVER_ERROR) > err.statusCode
67+
) {
68+
reply.code(err.statusCode);
69+
70+
return err.message;
71+
}
72+
73+
reply.internalServerError();
74+
75+
return {
76+
message: "Internal Server Error",
77+
};
78+
});
79+
80+
// An attacker could search for valid URLs if 404 error handling is not rate limited.
81+
fastify.setNotFoundHandler(
82+
{
83+
preHandler: fastify.rateLimit({
84+
max: RATE_LIMIT_MAX_REQUESTS,
85+
timeWindow: RATE_LIMIT_TIME_WINDOW_MS,
86+
}),
87+
},
88+
(request, reply) => {
89+
request.log.warn(
90+
{
91+
request: {
92+
method: request.method,
93+
url: request.url,
94+
query: request.query,
95+
params: request.params,
96+
},
97+
},
98+
"Resource not found"
99+
);
100+
101+
reply.notFound();
102+
103+
return {message: "Not Found"};
104+
}
105+
);
106+
}

components/webui/server/src/fastify-v2/app.ts

Lines changed: 0 additions & 113 deletions
This file was deleted.

components/webui/server/src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import closeWithGrace from "close-with-grace";
44
import fastify from "fastify";
55
import fp from "fastify-plugin";
66

7-
import serviceApp from "./fastify-v2/app.js";
7+
import serviceApp from "./app.js";
88

99

1010
const DEFAULT_FASTIFY_CLOSE_GRACE_DELAY = 500;
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ import {FastifyInstance} from "fastify";
66
import fp from "fastify-plugin";
77
import {ResultSetHeader} from "mysql2";
88

9-
import settings from "../../../../../settings.json" with {type: "json"};
9+
import settings from "../../../../settings.json" with {type: "json"};
1010
import {
1111
QUERY_JOB_STATUS,
1212
QUERY_JOB_STATUS_WAITING_STATES,
1313
QUERY_JOB_TYPE,
1414
QUERY_JOBS_TABLE_COLUMN_NAMES,
1515
QueryJob,
16-
} from "../../../../typings/query.js";
16+
} from "../../../typings/query.js";
1717
import {JOB_COMPLETION_STATUS_POLL_INTERVAL_MILLIS} from "./typings.js";
1818

1919

File renamed without changes.

components/webui/server/src/fastify-v2/plugins/app/S3Manager/index.ts renamed to components/webui/server/src/plugins/app/S3Manager/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import {
55
import {getSignedUrl} from "@aws-sdk/s3-request-presigner";
66
import fp from "fastify-plugin";
77

8-
import settings from "../../../../../settings.json" with {type: "json"};
9-
import {Nullable} from "../../../../typings/common.js";
8+
import settings from "../../../../settings.json" with {type: "json"};
9+
import {Nullable} from "../../../typings/common.js";
1010
import {PRE_SIGNED_URL_EXPIRY_TIME_SECONDS} from "./typings.js";
1111

1212

File renamed without changes.

components/webui/server/src/fastify-v2/plugins/app/StreamFileManager.ts renamed to components/webui/server/src/plugins/app/StreamFileManager.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import {
44
} from "fastify";
55
import fp from "fastify-plugin";
66

7-
import settings from "../../../../settings.json" with {type: "json"};
8-
import {Nullable} from "../../../typings/common.js";
9-
import {QUERY_JOB_TYPE} from "../../../typings/query.js";
7+
import settings from "../../../settings.json" with {type: "json"};
8+
import {Nullable} from "../../typings/common.js";
9+
import {QUERY_JOB_TYPE} from "../../typings/query.js";
1010
import {
1111
StreamFileMetadata,
1212
StreamFilesCollection,
13-
} from "../../../typings/stream-files.js";
13+
} from "../../typings/stream-files.js";
1414

1515

1616
/**
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type {
44
Db,
55
} from "mongodb";
66

7-
import {QueryId} from "../../../../../../../common/index.js";
7+
import {QueryId} from "../../../../../../common/index.js";
88
import {
99
CLIENT_UPDATE_TIMEOUT_MILLIS,
1010
MongoCustomSocket,

0 commit comments

Comments
 (0)