Skip to content

Commit 34613a6

Browse files
committed
[NAE-2116] Frontend remote configuration
- pull application configuration (nae.json) from backend (gateway which redirects to admin node) - load new pulled configuration inside configuration.service - add environment setup (env.js, environment.ts, index.html)
1 parent acc9a97 commit 34613a6

File tree

13 files changed

+111
-16
lines changed

13 files changed

+111
-16
lines changed

projects/nae-example-app/src/app/app.module.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {BrowserModule} from '@angular/platform-browser';
2-
import {Injector, NgModule} from '@angular/core';
2+
import {APP_INITIALIZER, Injector, NgModule} from '@angular/core';
33
import {AppRoutingModule} from './app-routing.module';
44
import {AppComponent} from './app.component';
55
import {
@@ -19,7 +19,8 @@ import {
1919
ViewService,
2020
ProfileModule,
2121
Dashboard,
22-
FrontActionModule, NAE_ASYNC_RENDERING_CONFIGURATION
22+
FrontActionModule, NAE_ASYNC_RENDERING_CONFIGURATION,
23+
loadConfiguration
2324
} from '@netgrif/components-core';
2425
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
2526
import {FlexLayoutModule, FlexModule} from '@ngbracket/ngx-layout';
@@ -244,10 +245,18 @@ export function HttpLoaderFactory(http: HttpClient) {
244245
DialogComponentsModule,
245246
FrontActionModule
246247
],
247-
providers: [{
248-
provide: ConfigurationService,
249-
useClass: NaeExampleAppConfigurationService
250-
},
248+
providers: [
249+
ResourceProvider,
250+
{
251+
provide: ConfigurationService,
252+
useClass: NaeExampleAppConfigurationService
253+
},
254+
{
255+
provide: APP_INITIALIZER,
256+
useFactory: loadConfiguration,
257+
deps: [ConfigurationService],
258+
multi: true
259+
},
251260
{provide: NAE_SNACKBAR_VERTICAL_POSITION, useValue: SnackBarVerticalPosition.TOP},
252261
{provide: NAE_SNACKBAR_HORIZONTAL_POSITION, useValue: SnackBarHorizontalPosition.LEFT},
253262
{provide: NAE_ASYNC_RENDERING_CONFIGURATION, useValue: {
@@ -257,7 +266,6 @@ export function HttpLoaderFactory(http: HttpClient) {
257266
enableAsyncRenderingForNewFields: true,
258267
enableAsyncRenderingOnTaskExpand: true
259268
}},
260-
ResourceProvider,
261269
TranslateService,
262270
TranslatePipe,
263271
TranslateStore,
Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
import {Injectable} from '@angular/core';
2-
import {ConfigurationService} from '@netgrif/components-core';
3-
import {NetgrifApplicationEngine} from '@netgrif/components-core/';
2+
import {ApplicationConfiguration, ConfigurationService, ResourceProvider, NetgrifApplicationEngine} from '@netgrif/components-core';
43
import {default as naeConfig} from '../../../../nae.json';
4+
import {environment} from '../environments/environment';
55

66
@Injectable({
77
providedIn: 'root'
88
})
99
export class NaeExampleAppConfigurationService extends ConfigurationService {
10-
constructor() {
11-
super(naeConfig as unknown as NetgrifApplicationEngine);
10+
constructor(private resourceProvider: ResourceProvider) {
11+
const applicationConfig: ApplicationConfiguration = {
12+
application: environment.application_identifier,
13+
type: environment.type_identifier,
14+
gateway_url: environment.gateway_url,
15+
resolve_configuration: environment.resolve_configuration
16+
};
17+
super(naeConfig as unknown as NetgrifApplicationEngine, resourceProvider, applicationConfig);
1218
}
1319
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
(function(window) {
2+
window["env"] = window["env"] || {};
3+
4+
window['env']['resolve_configuration'] = false;
5+
window['env']['gateway_url'] = 'http://localhost:8800/api';
6+
window['env']['application_identifier'] = 'nae';
7+
window['env']['type_identifier'] = 'default';
8+
})(this);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
(function(window) {
2+
window["env"] = window["env"] || {};
3+
4+
window['env']['resolve_configuration'] = "${RESOLVE_CONFIGURATION}";
5+
window['env']['gateway_url'] = "${GATEWAY_URL}";
6+
window['env']['application_identifier'] = "${APPLICATION_IDENTIFIER}";
7+
window['env']['type_identifier'] = "${TYPE_IDENTIFIER}";
8+
})(this);

projects/nae-example-app/src/environments/environment.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
// The list of file replacements can be found in `angular.json`.
44

55
export const environment = {
6-
production: false
6+
production: false,
7+
resolve_configuration: window['env']['resolve_configuration'] || false,
8+
gateway_url: window['env']['gateway_url'] || 'http://localhost:8800/api',
9+
application_identifier: window['env']['application_identifier'] || 'nae',
10+
type_identifier: window['env']['type_identifier'] || 'default'
711
};
812

913
/*

projects/nae-example-app/src/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<base href="/">
77
<meta name="viewport" content="width=device-width, initial-scale=1">
88
<link rel="icon" type="image/x-icon" href="favicon.ico">
9+
<script src="./assets/env.js"></script>
910
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500&display=swap" rel="stylesheet">
1011
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
1112
</head>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import {NetgrifApplicationEngine} from '../../commons/schema';
2+
3+
export interface ApplicationConfiguration {
4+
application: string;
5+
type: string;
6+
resolve_configuration?: boolean;
7+
gateway_url?: string;
8+
properties?: NetgrifApplicationEngine;
9+
}

projects/netgrif-components-core/src/lib/configuration/configuration.service.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
11
import {NetgrifApplicationEngine, Services, View, Views} from '../../commons/schema';
22
import {Observable, of} from 'rxjs';
3+
import {ResourceProvider} from '../resources/resource-provider.service';
4+
import {ApplicationConfiguration} from './application-configuration';
5+
36

47
export abstract class ConfigurationService {
58

6-
private readonly _dataFieldConfiguration: Services['dataFields'];
9+
private _dataFieldConfiguration: Services['dataFields'];
10+
11+
private readonly APPLICATION_CONFIG: ApplicationConfiguration;
12+
13+
protected constructor(protected configuration: NetgrifApplicationEngine,
14+
protected _resourceProvider: ResourceProvider,
15+
protected _applicationConfiguration: ApplicationConfiguration) {
16+
this.initialize();
17+
18+
this.APPLICATION_CONFIG = _applicationConfiguration;
19+
}
720

8-
protected constructor(protected configuration: NetgrifApplicationEngine) {
21+
private initialize(): void {
922
this.resolveEndpointURLs();
1023
this._dataFieldConfiguration = this.getConfigurationSubtree(['services', 'dataFields']);
1124
}
@@ -246,6 +259,23 @@ export abstract class ConfigurationService {
246259
return config.providers.auth.address + config.providers.auth.endpoints[endpointKey];
247260
}
248261

262+
public loadConfiguration(): Promise<any> {
263+
if (!this.APPLICATION_CONFIG.resolve_configuration) {
264+
return null;
265+
}
266+
return this._resourceProvider.get$(
267+
`/frontend-config/public/${this.APPLICATION_CONFIG.application}/${this.APPLICATION_CONFIG.type}`,
268+
this.APPLICATION_CONFIG.gateway_url
269+
).toPromise()
270+
.then((data: ApplicationConfiguration) => {
271+
if (!data || !data.properties) {
272+
return;
273+
}
274+
this.configuration = data.properties as NetgrifApplicationEngine;
275+
this.initialize();
276+
});
277+
}
278+
249279
private createConfigurationCopy(): any {
250280
return this.deepCopy(this.configuration);
251281
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/* SERVICES */
22
export * from './configuration.service';
3+
export * from './application-configuration';
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {ConfigurationService} from '../configuration/configuration.service';
2+
3+
4+
export function loadConfiguration(configurationService: ConfigurationService): () => Promise<any> | void {
5+
return (): Promise<any> => {
6+
return configurationService.loadConfiguration();
7+
}
8+
}

0 commit comments

Comments
 (0)