-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathProgram.cs
More file actions
55 lines (46 loc) · 1.65 KB
/
Program.cs
File metadata and controls
55 lines (46 loc) · 1.65 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
using ApiGateway.LoadBalancer;
using AppHost.ServiceDefaults;
using Ocelot.DependencyInjection;
using Ocelot.Middleware;
var builder = WebApplication.CreateBuilder(args);
builder.AddServiceDefaults();
builder.Services.AddServiceDiscovery();
builder.Configuration.AddJsonFile("ocelot.json", optional: false, reloadOnChange: true);
var weights = builder.Configuration
.GetSection("LoadBalancerWeights")
.Get<Dictionary<string, double>>() ?? [];
builder.Services
.AddOcelot()
.AddCustomLoadBalancer<WeightedRandomBalancer>((_, _, discoveryProvider) => new(discoveryProvider.GetAsync, weights));
var allowedOrigins = builder.Configuration.GetSection("Cors:AllowedOrigins").Get<string[]>();
var allowedMethods = builder.Configuration.GetSection("Cors:AllowedMethods").Get<string[]>();
var allowedHeaders = builder.Configuration.GetSection("Cors:AllowedHeaders").Get<string[]>();
builder.Services.AddCors(options =>
{
options.AddDefaultPolicy(policy =>
{
if (allowedOrigins != null)
{
_ = allowedOrigins.Contains("*")
? policy.AllowAnyOrigin()
: policy.WithOrigins(allowedOrigins);
}
if (allowedMethods != null)
{
_ = allowedMethods.Contains("*")
? policy.AllowAnyMethod()
: policy.WithMethods(allowedMethods);
}
if (allowedHeaders != null)
{
_ = allowedHeaders.Contains("*")
? policy.AllowAnyHeader()
: policy.WithHeaders(allowedHeaders);
}
});
});
var app = builder.Build();
app.MapDefaultEndpoints();
app.UseCors();
await app.UseOcelot();
app.Run();