Skip to content
Snippets Groups Projects
Commit cbd6057f authored by OZGCloud's avatar OZGCloud
Browse files

OZG-130 make environment injectable

parent e2d97496
Branches
No related tags found
No related merge requests found
Showing
with 120 additions and 48 deletions
<ng-container *ngIf="apiRoot$ | async as apiRoot"> <ng-container *ngIf="apiRoot$ | async as apiRoot">
<goofy-client-header <goofy-client-header
[darkMode]="darkMode$ | async" [navigationCollapse]="navigationCollapse$ | async" [darkMode]="darkMode$ | async"
(toggleMenuEvent)="toggleNavigation($event)" (darkModeEmitter)="changeColorMode($event)"> [navigationCollapse]="navigationCollapse$ | async"
(toggleMenuEvent)="toggleNavigation($event)"
(darkModeEmitter)="changeColorMode($event)"
>
</goofy-client-header> </goofy-client-header>
<div class="container"> <div class="container">
...@@ -12,7 +15,9 @@ ...@@ -12,7 +15,9 @@
</section> </section>
</div> </div>
</ng-container> </ng-container>
<!--
<ng-container *ngIf="apiRootStateless$ | async as apiRootStateless"> <ng-container *ngIf="apiRootStateless$ | async as apiRootStateless">
{{apiRootStateless | json}} {{apiRootStateless | json}}
</ng-container> </ng-container>
-->
import { registerLocaleData } from '@angular/common';
import { HttpClientModule } from '@angular/common/http'; import { HttpClientModule } from '@angular/common/http';
import { LOCALE_ID, NgModule } from '@angular/core';
import localeDe from '@angular/common/locales/de'; import localeDe from '@angular/common/locales/de';
import { LOCALE_ID, NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser'; import { BrowserModule } from '@angular/platform-browser';
import { ApiRootSharedModule } from '@goofy-client/api-root-shared';
import { EnvironmentSharedModule } from '@goofy-client/environment-shared';
import { NgxsReduxDevtoolsPluginModule } from '@ngxs/devtools-plugin';
import { NgxsLoggerPluginModule } from '@ngxs/logger-plugin';
import { NgxsModule } from '@ngxs/store';
import { AppComponent } from './app.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { RouterModule } from '@angular/router'; import { RouterModule } from '@angular/router';
import { ApiRootSharedModule } from '@goofy-client/api-root-shared';
import { EnvironmentModule } from '@goofy-client/environment-shared';
import { NavigationModule } from '@goofy-client/navigation';
import { UiModule } from '@goofy-client/ui'; import { UiModule } from '@goofy-client/ui';
import { VorgangModule } from '@goofy-client/vorgang'; import { VorgangModule } from '@goofy-client/vorgang';
import { registerLocaleData } from '@angular/common';
import { NgxsRouterPluginModule } from '@ngxs/router-plugin';
import { NgxsActionsExecutingModule } from '@ngxs-labs/actions-executing'; import { NgxsActionsExecutingModule } from '@ngxs-labs/actions-executing';
import { NavigationModule } from '@goofy-client/navigation'; import { NgxsReduxDevtoolsPluginModule } from '@ngxs/devtools-plugin';
import { NgxsLoggerPluginModule } from '@ngxs/logger-plugin';
import { NgxsRouterPluginModule } from '@ngxs/router-plugin';
import { NgxsModule } from '@ngxs/store';
import { AppComponent } from './app.component';
import { AppState } from './shared/app.state'; import { AppState } from './shared/app.state';
registerLocaleData(localeDe); registerLocaleData(localeDe);
...@@ -32,7 +32,7 @@ registerLocaleData(localeDe); ...@@ -32,7 +32,7 @@ registerLocaleData(localeDe);
NgxsRouterPluginModule.forRoot(), NgxsRouterPluginModule.forRoot(),
NgxsActionsExecutingModule.forRoot(), NgxsActionsExecutingModule.forRoot(),
HttpClientModule, HttpClientModule,
EnvironmentSharedModule, EnvironmentModule,
ApiRootSharedModule, ApiRootSharedModule,
BrowserAnimationsModule, BrowserAnimationsModule,
RouterModule, RouterModule,
......
import { ResourceUri } from '@ngxp/rest'; import { ResourceUri } from '@ngxp/rest';
import { Environment } from 'libs/environment-shared/src/lib/environment.model';
export class LoadApiRootAction { export class LoadApiRootAction {
static readonly type = '[System ApiRoot] Load'; static readonly type = '[System ApiRoot] Load';
constructor(public remoteHost: ResourceUri){} constructor() { }
} }
\ No newline at end of file
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { Action, NgxsOnInit, Selector, State, StateContext } from '@ngxs/store'; import { Action, NgxsOnInit, Selector, State, StateContext } from '@ngxs/store';
import { Environment } from 'libs/environment-shared/src/lib/environment-shared.model';
import { getEnvironmentFactory } from 'libs/environment-shared/src/lib/environment-shared.util';
import { tap } from 'rxjs/operators'; import { tap } from 'rxjs/operators';
import { LoadApiRootAction } from './api-root-shared.actions'; import { LoadApiRootAction } from './api-root-shared.actions';
import { ApiRootResource } from './api-root-shared.model'; import { ApiRootResource } from './api-root-shared.model';
import { ApiRootService } from './api-root-shared.service'; import { ApiRootService } from './api-root-shared.service';
import { ApiRootRepository } from './api-root.repository';
const defaults: ApiRootStateModel = { const defaults: ApiRootStateModel = {
apiRoot: null, apiRoot: null,
...@@ -23,15 +22,19 @@ export class ApiRootState implements NgxsOnInit{ ...@@ -23,15 +22,19 @@ export class ApiRootState implements NgxsOnInit{
return state.apiRoot; return state.apiRoot;
} }
constructor(private service: ApiRootService) {} constructor(private service: ApiRootService, private repository: ApiRootRepository) { }
ngxsOnInit(context: StateContext<any>): void { ngxsOnInit(context: StateContext<any>): void {
getEnvironmentFactory().then((environment: Environment) => context.dispatch(new LoadApiRootAction(environment.remoteHost))); // context.dispatch(new LoadApiRootAction(env))
// getEnvironmentFactory().then((environment: Environment) => context.dispatch(new LoadApiRootAction(environment.remoteHost)));
context.dispatch(new LoadApiRootAction());
} }
@Action(LoadApiRootAction) @Action(LoadApiRootAction)
loadApiRoot(context: StateContext<ApiRootStateModel>, action: LoadApiRootAction) { loadApiRoot(context: StateContext<ApiRootStateModel>, action: LoadApiRootAction) {
return this.service.get(action.remoteHost).pipe( return this.repository.loadApiRoot().pipe(
// return this.service.get(action.remoteHost).pipe(
tap((apiRoot) => { tap((apiRoot) => {
context.patchState({ apiRoot }); context.patchState({ apiRoot });
}) })
......
import { Mock, mock, useFromMock } from '@goofy-client/test-utils';
import { HttpService } from '@ngxp/rest';
import { Environment } from 'libs/environment-shared/src/lib/environment.model';
import { ApiRootRepository } from "./api-root.repository"
import { createEnvironment } from "../../../environment-shared/test/environment";
var repository: ApiRootRepository;
var httpService: Mock<HttpService>;
const environment: Environment = createEnvironment();
beforeEach(() => {
httpService = mock(HttpService);
repository = new ApiRootRepository(useFromMock(httpService), environment);
})
describe('api root repository', () => {
describe('loadApiRoot', () => {
it('should call httpService', () => {
repository.loadApiRoot();
expect(httpService.get).toHaveBeenCalledWith(environment.remoteHost);
})
})
})
\ No newline at end of file
import { Injectable } from '@angular/core'; import { Inject, Injectable } from '@angular/core';
import { HttpService } from '@ngxp/rest'; import { HttpService } from '@ngxp/rest';
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';
import { ApiRootResource } from './api-root-shared.model'; import { ApiRootResource } from './api-root-shared.model';
import { ENVIRONMENT_CONFIG } from '@goofy-client/environment-shared';
import { Environment } from 'libs/environment-shared/src/lib/environment.model';
@Injectable() @Injectable()
export class ApiRootRepository { export class ApiRootRepository {
constructor( constructor(
private httpService: HttpService, private httpService: HttpService,
//@Inject(ENVIRONMENT_CONFIG) private environmentConfig @Inject(ENVIRONMENT_CONFIG) private environmentConfig: Environment
) { } ) { }
public loadApiRoot(): Observable<ApiRootResource> { public loadApiRoot(): Observable<ApiRootResource> {
return this.httpService.get('http://localhost:4300/api'); return this.httpService.get(this.environmentConfig.remoteHost);
} }
} }
\ No newline at end of file
export * from './lib/environment-shared.module'; export * from './lib/environment.module';
export * from './lib/environment-shared.service'; export * from './lib/environment.service';
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { Action, NgxsOnInit, State, StateContext } from '@ngxs/store'; import { Action, NgxsOnInit, State, StateContext } from '@ngxs/store';
import { SetEnvironmentAction } from './environment-shared.actions'; import { SetEnvironmentAction } from './environment-shared.actions';
import { Environment } from './environment-shared.model'; import { Environment } from './environment.model';
import { getEnvironmentFactory } from './environment-shared.util'; import { getEnvironmentFactory } from './environment.service';
const defaults: EnvironmentStateModel = { const defaults: EnvironmentStateModel = {
environment: null environment: null
...@@ -24,8 +24,7 @@ export class EnvironmentState implements NgxsOnInit { ...@@ -24,8 +24,7 @@ export class EnvironmentState implements NgxsOnInit {
@Action(SetEnvironmentAction) @Action(SetEnvironmentAction)
loadEnvironment(context: StateContext<EnvironmentStateModel>) { loadEnvironment(context: StateContext<EnvironmentStateModel>) {
return getEnvironmentFactory().then((environment: Environment) => { const environment = getEnvironmentFactory();
context.patchState({ environment }); context.patchState({ environment });
})
} }
} }
\ No newline at end of file
export function getEnvironmentFactory(): any {
return window['__env__'];
}
\ No newline at end of file
...@@ -3,12 +3,19 @@ import { NgModule } from '@angular/core'; ...@@ -3,12 +3,19 @@ import { NgModule } from '@angular/core';
import { RestModule } from '@ngxp/rest'; import { RestModule } from '@ngxp/rest';
import { NgxsModule } from '@ngxs/store'; import { NgxsModule } from '@ngxs/store';
import { EnvironmentState } from './environment-shared.state'; import { EnvironmentState } from './environment-shared.state';
import { ENVIRONMENT_CONFIG, getEnvironmentFactory } from './environment.service';
@NgModule({ @NgModule({
imports: [ imports: [
CommonModule, CommonModule,
NgxsModule.forFeature([EnvironmentState]), NgxsModule.forFeature([EnvironmentState]),
RestModule RestModule
],
providers: [
{
provide: ENVIRONMENT_CONFIG,
useFactory: getEnvironmentFactory
}
] ]
}) })
export class EnvironmentSharedModule {} export class EnvironmentModule { }
import { InjectionToken } from '@angular/core'; import { InjectionToken } from '@angular/core';
import { getBaseUrl } from '@goofy-client/tech-shared'; import { getBaseUrl } from '@goofy-client/tech-shared';
import { Environment } from './environment.model';
export const ENVIRONMENT_CONFIG = new InjectionToken('environmentConfig'); export const ENVIRONMENT_CONFIG = new InjectionToken('environmentConfig');
export function getEnvironmentFactory(): Environment {
return window['__env__'];
}
export async function loadEnvironment(url?: string): Promise<any> { export async function loadEnvironment(url?: string): Promise<any> {
const envUrl = url ? url : `${getBaseUrl()}api/environment`; const envUrl = url ? url : `${getBaseUrl()}api/environment`;
const headers = new Headers({ 'X-Client': 'web' }); const headers = new Headers({ 'X-Client': 'web' });
return window.fetch(envUrl, { headers }).then(response => { return window.fetch(envUrl, { headers })
const env = response.json(); .then(response => response.json())
.then(env => {
window['__env__'] = env; window['__env__'] = env;
return env; return env;
}) });
} }
\ No newline at end of file
import * as faker from 'faker';
import { cloneDeep } from 'lodash-es';
import { Environment } from '../src/lib/environment.model';
const baseUrl = faker.internet.url();
const environment: Environment = {
production: false,
remoteHost: baseUrl
};
export function createEnvironment(): Environment {
return cloneDeep(environment);
}
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
"ng": "nx", "ng": "nx",
"postinstall": "node ./decorate-angular-cli.js && ngcc --properties es2015 browser module main --first-only --create-ivy-entry-points", "postinstall": "node ./decorate-angular-cli.js && ngcc --properties es2015 browser module main --first-only --create-ivy-entry-points",
"start": "run-s \"nx -- serve --port 4300 --disable-host-check --proxy-config proxy.conf.json\" --", "start": "run-s \"nx -- serve --port 4300 --disable-host-check --proxy-config proxy.conf.json\" --",
"start:devbe": "run-s \"nx -- serve --port 4300 --disable-host-check --proxy-config proxy.dev.conf.json\" --",
"build": "ng build", "build": "ng build",
"test": "jest test a", "test": "jest test a",
"test:cov": "jest --coverage", "test:cov": "jest --coverage",
......
{
"/api": {
"target": {
"host": "kiel.dev.ozg-sh.de",
"protocol": "https:"
},
"secure": false,
"logLevel": "debug"
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment