Skip to content

Commit f20895b

Browse files
#250 - Added appBaseHref to get application root address
1 parent 3e76450 commit f20895b

File tree

6 files changed

+94
-66
lines changed

6 files changed

+94
-66
lines changed

angular/src/AppPreBootstrap.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import * as moment from 'moment';
1+
import * as moment from 'moment';
22
import { AppConsts } from '@shared/AppConsts';
33
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
44
import { Type, CompilerOptions, NgModuleRef } from '@angular/core';
5+
import { environment } from './environments/environment';
56

67
export class AppPreBootstrap {
78

8-
static run(callback: () => void): void {
9-
AppPreBootstrap.getApplicationConfig(() => {
9+
static run(appRootUrl: string, callback: () => void): void {
10+
AppPreBootstrap.getApplicationConfig(appRootUrl, () => {
1011
AppPreBootstrap.getUserConfiguration(callback);
1112
});
1213
}
@@ -15,9 +16,9 @@ export class AppPreBootstrap {
1516
return platformBrowserDynamic().bootstrapModule(moduleType, compilerOptions);
1617
}
1718

18-
private static getApplicationConfig(callback: () => void) {
19+
private static getApplicationConfig(appRootUrl: string, callback: () => void) {
1920
return abp.ajax({
20-
url: '/assets/appconfig.json',
21+
url: appRootUrl + 'assets/' + environment.appConfig,
2122
method: 'GET',
2223
headers: {
2324
'Abp.TenantId': abp.multiTenancy.getTenantIdCookie()
@@ -65,4 +66,4 @@ export class AppPreBootstrap {
6566
callback();
6667
});
6768
}
68-
}
69+
}
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
// "Hot Module Replacement" enabled environment
1+
// "Hot Module Replacement" enabled environment
22

33
export const environment = {
44
production: false,
5-
hmr: true
6-
};
5+
hmr: true,
6+
appConfig: 'appconfig.json'
7+
};

angular/src/environments/environment.prod.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22

33
export const environment = {
44
production: true,
5-
hmr: false
6-
};
5+
hmr: false,
6+
appConfig: 'appconfig.json'
7+
};

angular/src/environments/environment.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55

66
export const environment = {
77
production: false,
8-
hmr: false
9-
};
8+
hmr: false,
9+
appConfig: 'appconfig.json'
10+
};

angular/src/root.module.ts

Lines changed: 75 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { BrowserModule } from '@angular/platform-browser';
22
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
33
import { NgModule, Injector, APP_INITIALIZER, LOCALE_ID } from '@angular/core';
4+
import { PlatformLocation } from '@angular/common';
45

56
import { AbpModule } from '@abp/abp.module';
67
import { AbpHttpInterceptor } from '@abp/abpHttpInterceptor';
@@ -17,69 +18,91 @@ import { API_BASE_URL } from '@shared/service-proxies/service-proxies';
1718
import { RootComponent } from './root.component';
1819
import { AppPreBootstrap } from './AppPreBootstrap';
1920
import { ModalModule } from 'ngx-bootstrap';
20-
import { HttpClientModule, HttpResponse } from '@angular/common/http';
21-
22-
export function appInitializerFactory(injector: Injector) {
23-
return () => {
24-
25-
abp.ui.setBusy();
26-
return new Promise<boolean>((resolve, reject) => {
27-
AppPreBootstrap.run(() => {
28-
abp.event.trigger('abp.dynamicScriptsInitialized');
29-
var appSessionService: AppSessionService = injector.get(AppSessionService);
30-
appSessionService.init().then(
31-
(result) => {
32-
abp.ui.clearBusy();
33-
resolve(result);
34-
},
35-
(err) => {
36-
abp.ui.clearBusy();
37-
reject(err);
38-
}
39-
);
40-
});
41-
});
42-
}
21+
import { HttpClientModule } from '@angular/common/http';
22+
23+
export function appInitializerFactory(injector: Injector,
24+
platformLocation: PlatformLocation) {
25+
return () => {
26+
27+
abp.ui.setBusy();
28+
return new Promise<boolean>((resolve, reject) => {
29+
AppConsts.appBaseHref = getBaseHref(platformLocation);
30+
let appBaseUrl = getDocumentOrigin() + AppConsts.appBaseHref;
31+
32+
AppPreBootstrap.run(appBaseUrl, () => {
33+
abp.event.trigger('abp.dynamicScriptsInitialized');
34+
var appSessionService: AppSessionService = injector.get(AppSessionService);
35+
appSessionService.init().then(
36+
(result) => {
37+
abp.ui.clearBusy();
38+
resolve(result);
39+
},
40+
(err) => {
41+
abp.ui.clearBusy();
42+
reject(err);
43+
}
44+
);
45+
});
46+
});
47+
}
4348
}
4449

4550
export function getRemoteServiceBaseUrl(): string {
46-
return AppConsts.remoteServiceBaseUrl;
51+
return AppConsts.remoteServiceBaseUrl;
4752
}
4853

4954
export function getCurrentLanguage(): string {
5055
return abp.localization.currentLanguage.name;
5156
}
5257

5358
@NgModule({
54-
imports: [
55-
BrowserModule,
56-
BrowserAnimationsModule,
57-
SharedModule.forRoot(),
58-
ModalModule.forRoot(),
59-
AbpModule,
60-
ServiceProxyModule,
61-
RootRoutingModule,
62-
HttpClientModule
63-
],
64-
declarations: [
65-
RootComponent
66-
],
67-
providers: [
68-
{ provide: HTTP_INTERCEPTORS, useClass: AbpHttpInterceptor, multi: true },
69-
{ provide: API_BASE_URL, useFactory: getRemoteServiceBaseUrl },
70-
{
71-
provide: APP_INITIALIZER,
72-
useFactory: appInitializerFactory,
73-
deps: [Injector],
74-
multi: true
75-
},
76-
{
77-
provide: LOCALE_ID,
78-
useFactory: getCurrentLanguage
79-
}
80-
],
81-
bootstrap: [RootComponent]
59+
imports: [
60+
BrowserModule,
61+
BrowserAnimationsModule,
62+
SharedModule.forRoot(),
63+
ModalModule.forRoot(),
64+
AbpModule,
65+
ServiceProxyModule,
66+
RootRoutingModule,
67+
HttpClientModule
68+
],
69+
declarations: [
70+
RootComponent
71+
],
72+
providers: [
73+
{ provide: HTTP_INTERCEPTORS, useClass: AbpHttpInterceptor, multi: true },
74+
{ provide: API_BASE_URL, useFactory: getRemoteServiceBaseUrl },
75+
{
76+
provide: APP_INITIALIZER,
77+
useFactory: appInitializerFactory,
78+
deps: [Injector, PlatformLocation],
79+
multi: true
80+
},
81+
{
82+
provide: LOCALE_ID,
83+
useFactory: getCurrentLanguage
84+
}
85+
],
86+
bootstrap: [RootComponent]
8287
})
88+
8389
export class RootModule {
8490

8591
}
92+
93+
export function getBaseHref(platformLocation: PlatformLocation): string {
94+
var baseUrl = platformLocation.getBaseHrefFromDOM();
95+
if (baseUrl) {
96+
return baseUrl;
97+
}
98+
99+
return '/';
100+
}
101+
102+
function getDocumentOrigin() {
103+
if (!document.location.origin) {
104+
return document.location.protocol + "//" + document.location.hostname + (document.location.port ? ':' + document.location.port : '');
105+
}
106+
107+
return document.location.origin;
108+
}

angular/src/shared/AppConsts.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
export class AppConsts {
1+
export class AppConsts {
22

33
static remoteServiceBaseUrl: string;
44
static appBaseUrl: string;
5+
static appBaseHref: string; // returns angular's base-href parameter value if used during the publish
56

67
static readonly userManagement = {
78
defaultAdminUserName: 'admin'

0 commit comments

Comments
 (0)