-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
98 lines (81 loc) · 2.72 KB
/
Copy pathProgram.cs
File metadata and controls
98 lines (81 loc) · 2.72 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
using BookLibrarySystem.Data;
using BookLibrarySystem.Repositories;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.StackExchangeRedis;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Serilog;
using System.IO;
// Configure Serilog
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.Enrich.FromLogContext()
.CreateLogger();
var builder = WebApplication.CreateBuilder(args);
// Use Serilog as the logging provider
builder.Host.UseSerilog();
// Add services to the container
builder.Services.AddControllersWithViews(); // For MVC with views
builder.Services.AddScoped<IBookRepository, BookRepository>();
builder.Services.AddScoped<BookLibrarySystem.Filters.ActionLoggingFilter>();
builder.Services.AddResponseCaching();
builder.Services.AddMemoryCache();
builder.Services.AddStackExchangeRedisCache(options =>
{
options.Configuration = "localhost:6379";
options.InstanceName = "BookLibrary_";
});
builder.Services.AddHealthChecks();
// Configure DbContext
builder.Services.AddDbContext<BookLibraryContext>(options =>
options.UseSqlite(builder.Configuration.GetConnectionString("DefaultConnection")));
// Add Swagger
var xmlFile = $"{System.Reflection.Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
builder.Services.AddSwaggerGen(options =>
{
options.IncludeXmlComments(xmlPath);
});
var app = builder.Build();
// Configure the HTTP request pipeline
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles(); // For serving static files like CSS/JS
app.UseRouting();
app.UseAuthorization();
// Use custom request logging middleware
app.UseMiddleware<BookLibrarySystem.Middleware.RequestLoggingMiddleware>();
// Use response caching middleware
app.UseResponseCaching();
// Map default route for MVC
app.MapControllerRoute(
name: "default",
pattern: "{controller=Books}/{action=Index}/{id?}");
// Map health check endpoint
app.MapHealthChecks("/health");
// Initialize the database
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
try
{
var context = services.GetRequiredService<BookLibraryContext>();
DbInitializer.Initialize(context);
}
catch (Exception ex)
{
var logger = services.GetRequiredService<ILogger<Program>>();
logger.LogError(ex, "An error occurred while seeding the database.");
}
}
app.Run();
// Ensure to flush and stop Serilog on shutdown
Log.CloseAndFlush();