-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
52 lines (41 loc) · 1.39 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
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Mvc;
using ServiceContracts;
using Services;
using WeatherApp.ActionFilters;
using Azure.Identity;
using Azure.Security.KeyVault.Secrets;
using WeatherApp;
var builder = WebApplication.CreateBuilder(args);
//string vaultUri = builder.Configuration.GetValue<string>("");
string? vaultUri = Environment.GetEnvironmentVariable("KEYVAULT_URI");
if (string.IsNullOrEmpty(vaultUri))
{
Console.WriteLine("KEYVAULT_URI is not set or is empty.");
vaultUri = Config.Uri;
}
else
{
Console.WriteLine($"KEYVAULT_URI: {vaultUri}");
}
if (string.IsNullOrEmpty(vaultUri))
{
throw new ArgumentNullException(nameof(vaultUri), "VaultUri cannot be null or empty.");
}
var keyVaultEndpoint = new Uri(vaultUri);
builder.Configuration.AddAzureKeyVault(keyVaultEndpoint, new DefaultAzureCredential());
builder.Services.AddSingleton(new SecretClient(new Uri(vaultUri), new DefaultAzureCredential()));
builder.Services.AddControllersWithViews();
builder.Services.AddSingleton<IWeatherService, WeatherService>();
builder.Services.AddTransient<IApiKeyValidation, ApiKeyValidation>();
builder.Services.AddMemoryCache();
builder.Services.AddScoped<ApiKeyFilter>();
var app = builder.Build();
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseStaticFiles();
app.MapDefaultControllerRoute();
app.Run();