-
Notifications
You must be signed in to change notification settings - Fork 2
/
Program.cs
124 lines (91 loc) · 3.26 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
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
116
117
118
119
120
121
122
123
124
using Serilog;
using Microsoft.EntityFrameworkCore;
using System.Text.Json.Serialization;
using CoolingGridManager.Extensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.OpenApi.Models;
using System.Reflection;
using Microsoft.AspNetCore.RateLimiting;
using System.Threading.RateLimiting;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHealthChecks();
Serilog.ILogger logger = Log.Logger;
// Load Configuration File
IConfiguration configuration = new Settings(logger).LoadSettings();
//Add support to logging with SERILOG
builder.Host.UseSerilog((context, configuration) =>
configuration.ReadFrom.Configuration(context.Configuration));
// Register the logger as a service
builder.Services.AddSingleton<Serilog.ILogger>(_ => Log.Logger);
// Register Exception responses
builder.Services.AddSingleton<ExceptionResponse>();
// Configure database
var connectionString = new DatabaseConnection(logger).GetDatabaseConnectionString();
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseNpgsql(connectionString));
// Add support for controllers
builder.Services.AddControllers().AddJsonOptions(x =>
x.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.Preserve);
// Add Api Versioning
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddApiVersioning(options =>
{
options.DefaultApiVersion = new ApiVersion(1, 0);
options.AssumeDefaultVersionWhenUnspecified = true;
options.ReportApiVersions = true;
});
builder.Services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo { Title = "Cooling Grid Manager API", Version = "v1" });
// Add support for XML comments
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
options.IncludeXmlComments(xmlPath);
});
// Register Cron Jobs
builder.Services.AddCustomCronJobs();
// Register Validators
builder.Services.AddValidators();
// Rate Limiter
builder.Services.AddRateLimiter(_ => _
.AddFixedWindowLimiter(policyName: "fixed", options =>
{
options.PermitLimit = 4;
options.Window = TimeSpan.FromSeconds(15);
options.QueueProcessingOrder = QueueProcessingOrder.OldestFirst;
options.QueueLimit = 2;
}));
// Register Services
ServiceExtension.AddServices(builder.Services);
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
options.RoutePrefix = string.Empty; // Sets Swagger UI at the root URL
options.DocumentTitle = "Cooling Grid Manager API";
});
}
//Add support to logging request with SERILOG
app.UseSerilogRequestLogging();
app.UseRouting();
app.MapHealthChecks("/health");
app.UseRateLimiter();
// Register routes
RouteExtension.ConfigureRoutes(app);
app.MapGet("/{**slug}", async (context) =>
{
// Return a 404 Not Found response for any unmatched route
context.Response.StatusCode = StatusCodes.Status404NotFound;
await context.Response.WriteAsync("404 - Not Found");
});
app.Run();
Log.CloseAndFlush();