-
Notifications
You must be signed in to change notification settings - Fork 83
refactor(webui): Complete migration to new Fastify Architecture by removing log-viewer-webui Fastify app.
#1106
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,13 +6,13 @@ | |
| "scripts": { | ||
| "build": "tsc", | ||
| "build:watch": "npm run build -- -w", | ||
| "dev:start": "fastify start --ignore-watch=.ts$ -w -l info -P dist/server/src/fastify-v2/app.js", | ||
| "dev:start": "fastify start --ignore-watch=.ts$ -w -l info -P dist/server/src/app.js", | ||
| "dev": "npm run build && concurrently -k -p \"[{name}]\" -n \"TypeScript,App\" -c \"yellow.bold,cyan.bold\" \"npm:build:watch\" \"npm:dev:start\"", | ||
| "start": "npm run build && fastify start -l info dist/server/src/fastify-v2/app.js", | ||
| "start": "npm run build && fastify start -l info dist/server/src/app.js", | ||
| "standalone": "npm run build && node --env-file=.env dist/server/src/main.js", | ||
| "lint:check": "eslint . --max-warnings 0", | ||
| "lint:fix": "npm run lint:check -- --fix", | ||
| "test": "NODE_ENV=test tap --include='**/*test.ts'" | ||
| "test": "tap --include='**/*test.ts'" | ||
| }, | ||
| "author": "YScope Inc. <[email protected]>", | ||
| "license": "Apache-2.0", | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,106 @@ | ||
| // Reference: https://github.com/fastify/demo/blob/main/src/app.ts | ||
|
|
||
| import path from "node:path"; | ||
|
|
||
| import {fastifyAutoload} from "@fastify/autoload"; | ||
| import { | ||
| FastifyInstance, | ||
| FastifyPluginAsync, | ||
| FastifyPluginOptions, | ||
| } from "fastify"; | ||
| import {StatusCodes} from "http-status-codes"; | ||
|
|
||
| import staticRoutes from "./routes/static.js"; | ||
|
|
||
| const RATE_LIMIT_MAX_REQUESTS = 3; | ||
| const RATE_LIMIT_TIME_WINDOW_MS = 500; | ||
|
|
||
| /** | ||
| * Creates the Fastify app with the given options. | ||
| * | ||
| * TODO: Once old webui code is refactored to new modlular fastify style, this plugin should be | ||
| * removed. | ||
| * Registers all plugins and routes. | ||
| * | ||
| * @param fastify | ||
| * @return | ||
| * @param opts | ||
| */ | ||
| const FastifyV1App: FastifyPluginAsync = async ( | ||
| fastify: FastifyInstance | ||
| ) => { | ||
| // Register the routes | ||
| await fastify.register(staticRoutes); | ||
| }; | ||
|
|
||
| export default FastifyV1App; | ||
| // eslint-disable-next-line max-lines-per-function | ||
| export default async function serviceApp ( | ||
| fastify: FastifyInstance, | ||
| opts: FastifyPluginOptions | ||
| ) { | ||
| // Option only serves testing purpose. It's used in testing to expose all decorators to the | ||
| // test app. Some decorators may not be exposed in production. | ||
| delete opts.skipOverride; | ||
davemarco marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Loads all external plugins. Registered first as application plugins might depend on them. | ||
| await fastify.register(fastifyAutoload, { | ||
| dir: path.join(import.meta.dirname, "plugins/external"), | ||
| options: {...opts}, | ||
| }); | ||
|
|
||
| // Loads all application plugins. | ||
| fastify.register(fastifyAutoload, { | ||
| dir: path.join(import.meta.dirname, "plugins/app"), | ||
| options: {...opts}, | ||
| }); | ||
|
|
||
| // Loads all routes. | ||
| fastify.register(fastifyAutoload, { | ||
| autoHooks: true, | ||
| cascadeHooks: true, | ||
| dir: path.join(import.meta.dirname, "routes"), | ||
| options: {...opts}, | ||
| }); | ||
|
|
||
| fastify.setErrorHandler((err, request, reply) => { | ||
| fastify.log.error( | ||
| { | ||
| err: err, | ||
| request: { | ||
| method: request.method, | ||
| url: request.url, | ||
| query: request.query, | ||
| params: request.params, | ||
| }, | ||
| }, | ||
| "Unhandled error occurred" | ||
| ); | ||
|
|
||
| if ("undefined" !== typeof err.statusCode && | ||
| Number(StatusCodes.INTERNAL_SERVER_ERROR) > err.statusCode | ||
| ) { | ||
| reply.code(err.statusCode); | ||
|
|
||
| return err.message; | ||
| } | ||
|
|
||
| reply.internalServerError(); | ||
|
|
||
| return { | ||
| message: "Internal Server Error", | ||
| }; | ||
| }); | ||
|
|
||
| // An attacker could search for valid URLs if 404 error handling is not rate limited. | ||
| fastify.setNotFoundHandler( | ||
| { | ||
| preHandler: fastify.rateLimit({ | ||
| max: RATE_LIMIT_MAX_REQUESTS, | ||
| timeWindow: RATE_LIMIT_TIME_WINDOW_MS, | ||
| }), | ||
| }, | ||
| (request, reply) => { | ||
| request.log.warn( | ||
| { | ||
| request: { | ||
| method: request.method, | ||
| url: request.url, | ||
| query: request.query, | ||
| params: request.params, | ||
| }, | ||
| }, | ||
| "Resource not found" | ||
| ); | ||
|
|
||
| reply.notFound(); | ||
|
|
||
| return {message: "Not Found"}; | ||
| } | ||
| ); | ||
| } | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -5,8 +5,8 @@ import { | |||||||
| import {getSignedUrl} from "@aws-sdk/s3-request-presigner"; | ||||||||
| import fp from "fastify-plugin"; | ||||||||
|
|
||||||||
| import settings from "../../../../../settings.json" with {type: "json"}; | ||||||||
| import {Nullable} from "../../../../typings/common.js"; | ||||||||
| import settings from "../../../../settings.json" with {type: "json"}; | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. JSON import assertion syntax may not compile under Node ≥ 20 / TS ≥ 5.4 The current syntax uses the Stage-3 -import settings from "../../../../settings.json" with {type: "json"};
+import settings from "../../../../settings.json" assert { type: "json" };Please verify the chosen syntax against the toolchain versions in CI. 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||
| import {Nullable} from "../../../typings/common.js"; | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Importing “.js” from TypeScript may generate incorrect
Recommend importing the bare module and let TS resolve the -import {Nullable} from "../../../typings/common.js";
+import {Nullable} from "../../../typings/common";or add a 🤖 Prompt for AI Agents |
||||||||
| import {PRE_SIGNED_URL_EXPIRY_TIME_SECONDS} from "./typings.js"; | ||||||||
|
|
||||||||
|
|
||||||||
|
|
||||||||
Uh oh!
There was an error while loading. Please reload this page.