Skip to content

Commit 7e19449

Browse files
committed
perf: upgrade for new lavateinn
1 parent 360111e commit 7e19449

File tree

8 files changed

+43
-62
lines changed

8 files changed

+43
-62
lines changed

.env.default

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
# node env ('production' or 'development')
2-
NODE_ENV="development"
3-
41
# general
5-
TRUST_PROXY="loopback" # as known as trust proxy of express.js ('loopback', 'linklocal', 'uniquelocal' or ip addresses, empty to disable)
2+
NODE_ENV="development" # node environment ('development', 'production')
3+
RUNTIME_ENV="native" # runtime environment ('native' or 'container')
4+
5+
# protocol
6+
TRUST_PROXY="_disabled_" # as known as trust proxy of express.js ('loopback', 'linklocal', 'uniquelocal' or ip addresses, empty or '_disabled_' to disable)
67
ENABLED_PROTOCOLS="http" # enable protocols ('http', 'https' or both)
78
ENABLED_REDIRECT_HTTP_HTTPS="no" # redirect http to https ('yes' or 'no')
89

@@ -13,12 +14,14 @@ INSTANCE_URL="http://localhost:3000" # the url of the instance
1314
# logging
1415
LOGGING_LEVEL="info" # as known as logging level of winston ('error', 'warn', 'info', 'http', 'verbose', 'debug', 'silly')
1516
LOGGING_CONSOLE="yes" # logging into console ('yes' or 'no')
16-
LOGGING_FILE_PATH="data/lavateinn.log" # logging into file (empty to disable)
17-
LOGGING_HTTP_URL="http://localhost:3000/v1/logs" # logging into http (empty to disable)
17+
LOGGING_FILE_PATH="data/lavateinn.log" # logging into file (empty or '_disabled_' to disable)
18+
LOGGING_HTTP_URL="_disabled_" # logging into http (empty or '_disabled_' to disable)
1819

1920
# database
2021
SEQUELIZE_URL="sqlite:data/lavateinn.db" # sequelize url
2122
AMPQ_URL="amqp://localhost" # amqp url
23+
REDIS_URL="redis://localhost:6379" # redis url
24+
REDIS_NAMESPACE="lavateinn" # the redis namespace of the application
2225

2326
# http
2427
HTTP_PORT="3000"

src/config.mjs

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ export function runLoader() {
1414
const dotenvPathInstance = new URL("../.env", import.meta.url);
1515

1616
const isDotenvExists = existsSync(dotenvPathInstance);
17-
const isAppConfigured = get("APP_CONFIGURED") === "1";
17+
18+
// Check the application configured or not,
19+
// '1' means it won't check the .env file exists.
20+
// It can't be set by any .env file, because it'll check
21+
// the system environment variables directly before loading the .env files.
22+
const isAppConfigured = process.env["APP_CONFIGURED"] === "1";
1823

1924
if (!isDotenvExists && !isAppConfigured) {
2025
console.error(
@@ -42,7 +47,7 @@ export function runLoader() {
4247
* @returns {string} The NODE_ENV value.
4348
*/
4449
export function getNodeEnv() {
45-
return getFallback("NODE_ENV", "development");
50+
return get("NODE_ENV");
4651
}
4752

4853
/**
@@ -51,7 +56,7 @@ export function getNodeEnv() {
5156
* @returns {string} The RUNTIME_ENV value.
5257
*/
5358
export function getRuntimeEnv() {
54-
return getFallback("RUNTIME_ENV", "native");
59+
return get("RUNTIME_ENV");
5560
}
5661

5762
/**
@@ -60,7 +65,7 @@ export function getRuntimeEnv() {
6065
* @returns {string} The INSTANCE_MODE value.
6166
*/
6267
export function getInstanceMode() {
63-
return getFallback("INSTANCE_MODE", "single");
68+
return get("INSTANCE_MODE");
6469
}
6570

6671
/**
@@ -82,13 +87,17 @@ export function isCluster() {
8287
}
8388

8489
/**
85-
* Shortcut to get config value.
90+
* Get the value from config or with an error thrown.
8691
* @module src/config
8792
* @param {string} key - The config key.
8893
* @returns {string} The config value.
94+
* @throws {Error} If value is undefined, throw an error.
8995
*/
9096
export function get(key) {
9197
const value = process.env[key];
98+
if (value === undefined) {
99+
throw new Error(`config key ${key} is undefined`);
100+
}
92101
if (value === "_disabled_") {
93102
return "";
94103
}
@@ -102,7 +111,7 @@ export function get(key) {
102111
* @returns {boolean} The boolean value.
103112
*/
104113
export function getEnabled(key) {
105-
return getMust(key) === "yes";
114+
return get(key) === "yes";
106115
}
107116

108117
/**
@@ -113,34 +122,8 @@ export function getEnabled(key) {
113122
* @returns {string[]} The array value.
114123
*/
115124
export function getSplited(key, separator = ",") {
116-
return getMust(key).
125+
return get(key).
117126
split(separator).
118127
filter((i) => i).
119128
map((i) => i.trim());
120129
}
121-
122-
/**
123-
* Get the value from config with error thrown.
124-
* @module src/config
125-
* @param {string} key - The config key.
126-
* @returns {string} The expected value.
127-
* @throws {Error} If value is undefined, throw an error.
128-
*/
129-
export function getMust(key) {
130-
const value = get(key);
131-
if (value === undefined) {
132-
throw new Error(`config key ${key} is undefined`);
133-
}
134-
return value;
135-
}
136-
137-
/**
138-
* Get the value from config with fallback.
139-
* @module src/config
140-
* @param {string} key - The config key.
141-
* @param {string} fallback - The fallback value.
142-
* @returns {string} The expected value.
143-
*/
144-
export function getFallback(key, fallback) {
145-
return get(key) || fallback;
146-
}

src/execute.mjs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
// Import modules
55
import {
6-
getMust,
6+
get,
77
getSplited,
88
} from "./config.mjs";
99

@@ -37,8 +37,8 @@ import {
3737
*/
3838
function setupHttpProtocol(app) {
3939
const protocol = "http";
40-
const hostname = getMust("HTTP_HOSTNAME");
41-
const port = parseInt(getMust("HTTP_PORT"));
40+
const hostname = get("HTTP_HOSTNAME");
41+
const port = parseInt(get("HTTP_PORT"));
4242

4343
const httpServer = http.createServer({}, app);
4444
httpServer.listen(port, hostname);
@@ -54,12 +54,12 @@ function setupHttpProtocol(app) {
5454
*/
5555
async function setupHttpsProtocol(app) {
5656
const protocol = "https";
57-
const hostname = getMust("HTTPS_HOSTNAME");
58-
const port = parseInt(getMust("HTTPS_PORT"));
57+
const hostname = get("HTTPS_HOSTNAME");
58+
const port = parseInt(get("HTTPS_PORT"));
5959

6060
const [key, cert] = await Promise.all([
61-
readFile(getMust("HTTPS_KEY_PATH")),
62-
readFile(getMust("HTTPS_CERT_PATH")),
61+
readFile(get("HTTPS_KEY_PATH")),
62+
readFile(get("HTTPS_CERT_PATH")),
6363
]);
6464

6565
const httpsServer = https.createServer({key, cert}, app);

src/init/const.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export const APP_NAME = "earendel";
77
export const APP_DESCRIPTION = "Simple cache server for DevOps.";
88

99
export const APP_VERSION = "latest"; // SemVer
10-
export const LAV_VERSION = "2025.01.29"; // CalVer, UTC +8
10+
export const LAV_VERSION = "2025.01.30"; // CalVer, UTC +8
1111

1212
export const APP_AUTHOR_NAME = "Star Inc.";
1313
export const APP_AUTHOR_URL = "https://starinc.xyz";

src/init/instance.mjs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,20 @@ import {
2121

2222
import {
2323
isCluster,
24-
getMust,
24+
get,
2525
} from "../config.mjs";
2626

2727
// Define instance id
2828
export const instanceId = `${APP_NAME}#${nanoid()}`;
2929

3030
// Define instance http url (aka. canonical url)
31-
export const instanceUrl = getMust("INSTANCE_URL");
31+
export const instanceUrl = get("INSTANCE_URL");
3232

3333
// Define instance role (single, primary or worker)
3434
export const instanceRole = isCluster() ? (
3535
cluster.isPrimary ? "primary" : "worker"
3636
) : "single";
3737

38-
// Earendel only can be allowed to use "single" mode
39-
if (instanceRole !== "single") {
40-
throw new Error("earendel only can be allowed to use \"single\" mode");
41-
}
42-
4338
// Define instance context
4439
export const instanceContext = new Map();
4540

src/init/logger.mjs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55

66
// Import modules
77
import winston from "winston";
8-
import {getMust, getEnabled} from "../config.mjs";
8+
import {get, getEnabled} from "../config.mjs";
99

1010
// Read configuration
11-
const loggingLevel = getMust("LOGGING_LEVEL");
11+
const loggingLevel = get("LOGGING_LEVEL");
1212
const isLoggingConsole = getEnabled("LOGGING_CONSOLE");
13-
const loggingFilePath = getMust("LOGGING_FILE_PATH");
14-
const loggingHttpUrl = getMust("LOGGING_HTTP_URL");
13+
const loggingFilePath = get("LOGGING_FILE_PATH");
14+
const loggingHttpUrl = get("LOGGING_HTTP_URL");
1515

1616
// Define logging configuration
1717
const useLoggingConsole = () => isLoggingConsole &&

src/middleware/cors.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
// Cross-Origin Resource Sharing
55

66
// Import modules
7-
import {getMust} from "../config.mjs";
7+
import {get} from "../config.mjs";
88
import cors from "cors";
99

1010
// Read configuration
11-
const origin = getMust("CORS_ORIGIN");
11+
const origin = get("CORS_ORIGIN");
1212

1313
// Export (function)
1414
export default cors({origin});

src/middleware/origin.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// if not, interrupt it.
66

77
// Import modules
8-
import {getMust} from "../config.mjs";
8+
import {get} from "../config.mjs";
99
import {express, StatusCodes} from "../init/express.mjs";
1010
import {useLogger} from "../init/logger.mjs";
1111

@@ -34,7 +34,7 @@ export default function middlewareOrigin(req, res, next) {
3434

3535
// Get actual and expected URLs
3636
const actualUrl = req.header("origin");
37-
const expectedUrl = getMust("CORS_ORIGIN");
37+
const expectedUrl = get("CORS_ORIGIN");
3838

3939
// Origin match
4040
if (actualUrl === expectedUrl) {

0 commit comments

Comments
 (0)