-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
120 lines (103 loc) · 3.68 KB
/
Program.cs
File metadata and controls
120 lines (103 loc) · 3.68 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
using Microsoft.Extensions.Logging;
using Npgsql;
using QuestPDF.Infrastructure;
using Microsoft.Extensions.Logging;
using System.IO;
using Serilog;
using OrdersApi.Dtos;
var builder = WebApplication.CreateBuilder(args);
// make sure the directory exists
Directory.CreateDirectory("logs");
// Configure logging
//builder.Logging.ClearProviders();
//builder.Logging.AddConsole();
//builder.Logging.Addfil
// Configure API services
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
/////////
//dotnet add package Serilog.AspNetCore
//dotnet add package Serilog.Sinks.Console
//dotnet add package Serilog.Settings.Configuration
builder.Host.UseSerilog((context, services, configuration) =>
configuration.ReadFrom.Configuration(context.Configuration));
// Configure database connection pooling
var connectionString = builder.Configuration.GetConnectionString("Default");
builder.Services.AddSingleton<NpgsqlDataSource>(
_ => new NpgsqlDataSourceBuilder(connectionString).Build());
builder.Services.AddProblemDetails();
// In the pipeline
//app.UseExceptionHandler(); // catches unhandled exceptions
//app.UseStatusCodePages(); // turns bare 404s/500s into ProblemDetails JSON
//app.UseExceptionHandler(errorApp =>
//{
// errorApp.Run(async context =>
// {
// var exception = context.Features.Get<IExceptionHandlerFeature>()?.Error;
// var problemDetails = exception switch
// {
// NotFoundException => new ProblemDetails
// {
// Status = 404,
// Title = "Resource Not Found",
// Detail = exception.Message
// },
// ValidationException ve => new ProblemDetails
// {
// Status = 400,
// Title = "Validation Failed",
// Extensions = { ["errors"] = ve.Errors }
// },
// _ => new ProblemDetails
// {
// Status = 500,
// Title = "Internal Server Error"
// Detail intentionally omitted — no stack traces to clients
// }
// };
// context.Response.StatusCode = problemDetails.Status ?? 500;
// await context.Response.WriteAsJsonAsync(problemDetails);
// });
//});
////// ANOTHER WAY TO CONFIGURE SERILOG
//Log.Logger = new LoggerConfiguration()
// .Enrich.WithUtcTimestamp()
// .WriteTo.Console(outputTemplate:
// "{UtcTimestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level}] {Message}{NewLine}{Exception}")
// .CreateLogger();
//
/////
#pragma warning disable CS8604 // Possible null reference argument.
builder.Services.AddSingleton(new PgDb(connectionString));
#pragma warning restore CS8604 // Possible null reference argument.
builder.Services.AddScoped<OrdersRepository>();
//builder.Services.AddScoped<CustomersRepository>();
//builder.Services.AddScoped<OrderItemsRepository>();
// Configure CORS for Blazor frontend
// 1. Define the policy
builder.Services.AddCors(options => {
options.AddPolicy("LocalDevPolicy", policy => {
policy.WithOrigins("http://localhost:5000")
.AllowAnyMethod()
.AllowAnyHeader();
});
});
// Configure QuestPDF license
QuestPDF.Settings.License = LicenseType.Community;
var app = builder.Build();
// Configure middleware
app.UseCors("LocalDevPolicy");
app.UseSerilogRequestLogging(); // replaces default noisy request logs
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.MapControllers();
var logger = LoggerFactory.Create(cfg =>
{
cfg.AddConsole();
}).CreateLogger("startup");
logger.LogCritical("{Timestamp} serverstarted!!!!!!!!", DateTime.Now);
app.Run();