diff --git a/server.js b/server.js index 97e286fdd..b39dbf261 100644 --- a/server.js +++ b/server.js @@ -18,7 +18,6 @@ import responseTime from 'response-time'; import helmet from 'helmet'; import bodyParser from 'body-parser'; import open from 'open'; -import fetch from 'node-fetch'; // local library imports import { CommandLineParser } from './src/command-line.js'; @@ -36,7 +35,6 @@ import { setUserDataMiddleware, shouldRedirectToLogin, tryAutoLogin, - router as userDataRouter, cleanUploads, getSessionCookieAge, } from './src/users.js'; @@ -64,8 +62,6 @@ import { ensureThumbnailCache } from './src/endpoints/thumbnails.js'; // Routers import { router as usersPublicRouter } from './src/endpoints/users-public.js'; -import { router as usersPrivateRouter } from './src/endpoints/users-private.js'; -import { router as usersAdminRouter } from './src/endpoints/users-admin.js'; import { init as statsInit, onExit as statsOnExit } from './src/endpoints/stats.js'; import { checkForNewContent } from './src/endpoints/content-manager.js'; import { init as settingsInit } from './src/endpoints/settings.js'; @@ -255,17 +251,10 @@ app.get('/api/ping', (request, response) => { }); // File uploads -const uploadsPath = path.join(globalThis.DATA_ROOT, UPLOADS_DIRECTORY); +const uploadsPath = path.join(cliArgs.dataRoot, UPLOADS_DIRECTORY); app.use(multer({ dest: uploadsPath, limits: { fieldSize: 10 * 1024 * 1024 } }).single('avatar')); app.use(multerMonkeyPatch); -// User data mount -app.use('/', userDataRouter); -// Private endpoints -app.use('/api/users', usersPrivateRouter); -// Admin endpoints -app.use('/api/users', usersAdminRouter); - app.get('/version', async function (_, response) { const data = await getVersion(); response.send(data); @@ -335,8 +324,8 @@ async function preSetupTasks() { * @param {import('./src/server-startup.js').ServerStartupResult} result The result of the server startup * @returns {Promise} */ -async function postSetupTasks({ v6Failed, v4Failed, useIPv6, useIPv4 }) { - const autorunHostname = await cliArgs.getAutorunHostname(useIPv6, useIPv4); +async function postSetupTasks(result) { + const autorunHostname = await cliArgs.getAutorunHostname(result); const autorunUrl = cliArgs.getAutorunUrl(autorunHostname); console.log('Launching...'); @@ -348,13 +337,13 @@ async function postSetupTasks({ v6Failed, v4Failed, useIPv6, useIPv4 }) { let logListen = 'SillyTavern is listening on'; - if (useIPv6 && !v6Failed) { + if (result.useIPv6 && !result.v6Failed) { logListen += color.green( ' IPv6: ' + cliArgs.getIPv6ListenUrl().host, ); } - if (useIPv4 && !v4Failed) { + if (result.useIPv4 && !result.v4Failed) { logListen += color.green( ' IPv4: ' + cliArgs.getIPv4ListenUrl().host, ); diff --git a/src/command-line.js b/src/command-line.js index 09e2c287f..eb0bfc7f3 100644 --- a/src/command-line.js +++ b/src/command-line.js @@ -4,33 +4,33 @@ import ipRegex from 'ip-regex'; import { canResolve, color, getConfigValue, stringToBool } from './util.js'; /** - * @typedef {object} CommandLineArguments - * @property {string} dataRoot - * @property {number} port - * @property {boolean} listen - * @property {string} listenAddressIPv6 - * @property {string} listenAddressIPv4 - * @property {boolean|string} enableIPv4 - * @property {boolean|string} enableIPv6 - * @property {boolean} dnsPreferIPv6 - * @property {boolean} autorun - * @property {string} autorunHostname - * @property {number} autorunPortOverride - * @property {boolean} enableCorsProxy - * @property {boolean} disableCsrf - * @property {boolean} ssl - * @property {string} certPath - * @property {string} keyPath - * @property {boolean} whitelistMode - * @property {boolean} avoidLocalhost - * @property {boolean} basicAuthMode - * @property {boolean} requestProxyEnabled - * @property {string} requestProxyUrl - * @property {string[]} requestProxyBypass - * @property {function(): URL} getIPv4ListenUrl - * @property {function(): URL} getIPv6ListenUrl - * @property {function(boolean, boolean): Promise} getAutorunHostname - * @property {function(string): URL} getAutorunUrl + * @typedef {object} CommandLineArguments Parsed command line arguments + * @property {string} dataRoot Data root directory + * @property {number} port Port number + * @property {boolean} listen If SillyTavern is listening on all network interfaces + * @property {string} listenAddressIPv6 IPv6 address to listen to + * @property {string} listenAddressIPv4 IPv4 address to listen to + * @property {boolean|string} enableIPv4 If enable IPv4 protocol ("auto" is also allowed) + * @property {boolean|string} enableIPv6 If enable IPv6 protocol ("auto" is also allowed) + * @property {boolean} dnsPreferIPv6 If prefer IPv6 for DNS + * @property {boolean} autorun If automatically launch SillyTavern in the browser + * @property {string} autorunHostname Autorun hostname + * @property {number} autorunPortOverride Autorun port override (-1 is use server port) + * @property {boolean} enableCorsProxy If enable CORS proxy + * @property {boolean} disableCsrf If disable CSRF protection + * @property {boolean} ssl If enable SSL + * @property {string} certPath Path to certificate + * @property {string} keyPath Path to private key + * @property {boolean} whitelistMode If enable whitelist mode + * @property {boolean} avoidLocalhost If avoid using 'localhost' for autorun in auto mode + * @property {boolean} basicAuthMode If enable basic authentication + * @property {boolean} requestProxyEnabled If enable outgoing request proxy + * @property {string} requestProxyUrl Request proxy URL + * @property {string[]} requestProxyBypass Request proxy bypass list + * @property {function(): URL} getIPv4ListenUrl Get IPv4 listen URL + * @property {function(): URL} getIPv6ListenUrl Get IPv6 listen URL + * @property {function(import('./server-startup.js').ServerStartupResult): Promise} getAutorunHostname Get autorun hostname + * @property {function(string): URL} getAutorunUrl Get autorun URL */ /** @@ -220,7 +220,7 @@ export class CommandLineParser { (':' + this.port), ); }, - getAutorunHostname: async function (useIPv6, useIPv4) { + getAutorunHostname: async function ({ useIPv6, useIPv4 }) { if (this.autorunHostname === 'auto') { let localhostResolve = await canResolve('localhost', useIPv6, useIPv4); diff --git a/src/server-startup.js b/src/server-startup.js index 656eef5f8..34b0d46f5 100644 --- a/src/server-startup.js +++ b/src/server-startup.js @@ -4,6 +4,9 @@ import fs from 'node:fs'; import { color, urlHostnameToIPv6, getHasIP } from './util.js'; // Express routers +import { router as userDataRouter } from './users.js'; +import { router as usersPrivateRouter } from './endpoints/users-private.js'; +import { router as usersAdminRouter } from './endpoints/users-admin.js'; import { router as movingUIRouter } from './endpoints/moving-ui.js'; import { router as imagesRouter } from './endpoints/images.js'; import { router as quickRepliesRouter } from './endpoints/quick-replies.js'; @@ -128,6 +131,9 @@ export function redirectDeprecatedEndpoints(app) { * @param {import('express').Express} app The Express app to use */ export function setupPrivateEndpoints(app) { + app.use('/', userDataRouter); + app.use('/api/users', usersPrivateRouter); + app.use('/api/users', usersAdminRouter); app.use('/api/moving-ui', movingUIRouter); app.use('/api/images', imagesRouter); app.use('/api/quick-replies', quickRepliesRouter); @@ -274,13 +280,10 @@ export class ServerStartup { /** * Handles the case where the server failed to start on one or both protocols. - * @param {boolean} v6Failed If the server failed to start on IPv6 - * @param {boolean} v4Failed If the server failed to start on IPv4 - * @param {boolean} useIPv6 If use IPv6 - * @param {boolean} useIPv4 If use IPv4 + * @param {ServerStartupResult} result The results of the server startup * @returns {void} */ - #handleServerListenFail(v6Failed, v4Failed, useIPv6, useIPv4) { + #handleServerListenFail({ v6Failed, v4Failed, useIPv6, useIPv4 }) { if (v6Failed && !useIPv4) { console.error(color.red('fatal error: Failed to start server on IPv6 and IPv4 disabled')); process.exit(1); @@ -353,7 +356,8 @@ export class ServerStartup { } const [v6Failed, v4Failed] = await this.#startHTTPorHTTPS(useIPv6, useIPv4); - this.#handleServerListenFail(v6Failed, v4Failed, useIPv6, useIPv4); - return { v6Failed, v4Failed, useIPv6, useIPv4 }; + const result = { v6Failed, v4Failed, useIPv6, useIPv4 }; + this.#handleServerListenFail(result); + return result; } }