-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
87 lines (76 loc) · 3.36 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using Microsoft.Extensions.Hosting;
using Altinn.ApiClients.Maskinporten.Services;
using Altinn.ApiClients.Maskinporten.Extensions;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Console;
using Digdir.Oed.FeedPoller;
using Digdir.Oed.FeedPoller.Interfaces;
using Digdir.Oed.FeedPoller.Services;
using Digdir.Oed.FeedPoller.Settings;
var host = new HostBuilder()
.ConfigureAppConfiguration((hostContext, config) =>
{
config
.AddEnvironmentVariables()
.AddJsonFile("worker.json");
if (hostContext.HostingEnvironment.IsDevelopment())
{
config.AddUserSecrets<FeedPoller>(false);
}
})
// TODO Workaround for https://github.com/Azure/azure-functions-dotnet-worker/issues/1090
.ConfigureLogging(loggingBuilder =>
{
if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Development")
{
loggingBuilder.AddSimpleConsole(options =>
{
options.ColorBehavior = LoggerColorBehavior.Enabled;
options.SingleLine = true;
});
}
})
.ConfigureFunctionsWorkerDefaults(builder =>
{
builder
// Using preview package Microsoft.Azure.Functions.Worker.ApplicationInsights, see https://github.com/Azure/azure-functions-dotnet-worker/pull/944
// Requires APPLICATIONINSIGHTS_CONNECTION_STRING being set. Note that host.json logging settings will have to be replicated to worker.json
.AddApplicationInsights()
.AddApplicationInsightsLogger();
}, options =>
{
//options.Serializer = new NewtonsoftJsonObjectSerializer();
})
.ConfigureServices((context, services) =>
{
services.Configure<OedSettings>(context.Configuration.GetSection("OedSettings"));
services.AddMaskinportenHttpClient<SettingsJwkClientDefinition>(Constants.DaHttpClient, context.Configuration.GetSection("MaskinportenSettings"),
clientDefinition =>
{
clientDefinition.ClientSettings.Scope = ScopesByPrefix("domstol", clientDefinition.ClientSettings.Scope);
clientDefinition.ClientSettings.OverwriteAuthorizationHeader = false;
});
services.AddMaskinportenHttpClient<SettingsJwkClientDefinition>(Constants.EventsHttpClient, context.Configuration.GetSection("MaskinportenSettings"),
clientDefinition =>
{
clientDefinition.ClientSettings.Scope = ScopesByPrefix("altinn", clientDefinition.ClientSettings.Scope);
});
// Use if Redis not available locally
//services.AddDistributedMemoryCache();
services.AddStackExchangeRedisCache(option =>
{
option.Configuration = context.Configuration.GetConnectionString("Redis");
});
services.AddSingleton<IDaEventFeedProxyService, DaEventFeedProxyService>();
})
.Build();
host.Run();
string ScopesByPrefix(string prefix, string scopes)
{
var scopeList = scopes.Split(' ', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
var filteredScopes = scopeList.Where(scope => scope.StartsWith($"{prefix}:")).ToList();
return string.Join(' ', filteredScopes);
}