-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
117 lines (95 loc) · 3.51 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
using System.Text.Json;
using System.Text.Json.Serialization;
using BdGeographicalData.Api.District;
using BdGeographicalData.Api.Division;
using BdGeographicalData.Api.SubDistrict;
using BdGeographicalData.Shared;
using BdGeographicalData.Utils;
using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using Serilog;
using Serilog.Events;
Log.Logger = new LoggerConfiguration()
.WriteTo.Async(wt => wt.Console())
.CreateBootstrapLogger();
try
{
var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddJsonFile(Constants.SecretsJsonFileName, optional: true, reloadOnChange: true);
builder.Host.UseSerilog((_, loggerConfiguration) => loggerConfiguration
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning)
.MinimumLevel.Override("System", LogEventLevel.Warning)
.Enrich.FromLogContext()
.WriteTo.Async(x => x.Console(
restrictedToMinimumLevel: LogEventLevel.Information,
outputTemplate: Constants.SerilogConsoleOutputTemplate
))
.WriteTo.Async(x => x.AsyncRollingFile(
restrictedToMinimumLevel: LogEventLevel.Error,
pathFormat: Constants.SerilogFilePath
))
);
builder.Services.AddOptions<AppOptions>()
.BindConfiguration(Constants.AppOptions)
.ValidateDataAnnotations()
.ValidateOnStart();
var section = builder.Configuration.GetSection(Constants.AppOptions);
var options = new AppOptions
{
DatabaseUrl = section.GetValue<string>("DatabaseUrl"),
ResponseCacheDurationInSecond = section.GetValue<int>("ResponseCacheDurationInSecond")
};
builder.Services.AddDbContext<BdGeographicalDataDbContext>(opts =>
opts.UseSqlite(options.DatabaseUrl));
builder.Services.AddScoped<ResponseCacheConfigMiddleware>();
builder.Services.AddScoped<IDivisionService, DivisionService>();
builder.Services.AddScoped<IDistrictService, DistrictService>();
builder.Services.AddScoped<ISubDistrictService, SubDistrictService>();
builder.Services
.AddControllers(opts =>
{
opts.Conventions.Add(new RouteTokenTransformerConvention(new SlugifyParameterTransformer()));
opts.OutputFormatters.RemoveType<StringOutputFormatter>();
})
.AddJsonOptions(opts =>
{
opts.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
opts.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
opts.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter(allowIntegerValues: false));
opts.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
});
builder.Services.AddResponseCaching();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
app.UseSwagger();
app.UseSwaggerUI();
app.UseAuthorization();
app.MapControllers();
app.UseResponseCaching();
app.UseMiddleware<ResponseCacheConfigMiddleware>();
app.Run();
return 0;
}
catch (HostAbortedException)
{
Log.Information("HostAbortedException skipped");
return 0;
}
catch (OptionsValidationException)
{
return 1;
}
catch (Exception e)
{
Console.WriteLine(e);
Log.Fatal(e, "Failed to start application");
return 1;
}
finally
{
Log.CloseAndFlush();
}