Skip to content

Commit 74cdd97

Browse files
author
Rajkumar Balakrishnan
committed
GetConfig from API
1 parent 59bb87f commit 74cdd97

File tree

6 files changed

+98
-6
lines changed

6 files changed

+98
-6
lines changed

web/src/signalr-web/SignalRMiddleware/EventApp/src/app/app.module.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import { DataService } from './services/data.service';
1212
import { HubService } from './services/hub.service'
1313
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
1414
import { AppInsightsService } from './services/app-insights.service';
15+
import { APP_INITIALIZER } from '@angular/core';
16+
import { ConfigurationService } from "./configuration/configuration.service";
1517

1618
const appRoutes: Routes = [
1719
{
@@ -25,6 +27,12 @@ const appRoutes: Routes = [
2527
}
2628
]
2729

30+
const appInitializerFn = (appConfig: ConfigurationService) => {
31+
return () => {
32+
return appConfig.loadConfig();
33+
};
34+
};
35+
2836
@NgModule({
2937

3038
declarations: [
@@ -38,7 +46,14 @@ const appRoutes: Routes = [
3846
NgbModule.forRoot(),
3947
RouterModule.forRoot(appRoutes, {enableTracing: true})
4048
],
41-
providers: [DataService, HubService, AppInsightsService],
49+
providers: [DataService, HubService, AppInsightsService,ConfigurationService,
50+
{
51+
provide: APP_INITIALIZER,
52+
useFactory: appInitializerFn,
53+
multi: true,
54+
deps: [ConfigurationService]
55+
}],
4256
bootstrap: [AppComponent]
57+
4358
})
4459
export class AppModule { }
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Injectable } from '@angular/core';
2+
import { HttpClient } from '@angular/common/http';
3+
4+
@Injectable()
5+
export class ConfigurationService {
6+
7+
private configuration: IAppInsightsConfiguration;
8+
constructor(private http: HttpClient) { }
9+
10+
loadConfig() {
11+
return this.http.get<IAppInsightsConfiguration>('/api/applicationsetting')
12+
.toPromise()
13+
.then(result => {
14+
this.configuration = <IAppInsightsConfiguration>(result);
15+
}, error => console.error(error));
16+
}
17+
18+
get apiAddress() {
19+
return this.configuration.key;
20+
}
21+
22+
23+
}
24+
export interface IAppInsightsConfiguration {
25+
key: string;
26+
}
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import {ConfigurationService} from '../app/configuration/configuration.service'
2+
13
export const environment = {
2-
production: true,
3-
appInsights: {
4-
instrumentationKey: "3e0ab8f5-c675-448c-8a15-08ba5ec45317" /*The web middleware should return the key to the Angular Environment*/
4+
production: true,
5+
appInsights: {
6+
instrumentationKey: ""/*The web middleware should return the key to the Angular Environment*/
57
}
68
};
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Mvc;
6+
using Microsoft.Extensions.Logging;
7+
using SignalRMiddleware.Services;
8+
9+
namespace SignalRMiddleware.Controllers
10+
{
11+
[Produces("application/json")]
12+
[Route("api/[controller]")]
13+
public class ApplicationSettingController : Controller
14+
{
15+
private readonly ILogger _logger;
16+
private readonly ICRUDService _crudService;
17+
18+
public ApplicationSettingController(ILogger<ApplicationSettingController> logger, ICRUDService crudService)
19+
{
20+
_logger = logger;
21+
_crudService = crudService;
22+
}
23+
24+
[HttpGet]
25+
public IActionResult GetApplicationInsightsInstrumentationKey()
26+
{
27+
try
28+
{
29+
return Ok(new { key = _crudService.GetAppInsightskey() });
30+
}
31+
catch (Exception e)
32+
{
33+
_logger.LogError("Get AppInsightsKey Failed " + e);
34+
return BadRequest(e.Message);
35+
}
36+
}
37+
}
38+
}

web/src/signalr-web/SignalRMiddleware/SignalRMiddleware/Services/CRUDService.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,17 @@ public interface ICRUDService
3434
Task<dynamic> ListTextAsync(string userId);
3535
Task<dynamic> UpdateTextAsync(string userId, string textId,string requestBody);
3636
Task<dynamic> DeleteTextAsync(string userId, string textId);
37+
38+
/** ApplicationSetting Interfaces **/
39+
40+
string GetAppInsightskey();
3741
}
3842
public class CRUDService : ICRUDService
3943
{
4044
static HttpClient client = new HttpClient();
4145
const string jsonMimeType = "application/json";
4246
static string functionsApiProxy = Environment.GetEnvironmentVariable("FUNCTION_API_PROXY_ROOT", EnvironmentVariableTarget.Process);
43-
47+
4448
/** HTTP Methods */
4549
private static async Task<string> HttpMethodAsync(string verb,string url, HttpContent httpContent)
4650
{
@@ -205,5 +209,12 @@ public async Task<dynamic> DeleteTextAsync(string userId, string textId)
205209
string url = functionsApiProxy + "/text/" + textId + "/?userId=" + userId;
206210
return await CRUDService.HttpMethodAsync("DELETE", url, null);
207211
}
212+
213+
/** AppSettings API */
214+
215+
public string GetAppInsightskey()
216+
{
217+
return Environment.GetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY") ?? "NOKEYAVAILABLE";
218+
}
208219
}
209220
}

web/src/signalr-web/SignalRMiddleware/SignalRMiddleware/SignalRMiddleware.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<ItemGroup>
2121
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.3.0" />
2222
<PackageReference Include="Microsoft.AspNetCore.App" />
23-
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.0" PrivateAssets="All" />
23+
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.9" PrivateAssets="All" />
2424
</ItemGroup>
2525

2626
<ItemGroup>

0 commit comments

Comments
 (0)