Skip to content

Commit 3226b14

Browse files
committed
Move utils into src/util; move env utils from main.ts to src/utils/env.ts .
1 parent 61f8253 commit 3226b14

File tree

4 files changed

+111
-99
lines changed

4 files changed

+111
-99
lines changed

components/log-viewer-webui/server/src/DbManager.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
StreamFileMongoDocument,
2626
StreamFilesCollection,
2727
} from "./typings/DbManager.js";
28-
import {sleep} from "./utils.js";
28+
import {sleep} from "./utils/time.js";
2929

3030

3131
/**

components/log-viewer-webui/server/src/main.ts

+4-98
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,12 @@
11
import process from "node:process";
22

3-
import {config as dotenvConfig} from "dotenv";
4-
import {
5-
FastifyLoggerOptions,
6-
PinoLoggerOptions,
7-
} from "fastify/types/logger.js";
8-
93
import app from "./app.js";
4+
import {
5+
ENV_TO_LOGGER,
6+
parseEnvVars,
7+
} from "./utils/env.js";
108

119

12-
type NodeEnv = "development" | "production" | "test";
13-
14-
const KNOWN_NODE_ENVS = new Set<NodeEnv>([
15-
"development",
16-
"production",
17-
"test",
18-
]);
19-
20-
const NODE_ENV_DEFAULT: NodeEnv = "development";
21-
22-
/**
23-
* Maps known Node.js environments to Fastify logger configuration options.
24-
*/
25-
const ENV_TO_LOGGER
26-
: Record<NodeEnv, boolean | FastifyLoggerOptions & PinoLoggerOptions> = Object.freeze({
27-
development: {
28-
transport: {
29-
target: "pino-pretty",
30-
},
31-
},
32-
production: true,
33-
test: false,
34-
});
35-
36-
/**
37-
* Validates whether the given string corresponds to a known Node.js environment.
38-
*
39-
* @param value
40-
* @return True if the `value` is a known Node.js environment; otherwise, false.
41-
*/
42-
const isKnownNodeEnv = (value: string): value is NodeEnv => {
43-
return KNOWN_NODE_ENVS.has(value as NodeEnv);
44-
};
45-
46-
interface EnvVars {
47-
NODE_ENV: NodeEnv;
48-
49-
HOST: string;
50-
PORT: string;
51-
52-
CLP_DB_USER: string;
53-
CLP_DB_PASS: string;
54-
}
55-
56-
/**
57-
* Parses environment variables into config values for the application.
58-
*
59-
* @return
60-
* @throws {Error} if any required environment variable is undefined.
61-
*/
62-
const parseEnvVars = (): EnvVars => {
63-
dotenvConfig({
64-
path: [
65-
".env.local",
66-
".env",
67-
],
68-
});
69-
70-
71-
const {
72-
NODE_ENV, CLP_DB_USER, CLP_DB_PASS, HOST, PORT,
73-
} = process.env;
74-
const mandatoryEnvVars = {
75-
CLP_DB_USER, CLP_DB_PASS, HOST, PORT,
76-
} as EnvVars;
77-
78-
79-
// Check for mandatory environment variables
80-
for (const [key, value] of Object.entries(mandatoryEnvVars)) {
81-
if ("undefined" === typeof value) {
82-
throw new Error(`Environment variable ${key} must be defined.`);
83-
}
84-
}
85-
86-
// Sanitize other environment variables
87-
const sanitizedEnvVars = {
88-
NODE_ENV: NODE_ENV_DEFAULT as NodeEnv,
89-
};
90-
91-
if ("undefined" === typeof NODE_ENV || false === isKnownNodeEnv(NODE_ENV)) {
92-
console.log("NODE_ENV is not set, or the configured value is not known." +
93-
`Using default: ${NODE_ENV_DEFAULT}`);
94-
} else {
95-
sanitizedEnvVars.NODE_ENV = NODE_ENV;
96-
}
97-
98-
return {
99-
...mandatoryEnvVars,
100-
...sanitizedEnvVars,
101-
};
102-
};
103-
10410
/**
10511
* Sets up and runs the server.
10612
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import process from "node:process";
2+
3+
import {config as dotenvConfig} from "dotenv";
4+
import {
5+
FastifyLoggerOptions,
6+
PinoLoggerOptions,
7+
} from "fastify/types/logger.js";
8+
9+
10+
type NodeEnv = "development" | "production" | "test";
11+
12+
const KNOWN_NODE_ENVS = new Set<NodeEnv>([
13+
"development",
14+
"production",
15+
"test",
16+
]);
17+
18+
const NODE_ENV_DEFAULT: NodeEnv = "development";
19+
20+
/**
21+
* Maps known Node.js environments to Fastify logger configuration options.
22+
*/
23+
const ENV_TO_LOGGER
24+
: Record<NodeEnv, boolean | FastifyLoggerOptions & PinoLoggerOptions> = Object.freeze({
25+
development: {
26+
transport: {
27+
target: "pino-pretty",
28+
},
29+
},
30+
production: true,
31+
test: false,
32+
});
33+
34+
/**
35+
* Validates whether the given string corresponds to a known Node.js environment.
36+
*
37+
* @param value
38+
* @return True if the `value` is a known Node.js environment; otherwise, false.
39+
*/
40+
const isKnownNodeEnv = (value: string): value is NodeEnv => {
41+
return KNOWN_NODE_ENVS.has(value as NodeEnv);
42+
};
43+
44+
interface EnvVars {
45+
NODE_ENV: NodeEnv;
46+
47+
HOST: string;
48+
PORT: string;
49+
50+
CLP_DB_USER: string;
51+
CLP_DB_PASS: string;
52+
}
53+
54+
/**
55+
* Parses environment variables into config values for the application.
56+
*
57+
* @return
58+
* @throws {Error} if any required environment variable is undefined.
59+
*/
60+
const parseEnvVars = (): EnvVars => {
61+
dotenvConfig({
62+
path: [
63+
".env.local",
64+
".env",
65+
],
66+
});
67+
68+
69+
const {
70+
NODE_ENV, CLP_DB_USER, CLP_DB_PASS, HOST, PORT,
71+
} = process.env;
72+
const mandatoryEnvVars = {
73+
CLP_DB_USER, CLP_DB_PASS, HOST, PORT,
74+
} as EnvVars;
75+
76+
77+
// Check for mandatory environment variables
78+
for (const [key, value] of Object.entries(mandatoryEnvVars)) {
79+
if ("undefined" === typeof value) {
80+
throw new Error(`Environment variable ${key} must be defined.`);
81+
}
82+
}
83+
84+
// Sanitize other environment variables
85+
const sanitizedEnvVars = {
86+
NODE_ENV: NODE_ENV_DEFAULT as NodeEnv,
87+
};
88+
89+
if ("undefined" === typeof NODE_ENV || false === isKnownNodeEnv(NODE_ENV)) {
90+
console.log("NODE_ENV is not set, or the configured value is not known." +
91+
`Using default: ${NODE_ENV_DEFAULT}`);
92+
} else {
93+
sanitizedEnvVars.NODE_ENV = NODE_ENV;
94+
}
95+
96+
return {
97+
...mandatoryEnvVars,
98+
...sanitizedEnvVars,
99+
};
100+
};
101+
102+
export type {NodeEnv};
103+
export {
104+
ENV_TO_LOGGER,
105+
parseEnvVars,
106+
};

0 commit comments

Comments
 (0)