Modify default server url from environments
This commit is contained in:
@@ -18,6 +18,7 @@ import {
|
||||
User
|
||||
} from '../models/index';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import { environment } from '../../../environments/environment';
|
||||
|
||||
/**
|
||||
* A configured server endpoint that the user can connect to.
|
||||
@@ -44,18 +45,49 @@ const ENDPOINTS_STORAGE_KEY = 'metoyou_server_endpoints';
|
||||
/** Timeout (ms) for server health-check and alternative-endpoint pings. */
|
||||
const HEALTH_CHECK_TIMEOUT_MS = 5000;
|
||||
|
||||
/**
|
||||
* Derive the default server URL from the current page protocol so that
|
||||
* SSL/TLS is matched automatically.
|
||||
*/
|
||||
function buildDefaultServerUrl(): string {
|
||||
if (typeof window !== 'undefined' && window.location) {
|
||||
const protocol = window.location.protocol === 'https:' ? 'https' : 'http';
|
||||
function getDefaultHttpProtocol(): 'http' | 'https' {
|
||||
return typeof window !== 'undefined' && window.location?.protocol === 'https:'
|
||||
? 'https'
|
||||
: 'http';
|
||||
}
|
||||
|
||||
return `${protocol}://localhost:3001`;
|
||||
function normaliseDefaultServerUrl(rawUrl: string): string {
|
||||
let cleaned = rawUrl.trim();
|
||||
|
||||
if (!cleaned)
|
||||
return '';
|
||||
|
||||
if (cleaned.toLowerCase().startsWith('ws://')) {
|
||||
cleaned = `http://${cleaned.slice(5)}`;
|
||||
} else if (cleaned.toLowerCase().startsWith('wss://')) {
|
||||
cleaned = `https://${cleaned.slice(6)}`;
|
||||
} else if (cleaned.startsWith('//')) {
|
||||
cleaned = `${getDefaultHttpProtocol()}:${cleaned}`;
|
||||
} else if (!/^[a-z][a-z\d+.-]*:\/\//i.test(cleaned)) {
|
||||
cleaned = `${getDefaultHttpProtocol()}://${cleaned}`;
|
||||
}
|
||||
|
||||
return 'http://localhost:3001';
|
||||
cleaned = cleaned.replace(/\/+$/, '');
|
||||
|
||||
if (cleaned.toLowerCase().endsWith('/api')) {
|
||||
cleaned = cleaned.slice(0, -4);
|
||||
}
|
||||
|
||||
return cleaned;
|
||||
}
|
||||
|
||||
/**
|
||||
* Derive the default server URL from the environment when provided,
|
||||
* otherwise match the current page protocol automatically.
|
||||
*/
|
||||
function buildDefaultServerUrl(): string {
|
||||
const configuredUrl = environment.defaultServerUrl?.trim();
|
||||
|
||||
if (configuredUrl) {
|
||||
return normaliseDefaultServerUrl(configuredUrl);
|
||||
}
|
||||
|
||||
return `${getDefaultHttpProtocol()}://localhost:3001`;
|
||||
}
|
||||
|
||||
/** Blueprint for the built-in default endpoint. */
|
||||
@@ -241,11 +273,8 @@ export class ServerDirectoryService {
|
||||
getWebSocketUrl(): string {
|
||||
const active = this.activeServer();
|
||||
|
||||
if (!active) {
|
||||
const protocol = (typeof window !== 'undefined' && window.location?.protocol === 'https:') ? 'wss' : 'ws';
|
||||
|
||||
return `${protocol}://localhost:3001`;
|
||||
}
|
||||
if (!active)
|
||||
return buildDefaultServerUrl().replace(/^http/, 'ws');
|
||||
|
||||
return active.url.replace(/^http/, 'ws');
|
||||
}
|
||||
@@ -533,7 +562,7 @@ export class ServerDirectoryService {
|
||||
});
|
||||
}
|
||||
|
||||
/** Load endpoints from localStorage, migrating protocol if needed. */
|
||||
/** Load endpoints from localStorage, syncing the built-in default endpoint if needed. */
|
||||
private loadEndpoints(): void {
|
||||
const stored = localStorage.getItem(ENDPOINTS_STORAGE_KEY);
|
||||
|
||||
@@ -550,16 +579,12 @@ export class ServerDirectoryService {
|
||||
endpoints[0].isActive = true;
|
||||
}
|
||||
|
||||
// Migrate localhost entries to match the current page protocol
|
||||
const expectedProtocol =
|
||||
typeof window !== 'undefined' && window.location?.protocol === 'https:'
|
||||
? 'https'
|
||||
: 'http';
|
||||
const defaultServerUrl = buildDefaultServerUrl();
|
||||
|
||||
endpoints = endpoints.map((endpoint) => {
|
||||
if (endpoint.isDefault && /^https?:\/\/localhost:\d+$/.test(endpoint.url)) {
|
||||
if (endpoint.isDefault) {
|
||||
return { ...endpoint,
|
||||
url: endpoint.url.replace(/^https?/, expectedProtocol) };
|
||||
url: defaultServerUrl };
|
||||
}
|
||||
|
||||
return endpoint;
|
||||
|
||||
Reference in New Issue
Block a user