Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 22 additions & 49 deletions src/config-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,89 +4,62 @@ import application from './vcap-application.js';
import oauth2 from './oauth2.js';

const DEFAULT_PROFILE = 'default';

const NAME = env.name || application.getName() || 'application';
const PROFILES = env.profiles || DEFAULT_PROFILE;
const LABEL = env.label || 'main';

function configServerUri() {
return services.getUri('p.config-server');
function buildConfigUri() {
return `${services.getUri('p.config-server')}/${NAME}/${PROFILES}/${LABEL}`;
}

// Loads properties from the config-server for the given profiles.
async function loadProperties(profiles) {
const url = `${configServerUri()}/${NAME}/${profiles}/${env.label}`.replace(
/\/$/,
'',
);

console.log(`Loading properties from ${url}`);

async function makeHttpRequest(url, accept) {
const token = await oauth2.getAccessToken();
const response = await fetch(`${url}`, {
headers: {
Authorization: `Bearer ${token}`,
Accept: 'application/json',
Accept: accept,
},
});

if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
throw new Error(`HTTP error! status: ${response.status}, url: ${url}`);
}

// Merge the property sources into a single properties object.
return (await response.json()).propertySources
.map((it) => it.source)
.reduce((acc, source) => ({ ...source, ...acc }), {});
return response;
}

// Loads properties from the config-server.
const load = async () => {
// If given profile is not the default profile, load the default properties first.
let defaultProperties = {};
if (env.profiles !== DEFAULT_PROFILE) {
defaultProperties = await loadProperties(DEFAULT_PROFILE);
}
const url = `${buildConfigUri()}`;

console.log(`Loading properties from ${url}`);

// Merge the default properties with the profile-specific properties.
return { ...defaultProperties, ...(await loadProperties(env.profiles)) };
const response = await makeHttpRequest(`${url}`, 'application/json');

// Merge the property sources into a single properties object.
return (await response.json()).propertySources
.map((it) => it.source)
.reduce((acc, source) => ({ ...source, ...acc }), {});
};

// Loads a text resource from the config-server.
const loadTextResource = async (name) => {
const url = `${configServerUri()}/${NAME}/${env.profiles}/${env.label || 'main'}/${name}`;
const url = `${buildConfigUri()}/${name}`;

console.log(`Loading text resource from ${url}`);

const token = await oauth2.getAccessToken();
const response = await fetch(`${url}`, {
headers: {
Authorization: `Bearer ${token}`,
Accept: 'plain/text',
},
});

if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const response = await makeHttpRequest(`${url}`, 'text/plain');

return await response.text();
};

// Loads a binary resource from the config-server.
const loadBinaryResource = async (name) => {
const url = `${configServerUri()}/${NAME}/${env.profiles}/${env.label || 'main'}/${name}`;
const url = `${buildConfigUri()}/${name}`;

console.log(`Loading binary resource from ${url}`);

const token = await oauth2.getAccessToken();
const response = await fetch(`${url}`, {
headers: {
Authorization: `Bearer ${token}`,
Accept: 'application/octet-stream',
},
});

if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const response = await makeHttpRequest(`${url}`, 'application/octet-stream');

return response.blob();
};
Expand Down