mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: Implement --baseUrl flag.
Closes https://github.com/solid/community-server/issues/372
This commit is contained in:
@@ -4,6 +4,7 @@ import type { LoaderProperties } from 'componentsjs';
|
||||
import { Loader } from 'componentsjs';
|
||||
import yargs from 'yargs';
|
||||
import { getLoggerFor } from '../logging/LogUtil';
|
||||
import { ensureTrailingSlash } from '../util/PathUtil';
|
||||
import type { Setup } from './Setup';
|
||||
|
||||
const logger = getLoggerFor('CliRunner');
|
||||
@@ -30,11 +31,12 @@ export const runCli = function({
|
||||
const { argv: params } = yargs(argv.slice(2))
|
||||
.usage('node ./bin/server.js [args]')
|
||||
.options({
|
||||
port: { type: 'number', alias: 'p', default: 3000 },
|
||||
baseUrl: { type: 'string', alias: 'b' },
|
||||
config: { type: 'string', alias: 'c' },
|
||||
loggingLevel: { type: 'string', alias: 'l', default: 'info' },
|
||||
port: { type: 'number', alias: 'p', default: 3000 },
|
||||
rootFilePath: { type: 'string', alias: 'f' },
|
||||
sparqlEndpoint: { type: 'string', alias: 's' },
|
||||
loggingLevel: { type: 'string', alias: 'l', default: 'info' },
|
||||
})
|
||||
.help();
|
||||
|
||||
@@ -50,16 +52,17 @@ export const runCli = function({
|
||||
const setup: Setup = await loader
|
||||
.instantiateFromUrl('urn:solid-server:default', configPath, undefined, {
|
||||
variables: {
|
||||
'urn:solid-server:default:variable:baseUrl':
|
||||
params.baseUrl ? ensureTrailingSlash(params.baseUrl) : `http://localhost:${params.port}/`,
|
||||
'urn:solid-server:default:variable:loggingLevel': params.loggingLevel,
|
||||
'urn:solid-server:default:variable:port': params.port,
|
||||
'urn:solid-server:default:variable:base': `http://localhost:${params.port}/`,
|
||||
'urn:solid-server:default:variable:rootFilePath': params.rootFilePath ?? process.cwd(),
|
||||
'urn:solid-server:default:variable:sparqlEndpoint': params.sparqlEndpoint,
|
||||
'urn:solid-server:default:variable:loggingLevel': params.loggingLevel,
|
||||
},
|
||||
}) as Setup;
|
||||
return await setup.setup();
|
||||
})().then((base: string): void => {
|
||||
logger.info(`Running at ${base}`);
|
||||
})().then((baseUrl: string): void => {
|
||||
logger.info(`Running at ${baseUrl}`);
|
||||
}).catch((error): void => {
|
||||
// This is the only time we can *not* use the logger to print error messages, as dependency injection has failed.
|
||||
stderr.write(`${error}\n`);
|
||||
|
||||
@@ -2,26 +2,16 @@ import { addHeader } from '../../util/HeaderUtil';
|
||||
import { HttpHandler } from '../HttpHandler';
|
||||
import type { HttpResponse } from '../HttpResponse';
|
||||
|
||||
interface WebSocketSettings {
|
||||
hostname?: string;
|
||||
port?: number;
|
||||
protocol?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handler that advertises a WebSocket through the Updates-Via header.
|
||||
*/
|
||||
export class WebSocketAdvertiser extends HttpHandler {
|
||||
private readonly socketUrl: string;
|
||||
|
||||
public constructor(settings: WebSocketSettings = {}) {
|
||||
public constructor(baseUrl: string) {
|
||||
super();
|
||||
const { hostname = 'localhost', port = 80, protocol = 'ws:' } = settings;
|
||||
const secure = /^(?:https|wss)/u.test(protocol);
|
||||
const socketUrl = new URL(`${secure ? 'wss' : 'ws'}://${hostname}:${port}/`);
|
||||
if (socketUrl.hostname !== hostname) {
|
||||
throw new Error(`Invalid hostname: ${hostname}`);
|
||||
}
|
||||
const socketUrl = new URL(baseUrl);
|
||||
socketUrl.protocol = /^(?:http|ws):/u.test(baseUrl) ? 'ws:' : 'wss:';
|
||||
this.socketUrl = socketUrl.href;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user