-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathProgram.cs
More file actions
115 lines (101 loc) · 5.47 KB
/
Copy pathProgram.cs
File metadata and controls
115 lines (101 loc) · 5.47 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Azure;
using Microsoft.Extensions.Logging;
using Azure.Identity;
using TravelPlannerFunctions.Services;
using Azure.AI.Projects;
var host = new HostBuilder()
.ConfigureFunctionsWebApplication()
.ConfigureServices(services =>
{
services.AddApplicationInsightsTelemetryWorkerService();
services.ConfigureFunctionsApplicationInsights();
services.AddLogging();
services.AddSingleton<DestinationRecommenderService>((serviceProvider) =>
{
var configuration = serviceProvider.GetRequiredService<IConfiguration>();
var loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
var destinationConnectionString = configuration["DESTINATION_RECOMMENDER_CONNECTION"] ?? throw new InvalidOperationException("Destination recommender connection string is not configured");
var logger = loggerFactory.CreateLogger<DestinationRecommenderService>();
return new DestinationRecommenderService(destinationConnectionString, loggerFactory.CreateLogger<DestinationRecommenderService>());
});
services.AddSingleton<ItineraryPlannerService>((serviceProvider) =>
{
var configuration = serviceProvider.GetRequiredService<IConfiguration>();
var loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
var itineraryConnectionString = configuration["ITINERARY_PLANNER_CONNECTION"] ?? throw new InvalidOperationException("Itinerary planner connection string is not configured");
var logger = loggerFactory.CreateLogger<ItineraryPlannerService>();
return new ItineraryPlannerService(itineraryConnectionString, loggerFactory.CreateLogger<ItineraryPlannerService>());
});
services.AddSingleton<LocalRecommendationsService>((serviceProvider) =>
{
var configuration = serviceProvider.GetRequiredService<IConfiguration>();
var loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
var localRecommendationsConnectionString = configuration["LOCAL_RECOMMENDATIONS_CONNECTION"] ?? throw new InvalidOperationException("Local recommendations connection string is not configured");
var logger = loggerFactory.CreateLogger<LocalRecommendationsService>();
return new LocalRecommendationsService(localRecommendationsConnectionString, loggerFactory.CreateLogger<LocalRecommendationsService>());
});
services.AddAzureClients(clientBuilder =>
{
var tenantId = Environment.GetEnvironmentVariable("AZURE_TENANT_ID");
var clientId = Environment.GetEnvironmentVariable("AZURE_CLIENT_ID");
ArgumentNullException.ThrowIfNullOrEmpty(tenantId, nameof(tenantId));
ArgumentNullException.ThrowIfNullOrEmpty(clientId, nameof(clientId));
// Use the same credentials for all clients.
clientBuilder.UseCredential(new DefaultAzureCredential(new DefaultAzureCredentialOptions
{
TenantId = tenantId,
ManagedIdentityClientId = clientId
}));
// If running in local development with Azurite emulator
var connectionString = Environment.GetEnvironmentVariable("AzureWebJobsStorage");
if (!string.IsNullOrEmpty(connectionString))
{
clientBuilder.AddBlobServiceClient(connectionString);
}
// Use the managed identity to connect to the storage account.
else
{
var storageAccountName = Environment.GetEnvironmentVariable("AzureWebJobsStorage__accountName");
ArgumentNullException.ThrowIfNullOrEmpty(storageAccountName, "AzureWebJobsStorage__accountName environment variable is not set.");
clientBuilder.AddBlobServiceClient(
new Uri($"https://{storageAccountName}.blob.core.windows.net"),
new DefaultAzureCredential());
}
});
// Configure CORS for both local development and Azure Static Web App
services.AddCors(options =>
{
options.AddDefaultPolicy(policy =>
{
// Get the Static Web App URL from environment variables (set in Azure)
var allowedOrigins = Environment.GetEnvironmentVariable("ALLOWED_ORIGINS") ?? "*";
// Split by comma if multiple origins are provided
var origins = allowedOrigins.Split(',', StringSplitOptions.RemoveEmptyEntries);
if (origins.Length == 1 && origins[0] == "*")
{
// For development or if no specific origins are set, allow any origin
policy.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod();
}
else
{
// For production with specific origins
policy.WithOrigins(origins)
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
}
});
});
})
.ConfigureAppConfiguration(builder =>
{
builder.AddEnvironmentVariables();
})
.Build();
await host.RunAsync();