forked from backstage/backstage
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request backstage#1113 from spotify/rugvip/config
package/cli,core: add static configuration loading for frontend
- Loading branch information
Showing
18 changed files
with
245 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
app: | ||
title: Backstage Example App | ||
baseUrl: http://localhost:3000 | ||
|
||
backend: | ||
baseUrl: http://localhost:7000 | ||
|
||
organization: | ||
name: Spotify |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
* Copyright 2020 Spotify AB | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
export type { AppConfig } from './types'; | ||
export { loadConfig } from './loaders'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright 2020 Spotify AB | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { AppConfig } from './types'; | ||
import fs from 'fs-extra'; | ||
import yaml from 'yaml'; | ||
import { paths } from '../paths'; | ||
|
||
type LoadConfigOptions = { | ||
// Config path, defaults to app-config.yaml in project root | ||
configPath?: string; | ||
}; | ||
|
||
export async function loadConfig( | ||
options: LoadConfigOptions = {}, | ||
): Promise<AppConfig[]> { | ||
// TODO: We'll want this to be a bit more elaborate, probably adding configs for | ||
// specific env, and maybe local config for plugins. | ||
const { configPath = paths.resolveTargetRoot('app-config.yaml') } = options; | ||
|
||
try { | ||
const configYaml = await fs.readFile(configPath, 'utf8'); | ||
const config = yaml.parse(configYaml); | ||
return [config]; | ||
} catch (error) { | ||
throw new Error(`Failed to read static configuration file, ${error}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* | ||
* Copyright 2020 Spotify AB | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
export type AppConfig = any; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
app: | ||
title: Scaffolded Backstage App | ||
|
||
organization: | ||
name: Acme Corporation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright 2020 Spotify AB | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { defaultConfigLoader } from './createApp'; | ||
|
||
describe('defaultConfigLoader', () => { | ||
afterEach(() => { | ||
delete process.env.APP_CONFIG; | ||
}); | ||
|
||
it('loads static config', async () => { | ||
Object.defineProperty(process.env, 'APP_CONFIG', { | ||
configurable: true, | ||
value: [{ my: 'config' }, { my: 'override-config' }] as any, | ||
}); | ||
const configs = await defaultConfigLoader(); | ||
expect(configs).toEqual([{ my: 'config' }, { my: 'override-config' }]); | ||
}); | ||
|
||
it('loads runtime config', async () => { | ||
Object.defineProperty(process.env, 'APP_CONFIG', { | ||
configurable: true, | ||
value: [{ my: 'override-config' }, { my: 'config' }] as any, | ||
}); | ||
const configs = await (defaultConfigLoader as any)( | ||
'{"my":"runtime-config"}', | ||
); | ||
expect(configs).toEqual([ | ||
{ my: 'runtime-config' }, | ||
{ my: 'override-config' }, | ||
{ my: 'config' }, | ||
]); | ||
}); | ||
|
||
it('fails to load invalid missing config', async () => { | ||
await expect(defaultConfigLoader()).rejects.toThrow( | ||
'No static configuration provided', | ||
); | ||
}); | ||
|
||
it('fails to load invalid static config', async () => { | ||
Object.defineProperty(process.env, 'APP_CONFIG', { | ||
configurable: true, | ||
value: { my: 'invalid-config' } as any, | ||
}); | ||
await expect(defaultConfigLoader()).rejects.toThrow( | ||
'Static configuration has invalid format', | ||
); | ||
}); | ||
|
||
it('fails to load bad runtime config', async () => { | ||
Object.defineProperty(process.env, 'APP_CONFIG', { | ||
configurable: true, | ||
value: [{ my: 'config' }] as any, | ||
}); | ||
|
||
await expect((defaultConfigLoader as any)('}')).rejects.toThrow( | ||
'Failed to load runtime configuration, SyntaxError: Unexpected token } in JSON at position 0', | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.