52a75fd8cb
Build and Push Docker Image / build (push) Successful in 56s
Licence Check / Licence compatibility and copyright header verification (push) Successful in 1m18s
CI / Security audit (push) Successful in 2m9s
CI / Swagger Validation & Coverage (push) Successful in 2m25s
CI / Tests & coverage (push) Successful in 2m44s
- Update openapi.yaml to use single placeholder server URL - Add swagger-server-detection.js to auto-detect current server URL from window.location - Configure protocol, host, and port detection based on browser connection - Fallback to placeholder URL if detection fails - Include detection script in both app.js and index.js Swagger UI configurations - /api/swagger.json endpoint returns static placeholder for external consumers
46 lines
1.6 KiB
JavaScript
46 lines
1.6 KiB
JavaScript
// Swagger UI server URL auto-detection
|
|
// Automatically sets the server URL to match the current instance
|
|
(function() {
|
|
window.addEventListener('load', function() {
|
|
// Wait for Swagger UI to initialize
|
|
setTimeout(function() {
|
|
try {
|
|
// Detect current URL from browser
|
|
const protocol = window.location.protocol;
|
|
const host = window.location.host;
|
|
const detectedUrl = `${protocol}//${host}`;
|
|
|
|
// Get Swagger UI instance
|
|
const ui = window.ui;
|
|
if (!ui) {
|
|
console.warn('Swagger UI not found, cannot set server URL');
|
|
return;
|
|
}
|
|
|
|
// Get the spec servers
|
|
const spec = ui.specSelectors.specJson();
|
|
const servers = spec.getIn(['servers']);
|
|
|
|
if (!servers || servers.size === 0) {
|
|
console.warn('No servers defined in OpenAPI spec');
|
|
return;
|
|
}
|
|
|
|
// Update the first server with the detected URL
|
|
const updatedServers = servers.setIn([0, 'url'], detectedUrl);
|
|
|
|
// Apply the updated servers to the spec
|
|
const updatedSpec = spec.set('servers', updatedServers);
|
|
ui.specActions.updateSpec(updatedSpec.toJS());
|
|
|
|
// Also set the selected server in Swagger UI state
|
|
ui.specActions.setSelectedServer(updatedServers.get(0).get('url'));
|
|
|
|
console.log('Swagger UI server URL set to:', detectedUrl);
|
|
} catch (error) {
|
|
console.error('Failed to auto-detect Swagger UI server URL:', error);
|
|
}
|
|
}, 100); // Small delay to ensure Swagger UI is initialized
|
|
});
|
|
})();
|