X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Fui-server%2FUIServerFactory.ts;h=cf176bdc0403d2be85890204ba7b67e06f9b6c4a;hb=9388120364d5a2f3b9103f05802e1756d2dad04f;hp=0eb71cd469f7e7b3431a6caa0e7a9c19fd949314;hpb=4c3f6c20f9416e148a3d26b6a06acc13274ab469;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ui-server/UIServerFactory.ts b/src/charging-station/ui-server/UIServerFactory.ts index 0eb71cd4..cf176bdc 100644 --- a/src/charging-station/ui-server/UIServerFactory.ts +++ b/src/charging-station/ui-server/UIServerFactory.ts @@ -5,8 +5,10 @@ import { ApplicationProtocol, ApplicationProtocolVersion, AuthenticationType, + ConfigurationSection, type UIServerConfiguration } from '../../types/index.js' +import { logger, logPrefix } from '../../utils/index.js' import type { AbstractUIServer } from './AbstractUIServer.js' import { UIHttpServer } from './UIHttpServer.js' import { isLoopback } from './UIServerUtils.js' @@ -26,7 +28,7 @@ export class UIServerFactory { !Object.values(AuthenticationType).includes(uiServerConfiguration.authentication.type) ) { throw new BaseError( - `Unknown authentication type '${uiServerConfiguration.authentication.type}' for UI server` + `Unknown authentication type '${uiServerConfiguration.authentication.type}' in '${ConfigurationSection.uiServer}' configuration section` ) } if ( @@ -34,28 +36,26 @@ export class UIServerFactory { uiServerConfiguration.authentication?.enabled === true && uiServerConfiguration.authentication.type === AuthenticationType.PROTOCOL_BASIC_AUTH ) { - throw new BaseError('Protocol basic authentication is not supported for HTTP UI server') + throw new BaseError( + `'${uiServerConfiguration.authentication.type}' authentication type with application protocol type '${uiServerConfiguration.type}' is not supported in '${ConfigurationSection.uiServer}' configuration section` + ) } if ( uiServerConfiguration.authentication?.enabled !== true && // eslint-disable-next-line @typescript-eslint/no-non-null-assertion !isLoopback(uiServerConfiguration.options!.host!) ) { - console.warn( - chalk.yellow( - 'Non loopback address in UI server configuration without authentication enabled. This is not recommended' - ) - ) + const logMsg = `Non loopback address in '${ConfigurationSection.uiServer}' configuration section without authentication enabled. This is not recommended` + logger.warn(`${UIServerFactory.logPrefix()} ${logMsg}`) + console.warn(chalk.yellow(logMsg)) } if ( uiServerConfiguration.type === ApplicationProtocol.WS && uiServerConfiguration.version !== ApplicationProtocolVersion.VERSION_11 ) { - console.warn( - chalk.yellow( - `Only version ${ApplicationProtocolVersion.VERSION_11} is supported for WebSocket UI server. Falling back to version ${ApplicationProtocolVersion.VERSION_11}` - ) - ) + const logMsg = `Only version ${ApplicationProtocolVersion.VERSION_11} with application protocol type '${uiServerConfiguration.type}' is supported in '${ConfigurationSection.uiServer}' configuration section. Falling back to version ${ApplicationProtocolVersion.VERSION_11}` + logger.warn(`${UIServerFactory.logPrefix()} ${logMsg}`) + console.warn(chalk.yellow(logMsg)) uiServerConfiguration.version = ApplicationProtocolVersion.VERSION_11 } switch (uiServerConfiguration.type) { @@ -63,7 +63,30 @@ export class UIServerFactory { return new UIHttpServer(uiServerConfiguration) case ApplicationProtocol.WS: default: + if ( + !Object.values(ApplicationProtocol).includes( + uiServerConfiguration.type as ApplicationProtocol + ) + ) { + const logMsg = `Unknown application protocol type '${uiServerConfiguration.type}' in '${ + ConfigurationSection.uiServer + // eslint-disable-next-line @typescript-eslint/no-base-to-string + }' configuration section from values '${ApplicationProtocol.toString()}', defaulting to '${ + ApplicationProtocol.WS + }'` + logger.warn(`${UIServerFactory.logPrefix()} ${logMsg}`) + console.warn(logMsg) + } return new UIWebSocketServer(uiServerConfiguration) } } + + private static readonly logPrefix = (modName?: string, methodName?: string): string => { + const logMsgPrefix = 'UI Server' + const logMsg = + modName != null && methodName != null + ? ` ${logMsgPrefix} | ${modName}.${methodName}:` + : ` ${logMsgPrefix} |` + return logPrefix(logMsg) + } }