-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
84 lines (69 loc) · 2.65 KB
/
Program.cs
File metadata and controls
84 lines (69 loc) · 2.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
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
using System.Threading.RateLimiting;
using CanonSeSu.Api.Data;
using CanonSeSu.Api.Endpoints;
using CanonSeSu.Api.Jobs;
using CanonSeSu.Api.Middleware;
using CanonSeSu.Api.Services;
using Quartz;
using Serilog;
var builder = WebApplication.CreateBuilder(args);
var logFilePath = builder.Configuration["Serilog:FilePath"] ?? "logs/log-.log";
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Information()
.MinimumLevel.Override("Microsoft.AspNetCore", Serilog.Events.LogEventLevel.Warning)
.MinimumLevel.Override("Microsoft.Hosting.Lifetime", Serilog.Events.LogEventLevel.Warning)
.WriteTo.Console()
.WriteTo.File(
path: logFilePath,
rollingInterval: RollingInterval.Month,
retainedFileCountLimit: 12,
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level:u3}] {Message:lj}{NewLine}{Exception}")
.CreateLogger();
builder.Host.UseSerilog();
builder.Services.AddOpenApi();
builder.Services.AddRateLimiter(options =>
{
options.RejectionStatusCode = StatusCodes.Status429TooManyRequests;
options.GlobalLimiter = PartitionedRateLimiter.Create<HttpContext, string>(context =>
RateLimitPartition.GetFixedWindowLimiter(
partitionKey: context.Request.Headers["CF-Connecting-IP"].FirstOrDefault()
?? context.Connection.RemoteIpAddress?.ToString()
?? "unknown",
factory: _ => new FixedWindowRateLimiterOptions
{
Window = TimeSpan.FromMinutes(1),
PermitLimit = 60,
QueueProcessingOrder = QueueProcessingOrder.OldestFirst,
QueueLimit = 0
}));
});
builder.Services.AddScoped<AppDb>(_ =>
new AppDb(builder.Configuration.GetConnectionString("DefaultConnection")!));
builder.Services.AddScoped<EmailService>();
builder.Services.AddQuartz(q =>
{
q.AddJob<CounterRequestEmailJob>(CounterRequestEmailJob.Key, c => c.StoreDurably());
q.AddTrigger(t => t
.ForJob(CounterRequestEmailJob.Key)
.WithIdentity("CounterRequestEmailTrigger")
.WithCronSchedule(
"0 0 2 28 * ?",
x => x.InTimeZone(TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time"))));
});
builder.Services.AddQuartzHostedService(q => q.WaitForJobsToComplete = true);
builder.Services.AddHealthChecks();
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}
app.UseHttpsRedirection();
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseRateLimiter();
app.UseMiddleware<ApiKeyMiddleware>();
app.MapHealthChecks("/health");
app.MapDevicesEndpoints();
app.MapAdminEndpoints();
app.MapUserEndpoints();
app.Run();