-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
83 lines (67 loc) · 2.57 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
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
using Edi.Translator.Configuration;
using Edi.Translator.Providers.AzureOpenAI;
using Microsoft.AspNetCore.RateLimiting;
using System.Threading.RateLimiting;
namespace Edi.Translator;
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
if (Helper.IsRunningOnAzureAppService())
{
builder.Logging.AddAzureWebAppDiagnostics();
}
builder.Services.AddControllers();
builder.Services.AddHttpClient();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddApplicationInsightsTelemetry();
builder.Services.AddScoped<IAOAIClient, AOAIClient>();
builder.Services.Configure<RouteOptions>(options =>
{
options.LowercaseUrls = true;
options.LowercaseQueryStrings = true;
options.AppendTrailingSlash = false;
});
builder.Services.AddRateLimiter(limiterOptions =>
{
limiterOptions.OnRejected = async (context, ct) =>
{
if (context.Lease.TryGetMetadata(MetadataName.RetryAfter, out var retryAfter))
{
context.HttpContext.Response.Headers.RetryAfter = ((int)retryAfter.TotalSeconds).ToString();
}
context.HttpContext.Response.StatusCode = StatusCodes.Status429TooManyRequests;
await context.HttpContext.Response.WriteAsync("Too Many Requests", ct);
};
void AddLimiter(string policyName, int eventCount, TimeSpan perTimeSpan)
{
limiterOptions.AddFixedWindowLimiter(
policyName: policyName,
RateLimiterOptionFactory.GetFixedWindowRateLimiterOptions(eventCount, perTimeSpan));
}
AddLimiter("TranslateLimiter", 5, TimeSpan.FromSeconds(1));
});
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseDefaultFiles();
app.UseStaticFiles(new StaticFileOptions()
{
OnPrepareResponse = context =>
{
context.Context.Response.Headers.TryAdd("Cache-Control", "no-cache, no-store");
context.Context.Response.Headers.TryAdd("Expires", "-1");
}
});
app.UseAuthorization();
app.MapControllers();
app.UseRateLimiter();
app.Run();
}
}