-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
46 lines (35 loc) · 1.32 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
using SecretDeliveryApp;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddScoped<SecretRequestProcessor>();
builder.Services.AddScoped<ISecretProvider, AzureKeyVaultClient>();
builder.Services.AddScoped<IIoTHubServiceClient, IoTHubServiceClient>();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
app.UseAPIKeyCheckMiddleware();
}
// Doing the validation handshake with EventGrid so the subcription can be created
app.MapMethods("/events", new[] { "OPTIONS" }, (http) =>
{
app.Logger.LogInformation("Received request for validation.");
http.Response.StatusCode = 200;
http.Response.Headers.Add("Webhook-Allowed-Origin", "eventgrid.azure.net");
return Task.CompletedTask;
}
);
// Handle events
app.MapPost("/events", async (HttpContext http, SecretRequestProcessor processor) =>
{
string content = string.Empty;
using (var readStream = new StreamReader(http.Request.Body))
content = await readStream.ReadToEndAsync();
await processor.ProcessAsync(content);
});
app.Run();