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

build(log-viewer-webui): Migrate server codebase to TypeScript and update dependencies. #647

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
Fix lint.
  • Loading branch information
junhaoliao committed Dec 31, 2024
commit 85e6ff23f141241121154a86f57136f48fccd162
45 changes: 32 additions & 13 deletions components/log-viewer-webui/server/src/main.ts
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compared to the original main.js, changes in this file include:

  1. envToLogger was pulled out from the main() scope into global scope.
  2. Reference to process.env.NODE_ENV was removed in favour of parseEnv().NODE_ENV, which requires parseEnv() to also parse NODE_ENV from the environment variables.
  3. Added isKnownNodeEnv() for easier type deduction.

Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import dotenv from "dotenv";
import {config as dotenvConfig} from "dotenv";
import {
FastifyLoggerOptions,
PinoLoggerOptions,
} from "fastify/types/logger.js";
import process from "node:process";

import app from "./app.js";
Expand All @@ -8,7 +12,11 @@ type NodeEnv = "development" | "production" | "test";

const NODE_ENV_DEFAULT: NodeEnv = "development";

const ENV_TO_LOGGER: Record<NodeEnv, any> = Object.freeze({
/**
* Maps known Node.js environments to Fastify logger configuration options.
*/
const ENV_TO_LOGGER
: Record<NodeEnv, boolean | FastifyLoggerOptions & PinoLoggerOptions> = Object.freeze({
development: {
transport: {
target: "pino-pretty",
Expand All @@ -18,8 +26,18 @@ const ENV_TO_LOGGER: Record<NodeEnv, any> = Object.freeze({
test: false,
});

/**
* Validates whether the given string corresponds to a known Node.js environment.
*
* @param value
* @return True if the `value` is a known Node.js environment; otherwise, false.
*/
const isKnownNodeEnv = (value: string): value is NodeEnv => {
return ["development", "production", "test"].includes(value);
return [
"development",
"production",
"test",
].includes(value);
};

interface EnvVars {
Expand All @@ -35,11 +53,11 @@ interface EnvVars {
/**
* Parses environment variables into config values for the application.
*
* @return {{CLP_DB_USER: string, CLP_DB_PASS: string, HOST: string, PORT: string}}
* @return
* @throws {Error} if any required environment variable is undefined.
*/
const parseEnvVars = (): EnvVars => {
dotenv.config({
dotenvConfig({
path: [
".env.local",
".env",
Expand All @@ -63,19 +81,20 @@ const parseEnvVars = (): EnvVars => {
}

// Sanitize other environment variables
let sanitizedEnvVars = {
NODE_ENV: NODE_ENV_DEFAULT as NodeEnv
}
if (typeof NODE_ENV === "undefined" || false === isKnownNodeEnv(NODE_ENV)) {
console.log(`NODE_ENV is not set, or the configured value is not known.`+
const sanitizedEnvVars = {
NODE_ENV: NODE_ENV_DEFAULT as NodeEnv,
};

if ("undefined" === typeof NODE_ENV || false === isKnownNodeEnv(NODE_ENV)) {
console.log("NODE_ENV is not set, or the configured value is not known." +
`Using default: ${NODE_ENV_DEFAULT}`);
} else {
sanitizedEnvVars.NODE_ENV = NODE_ENV;
}

return {
...mandatoryEnvVars,
...sanitizedEnvVars
...sanitizedEnvVars,
};
};

Expand All @@ -86,7 +105,7 @@ const main = async () => {
const envVars = parseEnvVars();
const server = await app({
fastifyOptions: {
logger: ENV_TO_LOGGER[envVars.NODE_ENV] ?? true,
logger: ENV_TO_LOGGER[envVars.NODE_ENV],
},
sqlDbPass: envVars.CLP_DB_PASS,
sqlDbUser: envVars.CLP_DB_USER,
Expand All @@ -100,7 +119,7 @@ const main = async () => {
}
};

main().catch((e) => {
main().catch((e: unknown) => {
console.error(e);
process.exit(1);
});
Loading