Modify default server url from environments

This commit is contained in:
2026-03-07 16:18:54 +01:00
parent 0fd7d8ce27
commit 4bbb5cd174
4 changed files with 54 additions and 28 deletions

View File

@@ -16,9 +16,6 @@ export interface LoginResponse {
displayName: string; displayName: string;
} }
/** Fallback API base URL used when no server endpoint is configured. */
const DEFAULT_API_BASE = 'http://localhost:3001/api';
/** /**
* Handles user authentication (login and registration) against a * Handles user authentication (login and registration) against a
* configurable back-end server. * configurable back-end server.
@@ -50,7 +47,9 @@ export class AuthService {
const activeEndpoint = endpoint ?? this.serverDirectory.activeServer(); const activeEndpoint = endpoint ?? this.serverDirectory.activeServer();
return activeEndpoint ? `${activeEndpoint.url}/api` : DEFAULT_API_BASE; return activeEndpoint
? `${activeEndpoint.url}/api`
: this.serverDirectory.getApiBaseUrl();
} }
/** /**

View File

@@ -18,6 +18,7 @@ import {
User User
} from '../models/index'; } from '../models/index';
import { v4 as uuidv4 } from 'uuid'; import { v4 as uuidv4 } from 'uuid';
import { environment } from '../../../environments/environment';
/** /**
* A configured server endpoint that the user can connect to. * 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. */ /** Timeout (ms) for server health-check and alternative-endpoint pings. */
const HEALTH_CHECK_TIMEOUT_MS = 5000; const HEALTH_CHECK_TIMEOUT_MS = 5000;
/** function getDefaultHttpProtocol(): 'http' | 'https' {
* Derive the default server URL from the current page protocol so that return typeof window !== 'undefined' && window.location?.protocol === 'https:'
* SSL/TLS is matched automatically. ? 'https'
*/ : 'http';
function buildDefaultServerUrl(): string { }
if (typeof window !== 'undefined' && window.location) {
const protocol = 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. */ /** Blueprint for the built-in default endpoint. */
@@ -241,11 +273,8 @@ export class ServerDirectoryService {
getWebSocketUrl(): string { getWebSocketUrl(): string {
const active = this.activeServer(); const active = this.activeServer();
if (!active) { if (!active)
const protocol = (typeof window !== 'undefined' && window.location?.protocol === 'https:') ? 'wss' : 'ws'; return buildDefaultServerUrl().replace(/^http/, 'ws');
return `${protocol}://localhost:3001`;
}
return active.url.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 { private loadEndpoints(): void {
const stored = localStorage.getItem(ENDPOINTS_STORAGE_KEY); const stored = localStorage.getItem(ENDPOINTS_STORAGE_KEY);
@@ -550,16 +579,12 @@ export class ServerDirectoryService {
endpoints[0].isActive = true; endpoints[0].isActive = true;
} }
// Migrate localhost entries to match the current page protocol const defaultServerUrl = buildDefaultServerUrl();
const expectedProtocol =
typeof window !== 'undefined' && window.location?.protocol === 'https:'
? 'https'
: 'http';
endpoints = endpoints.map((endpoint) => { endpoints = endpoints.map((endpoint) => {
if (endpoint.isDefault && /^https?:\/\/localhost:\d+$/.test(endpoint.url)) { if (endpoint.isDefault) {
return { ...endpoint, return { ...endpoint,
url: endpoint.url.replace(/^https?/, expectedProtocol) }; url: defaultServerUrl };
} }
return endpoint; return endpoint;

View File

@@ -1,5 +1,6 @@
export const environment = { export const environment = {
production: true, production: true,
serverUrl: 'https://your-server.com/api', serverUrl: 'https://your-server.com/api',
signalingUrl: 'wss://your-server.com/signaling' signalingUrl: 'wss://your-server.com/signaling',
defaultServerUrl: ''
}; };

View File

@@ -1,5 +1,6 @@
export const environment = { export const environment = {
production: false, production: false,
serverUrl: 'http://localhost:3000/api', serverUrl: 'http://localhost:3000/api',
signalingUrl: 'ws://localhost:3001' signalingUrl: 'ws://localhost:3001',
defaultServerUrl: 'https://46.59.68.77:3001'
}; };