Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deployment-time configuration of front end application #1

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
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
14 changes: 11 additions & 3 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { NgModule, APP_INITIALIZER } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { AppConfigService } from './providers/app-config.service';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { HeaderComponent } from './components/header/header.component';
import { FooterComponent } from './components/footer/footer.component';

export function initConfig(appConfig: AppConfigService) {
return () => appConfig.loadConfig();
}

@NgModule({
declarations: [
Expand All @@ -20,7 +23,12 @@ import { FooterComponent } from './components/footer/footer.component';
BrowserAnimationsModule
],

providers: [],
providers: [{
provide: APP_INITIALIZER,
useFactory: initConfig,
deps: [AppConfigService],
multi: true,
}],
bootstrap: [AppComponent]
})
export class AppModule { }
12 changes: 12 additions & 0 deletions src/app/providers/app-config.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { TestBed } from '@angular/core/testing';

import { AppConfigService } from './app-config.service';

describe('AppConfigService', () => {
beforeEach(() => TestBed.configureTestingModule({}));

it('should be created', () => {
const service: AppConfigService = TestBed.get(AppConfigService);
expect(service).toBeTruthy();
});
});
30 changes: 30 additions & 0 deletions src/app/providers/app-config.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Injectable } from "@angular/core";
import { HttpClient } from "@angular/common/http";
import { environment } from '../../environments/environment';

@Injectable({
providedIn: "root",
})
export class AppConfigService {
private config: any = environment;
constructor(private http: HttpClient) { }

public async loadConfig() {
if ("configFile" in environment) {
try {
const config = await this.http
.get(environment["configFile"])
.toPromise();
this.config = Object.assign({}, environment, config);
console.log(this.config);
}
catch (err) {
console.error(err);
}
}
}

getConfig() {
return this.config;
}
}
12 changes: 8 additions & 4 deletions src/app/services/data.service.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { Injectable } from '@angular/core';
import { Injectable, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
import { HttpClient, HttpResponse } from '@angular/common/http';
import { environment } from '../../environments/environment';
import { AppConfigService } from '../providers/app-config.service';

@Injectable({
providedIn: 'root'
})

export class DataService {
apiUrl = environment.apiBaseUrl;

constructor(private httpClient: HttpClient) { }
apiUrl = null;

constructor(private httpClient: HttpClient, private config: AppConfigService) {
this.apiUrl = this.config.getConfig().apiBaseUrl;
console.log(this.config.getConfig());
}

processImage(fileLocation: string): Observable<HttpResponse<any>> {
const url = this.apiUrl.concat('/processImage');
Expand Down
3 changes: 3 additions & 0 deletions src/assets/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"apiBaseUrl": "https://jenkins-master-deephealth-unix01.ing.unimore.it/backend"
}
3 changes: 2 additions & 1 deletion src/environments/environment.prod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const environment = {
production: true,
apiBaseUrl: 'https://jenkins-master-deephealth-unix01.ing.unimore.it/backend'
// Use 'config.json' asset to configure API url at runtime
configFile: '/assets/config.json'
};
6 changes: 5 additions & 1 deletion src/environments/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
export const environment = {
production: false,
// apiBaseUrl: 'http://localhost:5000'
apiBaseUrl: 'https://jenkins-master-deephealth-unix01.ing.unimore.it/backend'
apiBaseUrl: 'https://jenkins-master-deephealth-unix01.ing.unimore.it/backend',
// 'configFile' is a path to an asset JSON file containing additional
// settings that can be set without rebuild. They can be accessed
// through the 'AppConfigService'
// configFile: '/assets/config.json'
};

/*
Expand Down