Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
134 lines (105 loc) 路 3.58 KB
/
Copy pathProgram.cs
File metadata and controls
134 lines (105 loc) 路 3.58 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using DotNetCoreAPITemplate.Configuration;
using DotNetCoreAPITemplate.Services;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Serilog;
// Configure Serilog early in the application startup
var builder = WebApplication.CreateBuilder(args);
// Configure Serilog logging
builder.ConfigureSerilogLogging();
try
{
Log.Information("Starting up DotNet Core API Template");
// Add configuration services
builder.Services.AddApplicationConfiguration(builder.Configuration);
// Add database services
builder.Services.AddDatabaseServices(builder.Configuration);
// Add API versioning
builder.Services.AddApiVersioningServices();
// Add controllers and API explorer
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
// Add Swagger documentation with versioning support
builder.Services.AddVersionedSwaggerDocumentation(builder.Configuration);
// Add CORS
builder.Services.AddCors(options =>
{
options.AddDefaultPolicy(policy =>
{
var corsSettings = builder.Configuration.GetSection("CorsSettings").Get<CorsSettings>() ?? new();
if (corsSettings.AllowedOrigins.Length > 0)
{
policy.WithOrigins(corsSettings.AllowedOrigins);
}
else
{
policy.AllowAnyOrigin();
}
policy.AllowAnyMethod()
.AllowAnyHeader();
if (corsSettings.AllowCredentials && corsSettings.AllowedOrigins.Length > 0)
{
policy.AllowCredentials();
}
});
});
// Add health checks
builder.Services.AddHealthChecks();
// Register application services
builder.Services.AddScoped<ISampleEntityService, SampleEntityService>();
var app = builder.Build();
// Configure the HTTP request pipeline
// Use request logging (must be early in pipeline)
app.UseRequestLogging();
// Use performance logging
app.UsePerformanceLogging();
// Development-specific middleware
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
// CORS
app.UseCors();
// HTTPS redirection
app.UseHttpsRedirection();
// Authentication and Authorization (if needed in the future)
// app.UseAuthentication();
app.UseAuthorization();
// Swagger documentation
var provider = app.Services.GetRequiredService<IApiVersionDescriptionProvider>();
app.UseVersionedSwaggerUI(provider, builder.Configuration);
// Health checks
app.MapHealthChecks("/health");
app.MapHealthChecks("/health/ready", new Microsoft.AspNetCore.Diagnostics.HealthChecks.HealthCheckOptions
{
Predicate = check => check.Tags.Contains("ready")
});
app.MapHealthChecks("/health/live", new Microsoft.AspNetCore.Diagnostics.HealthChecks.HealthCheckOptions
{
Predicate = _ => false
});
// Map controllers
app.MapControllers();
// Ensure database is created and migrated
app.EnsureDatabaseCreated();
// Seed database if empty
if (app.Environment.IsDevelopment())
{
app.SeedDatabase();
}
Log.Information("DotNet Core API Template started successfully");
app.Run();
}
catch (Exception ex)
{
Log.Fatal(ex, "Application terminated unexpectedly");
throw;
}
finally
{
await Log.CloseAndFlushAsync();
}