forked from microsoft/winget-cli-restsource
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStartup.cs
More file actions
78 lines (67 loc) · 3.61 KB
/
Startup.cs
File metadata and controls
78 lines (67 loc) · 3.61 KB
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
// -----------------------------------------------------------------------
// <copyright file="Startup.cs" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation. Licensed under the MIT License.
// </copyright>
// -----------------------------------------------------------------------
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.WinGet.RestSource.Functions.Constants;
[assembly:
Microsoft.Azure.Functions.Extensions.DependencyInjection.FunctionsStartup(
typeof(Microsoft.WinGet.RestSource.Functions.Startup))]
namespace Microsoft.WinGet.RestSource.Functions
{
using System;
using System.IO;
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.WinGet.RestSource.AppConfig;
using Microsoft.WinGet.RestSource.Cosmos;
using Microsoft.WinGet.RestSource.Factories;
using Microsoft.WinGet.RestSource.Helpers;
using Microsoft.WinGet.RestSource.Interfaces;
using Microsoft.WinGet.RestSource.Utils.Common;
using Microsoft.WinGet.RestSource.Utils.Constants;
/// <summary>
/// Azure function startup class.
/// </summary>
public class Startup : FunctionsStartup
{
/// <inheritdoc />
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddHttpClient();
string endpoint = Environment.GetEnvironmentVariable(CosmosConnectionConstants.CosmosAccountEndpointSetting) ?? throw new InvalidDataException();
string readOnlyKey = Environment.GetEnvironmentVariable(CosmosConnectionConstants.CosmosReadWriteKeySetting) ?? throw new InvalidDataException();
string readWriteKey = Environment.GetEnvironmentVariable(CosmosConnectionConstants.CosmosReadWriteKeySetting) ?? throw new InvalidDataException();
string databaseId = Environment.GetEnvironmentVariable(CosmosConnectionConstants.DatabaseNameSetting) ?? throw new InvalidDataException();
string containerId = Environment.GetEnvironmentVariable(CosmosConnectionConstants.ContainerNameSetting) ?? throw new InvalidDataException();
builder.Services.AddSingleton<IWinGetAppConfig>(sp => WinGetAppConfig.Instance);
builder.Services.AddSingleton<IApiDataStore, CosmosDataStore>(
sp => new CosmosDataStore(
sp.GetRequiredService<ILogger<CosmosDataStore>>(),
endpoint,
readWriteKey,
readOnlyKey,
databaseId,
containerId));
builder.Services.AddSingleton<IRebuild>((s) => RebuildFactory.InitializeRebuildInstance());
builder.Services.AddSingleton<IUpdate>((s) => UpdateFactory.InitializeUpdateInstance());
builder.Services.AddSingleton<IRestSourceTriggerFunction>((s) => new RestSourceTriggerFunctions(
ApiConstants.AzFuncRestSourceEndpoint));
InjectTelemetryConfiguration(builder);
}
private static void InjectTelemetryConfiguration(IFunctionsHostBuilder builder)
{
builder.Services.AddSingleton<TelemetryConfiguration>(sp =>
{
var key = AzureFunctionEnvironment.AppInsightsInstrumentationKey;
if (!string.IsNullOrWhiteSpace(key))
{
return new TelemetryConfiguration(key);
}
return new TelemetryConfiguration();
});
}
}
}