change(config): LOG_LEVEL environment variable, fixed erroneous port for default HTTP_PUBLIC_URL

This commit is contained in:
tecc 2023-09-09 19:25:00 +02:00
parent b2c43f084b
commit 3557f69743
No known key found for this signature in database
GPG Key ID: 622EEC5BAE5EBD3A
4 changed files with 23 additions and 5 deletions

View File

@ -6,13 +6,17 @@
# "development", "dev": Development environment
# "production", "prod": Production environment
ENVIRONMENT=development
# -- By default, ENVIRONMENT dictates the log level.
# -- However, the log level can be set manually through LOG_LEVEL.
# -- See pino's log levels. They're conventional. This value is also case-insensitive.
# LOG_LEVEL=
# -- Whether or not to allow API calls for unpublished (e.g. not-yet-ready) locales
ALLOW_UNPUBLISHED_LOCALES=true
# -- This base URL only refers to the backend.
# -- This also means we don't need HOME_URL, since the API doesn't have a homepage per se.
HTTP_BASE_URL=http://localhost:5000
HTTP_BASE_URL=http://localhost:4000
# -- These two variables configure the bind address. It defaults to 0.0.0.0:4000.
# HTTP_HOST=0.0.0.0
# HTTP_PORT=4000

View File

@ -1,7 +1,8 @@
import "dotenv/config";
import { identity, parseBool, parseIntOrThrow } from "@pronounspage/common/util";
import { identity, parseBool, parseIntOrThrow, toLowerCase } from "@pronounspage/common/util";
import * as path from "node:path";
import * as fs from "node:fs";
import type { log } from "#self/log";
export enum Environment {
DEVELOPMENT = "dev",
@ -10,6 +11,7 @@ export enum Environment {
export interface Config {
environment: Environment;
logLevel?: typeof log["level"];
http: {
baseUrl: string;
host: string;
@ -73,6 +75,7 @@ export function loadConfigFromEnv(): Config {
parseEnvironment,
Environment.DEVELOPMENT,
),
logLevel: envVarOrDefault("LOG_LEVEL", toLowerCase, undefined),
http: {
baseUrl: envVarNotNull("HTTP_BASE_URL", identity),
host: envVarOrDefault("HTTP_HOST", identity, "0.0.0.0"),

View File

@ -18,12 +18,19 @@ const loggerConfigurations = {
} satisfies Record<Environment, LoggerOptions>;
let environment;
let environment, logLevel;
try {
environment = getConfig().environment
const config = getConfig();
environment = config.environment
logLevel = config.logLevel;
} catch (e) {
environment = Environment.DEVELOPMENT;
}
export const log = pino(loggerConfigurations[environment]);
const configuration = loggerConfigurations[environment];
if (logLevel) {
configuration.level = logLevel;
}
export const log = pino(configuration);
export default log;

View File

@ -1,6 +1,10 @@
export function identity<T>(value: T): T {
return value;
}
// This is unnecessarily typed but I don't really care, it works doesn't it?
export function toLowerCase<S extends string>(value: S): Lowercase<S> {
return value.toLowerCase() as Lowercase<S>
}
export function parseIntOrThrow(value: string, radix?: number): number {
if (radix != null && (radix < 2 || radix > 36)) {