2020-06-11 18:13:47 +02:00
|
|
|
import {Inject, Injectable, Optional} from '@angular/core';
|
2019-06-03 15:20:36 +02:00
|
|
|
import {HttpClient} from "@angular/common/http";
|
2020-06-11 15:53:48 +02:00
|
|
|
import {BehaviorSubject, of} from 'rxjs';
|
2019-06-03 15:20:36 +02:00
|
|
|
|
|
|
|
import {Request} from 'express';
|
|
|
|
import {REQUEST} from '@nguniversal/express-engine/tokens';
|
2018-02-05 14:14:59 +01:00
|
|
|
|
2020-06-11 15:53:48 +02:00
|
|
|
import {EnvProperties} from './env-properties';
|
|
|
|
import {properties} from "../../../../environments/environment";
|
2019-06-03 15:20:36 +02:00
|
|
|
|
2018-02-05 14:14:59 +01:00
|
|
|
@Injectable()
|
|
|
|
export class EnvironmentSpecificService {
|
2020-06-12 16:37:40 +02:00
|
|
|
|
2018-02-05 14:14:59 +01:00
|
|
|
public envSpecific: EnvProperties;
|
2019-03-01 12:29:52 +01:00
|
|
|
public domain: string;
|
2018-02-05 14:14:59 +01:00
|
|
|
private envSpecificSubject: BehaviorSubject<EnvProperties> = new BehaviorSubject<EnvProperties>(null);
|
2019-12-03 11:45:28 +01:00
|
|
|
private propertiesPath = "/assets/env-properties.json?v=2";
|
2020-06-12 16:37:40 +02:00
|
|
|
|
2020-06-11 15:53:48 +02:00
|
|
|
constructor(private http: HttpClient, @Optional() @Inject(REQUEST) private request: Request) {
|
2020-08-11 14:39:55 +02:00
|
|
|
properties.domain = "https://" + this.getDomain();
|
2018-02-05 14:14:59 +01:00
|
|
|
}
|
2020-06-11 15:53:48 +02:00
|
|
|
|
2018-02-05 14:14:59 +01:00
|
|
|
public loadEnvironment() {
|
2020-06-12 16:37:40 +02:00
|
|
|
this.envSpecific = properties;
|
|
|
|
return Promise.resolve(this.envSpecific);
|
2018-02-05 14:14:59 +01:00
|
|
|
}
|
2020-06-12 16:37:40 +02:00
|
|
|
|
2018-03-15 10:53:07 +01:00
|
|
|
public subscribeEnvironment() {
|
2020-06-12 16:37:40 +02:00
|
|
|
// Only want to do this once - if root page is revisited, it calls this again.
|
|
|
|
if (this.envSpecific === null || this.envSpecific === undefined) {
|
|
|
|
this.envSpecific = properties;
|
|
|
|
}
|
|
|
|
// console.log('subscribeEnvironment: already loaded ');
|
|
|
|
return of(this.envSpecific);
|
2018-03-15 10:53:07 +01:00
|
|
|
}
|
2020-06-12 16:37:40 +02:00
|
|
|
|
|
|
|
getDomain() {
|
|
|
|
var domain = "";
|
2019-03-01 12:29:52 +01:00
|
|
|
if (typeof document == 'undefined') {
|
2019-06-03 15:20:36 +02:00
|
|
|
domain = this.request.get('host').split(":")[0];
|
2020-06-12 16:37:40 +02:00
|
|
|
} else {
|
2019-06-03 15:20:36 +02:00
|
|
|
domain = document.location.hostname;
|
2020-06-12 16:37:40 +02:00
|
|
|
}
|
|
|
|
return domain;
|
2019-03-01 12:29:52 +01:00
|
|
|
}
|
2020-06-12 16:37:40 +02:00
|
|
|
|
2018-02-05 14:14:59 +01:00
|
|
|
public setEnvProperties(es: EnvProperties) {
|
|
|
|
// This has already been set so bail out.
|
|
|
|
if (es === null || es === undefined) {
|
2020-06-12 16:37:40 +02:00
|
|
|
return;
|
2018-02-05 14:14:59 +01:00
|
|
|
}
|
|
|
|
this.envSpecific = es;
|
|
|
|
if (this.envSpecificSubject) {
|
2020-06-12 16:37:40 +02:00
|
|
|
this.envSpecificSubject.next(this.envSpecific);
|
2018-02-05 14:14:59 +01:00
|
|
|
}
|
|
|
|
}
|
2020-06-12 16:37:40 +02:00
|
|
|
|
2018-02-05 14:14:59 +01:00
|
|
|
/*
|
|
|
|
Call this if you want to know when EnvProperties is set.
|
|
|
|
*/
|
|
|
|
public subscribe(caller: any, callback: (caller: any, es: EnvProperties) => void) {
|
2020-06-12 16:37:40 +02:00
|
|
|
this.envSpecificSubject
|
|
|
|
.subscribe((es) => {
|
|
|
|
if (es === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
callback(caller, es);
|
|
|
|
});
|
2018-02-05 14:14:59 +01:00
|
|
|
}
|
|
|
|
}
|