@@ -4,89 +4,62 @@ import application from './vcap-application.js';
44import oauth2 from './oauth2.js' ;
55
66const DEFAULT_PROFILE = 'default' ;
7+
78const NAME = env . name || application . getName ( ) || 'application' ;
9+ const PROFILES = env . profiles || DEFAULT_PROFILE ;
10+ const LABEL = env . label || 'main' ;
811
9- function configServerUri ( ) {
10- return services . getUri ( 'p.config-server' ) ;
12+ function buildConfigUri ( ) {
13+ return ` ${ services . getUri ( 'p.config-server' ) } / ${ NAME } / ${ PROFILES } / ${ LABEL } ` ;
1114}
1215
13- // Loads properties from the config-server for the given profiles.
14- async function loadProperties ( profiles ) {
15- const url = `${ configServerUri ( ) } /${ NAME } /${ profiles } /${ env . label } ` . replace (
16- / \/ $ / ,
17- '' ,
18- ) ;
19-
20- console . log ( `Loading properties from ${ url } ` ) ;
21-
16+ async function makeHttpRequest ( url , accept ) {
2217 const token = await oauth2 . getAccessToken ( ) ;
2318 const response = await fetch ( `${ url } ` , {
2419 headers : {
2520 Authorization : `Bearer ${ token } ` ,
26- Accept : 'application/json' ,
21+ Accept : accept ,
2722 } ,
2823 } ) ;
2924
3025 if ( ! response . ok ) {
31- throw new Error ( `HTTP error! Status : ${ response . status } ` ) ;
26+ throw new Error ( `HTTP error! status : ${ response . status } , url: ${ url } ` ) ;
3227 }
33-
34- // Merge the property sources into a single properties object.
35- return ( await response . json ( ) ) . propertySources
36- . map ( ( it ) => it . source )
37- . reduce ( ( acc , source ) => ( { ...source , ...acc } ) , { } ) ;
28+ return response ;
3829}
3930
4031// Loads properties from the config-server.
4132const load = async ( ) => {
42- // If given profile is not the default profile, load the default properties first.
43- let defaultProperties = { } ;
44- if ( env . profiles !== DEFAULT_PROFILE ) {
45- defaultProperties = await loadProperties ( DEFAULT_PROFILE ) ;
46- }
33+ const url = `${ buildConfigUri ( ) } ` ;
34+
35+ console . log ( `Loading properties from ${ url } ` ) ;
4736
48- // Merge the default properties with the profile-specific properties.
49- return { ...defaultProperties , ...( await loadProperties ( env . profiles ) ) } ;
37+ const response = await makeHttpRequest ( `${ url } ` , 'application/json' ) ;
38+
39+ // Merge the property sources into a single properties object.
40+ return ( await response . json ( ) ) . propertySources
41+ . map ( ( it ) => it . source )
42+ . reduce ( ( acc , source ) => ( { ...source , ...acc } ) , { } ) ;
5043} ;
5144
5245// Loads a text resource from the config-server.
5346const loadTextResource = async ( name ) => {
54- const url = `${ configServerUri ( ) } / ${ NAME } / ${ env . profiles } / ${ env . label || 'main' } /${ name } ` ;
47+ const url = `${ buildConfigUri ( ) } /${ name } ` ;
5548
5649 console . log ( `Loading text resource from ${ url } ` ) ;
5750
58- const token = await oauth2 . getAccessToken ( ) ;
59- const response = await fetch ( `${ url } ` , {
60- headers : {
61- Authorization : `Bearer ${ token } ` ,
62- Accept : 'plain/text' ,
63- } ,
64- } ) ;
65-
66- if ( ! response . ok ) {
67- throw new Error ( `HTTP error! Status: ${ response . status } ` ) ;
68- }
51+ const response = await makeHttpRequest ( `${ url } ` , 'text/plain' ) ;
6952
7053 return await response . text ( ) ;
7154} ;
7255
7356// Loads a binary resource from the config-server.
7457const loadBinaryResource = async ( name ) => {
75- const url = `${ configServerUri ( ) } / ${ NAME } / ${ env . profiles } / ${ env . label || 'main' } /${ name } ` ;
58+ const url = `${ buildConfigUri ( ) } /${ name } ` ;
7659
7760 console . log ( `Loading binary resource from ${ url } ` ) ;
7861
79- const token = await oauth2 . getAccessToken ( ) ;
80- const response = await fetch ( `${ url } ` , {
81- headers : {
82- Authorization : `Bearer ${ token } ` ,
83- Accept : 'application/octet-stream' ,
84- } ,
85- } ) ;
86-
87- if ( ! response . ok ) {
88- throw new Error ( `HTTP error! Status: ${ response . status } ` ) ;
89- }
62+ const response = await makeHttpRequest ( `${ url } ` , 'application/octet-stream' ) ;
9063
9164 return response . blob ( ) ;
9265} ;
0 commit comments