Simplify port usage

This commit is contained in:
FoxxMD
2024-01-18 15:23:56 -05:00
parent bb0c079757
commit c98672bcef
2 changed files with 5 additions and 23 deletions
+2 -10
View File
@@ -49,27 +49,20 @@ export interface RootOptions {
const createRoot = (options?: RootOptions) => {
const {
port,
port = 9078,
baseUrl = process.env.BASE_URL,
} = options || {};
const configDir = process.env.CONFIG_DIR || path.resolve(projectDir, `./config`);
const envPort = process.env.PORT;
return createContainer().add({
version,
configDir: configDir,
logDir: logPath,
isProd: process.env.NODE_ENV !== undefined && (process.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'prod'),
configPort: port,
apiPort: process.env.API_PORT ?? 9079,
mainPort: envPort ?? 3000,
port: process.env.PORT ?? port,
clientEmitter: () => new WildcardEmitter(),
sourceEmitter: () => new WildcardEmitter(),
notifierEmitter: () => new EventEmitter(),
}).add((items) => {
let usedPort = items.configPort ?? envPort;
if (usedPort === undefined) {
usedPort = items.isProd ? items.mainPort : items.apiPort;
}
const base = normalizeUrl(baseUrl ?? 'http://localhost', {removeSingleSlash: true});
const u = new URL(base);
let localUrl = u.toString();
@@ -77,7 +70,6 @@ const createRoot = (options?: RootOptions) => {
localUrl = `${u.origin}:${items.mainPort}`;
}
return {
port: usedPort,
clients: () => new ScrobbleClients(items.clientEmitter, items.sourceEmitter, localUrl, items.configDir),
sources: () => new ScrobbleSources(items.sourceEmitter, localUrl, items.configDir),
notifiers: () => new Notifiers(items.notifierEmitter, items.clientEmitter, items.sourceEmitter),
+3 -13
View File
@@ -40,23 +40,13 @@ export const initServer = async (parentLogger: Logger, initialOutput: LogInfo[]
const root = getRoot();
const isProd = root.get('isProd');
const apiPort = root.get('apiPort');
const mainPort = root.get('mainPort');
const port = root.get('port');
const local = root.get('localUrl');
const localDefined = root.get('hasDefinedBaseUrl');
setupApi(app, logger, initialOutput);
// app.get("/*", function (req, res) {
// if (!isProd) {
// logger.warn(`In development environment this path (on port ${apiPort}) does nothing. You most likely want port ${mainPort}`)
// }
// res.sendFile(path.join(buildDir, "index.html"));
// });
const backendPort = isProd ? port : apiPort;
const server = app.listen(backendPort);
const server = app.listen(port);
ViteExpress.bind(app, server);
const addy = getAddress();
@@ -76,10 +66,10 @@ export const initServer = async (parentLogger: Logger, initialOutput: LogInfo[]
switch (k) {
case 'host':
case 'v4':
addresses.push(`---> ${k === 'host' ? 'Local'.padEnd(14, ' ') : 'Network'.padEnd(14, ' ')} http://${v}:${backendPort}`);
addresses.push(`---> ${k === 'host' ? 'Local'.padEnd(14, ' ') : 'Network'.padEnd(14, ' ')} http://${v}:${port}`);
break;
case 'v6':
addresses.push(`---> Network (IPv6) http://[${v}]:${backendPort}`);
addresses.push(`---> Network (IPv6) http://[${v}]:${port}`);
}
}
}