-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
153 lines (131 loc) · 4.98 KB
/
Copy pathProgram.cs
File metadata and controls
153 lines (131 loc) · 4.98 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Http.Json;
using Microsoft.AspNetCore.HttpOverrides;
using Serilog;
using Serilog.Exceptions;
using Serilog.Exceptions.Core;
using Serilog.Formatting.Json;
using VerticalSliceProject.Setup;
using VerticalSliceProject.Setup.Json;
using VerticalSliceProject.Setup.OpenApi;
// ----- Bootstrap Serilog before host construction so startup errors are captured. -----
Log.Logger = new LoggerConfiguration()
.WriteTo.Console(new JsonFormatter())
.Enrich.WithExceptionDetails(new DestructuringOptionsBuilder())
.Enrich.FromLogContext()
.CreateLogger();
Log.Information("Starting up");
try
{
// Special-case for `dotnet GetDocument.Insider` (Microsoft.Extensions.ApiDescription.Server) —
// it spins up the host to generate OpenAPI documents at build time. Everything endpoints
// resolve from DI must be visible here, but no real infra (DB, etc.) is needed.
if (Assembly.GetEntryAssembly()?.GetName().Name == "GetDocument.Insider")
{
var minimalBuilder = WebApplication.CreateBuilder(args);
ConfigureSerilog(minimalBuilder.Host);
ConfigureJson(minimalBuilder.Services);
minimalBuilder.Services.AddAppOpenApi();
minimalBuilder.Services.AddEndpointsApiExplorer();
minimalBuilder.Services.AddDocGenStubs();
var minimalApp = minimalBuilder.Build();
minimalApp.UseAppOpenApi();
// Endpoints must still be in the route table so doc generation can describe them, but
// they won't actually serve — GetDocument.Insider only walks the description provider.
minimalApp.MapAllEndpoints();
minimalApp.Run();
}
else
{
var builder = WebApplication.CreateBuilder(args);
ConfigureSerilog(builder.Host);
ConfigureConfiguration(builder.Configuration, builder.Environment);
ConfigureServices(builder.Services, builder.Configuration, builder.Environment);
var webApplication = builder.Build();
ConfigureMiddleware(webApplication, webApplication.Environment);
ConfigureEndpoints(webApplication);
webApplication.Run();
}
}
catch (Exception e)
{
Log.Fatal(e, "Unhandled exception");
}
finally
{
Log.Information("Shut down complete");
Log.CloseAndFlush();
}
return;
void ConfigureConfiguration(ConfigurationManager configuration, IWebHostEnvironment env)
{
configuration
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true)
.AddJsonFile("appsettings.local.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.AddEnvironmentVariablesFromJsonVariables();
}
void ConfigureServices(IServiceCollection services, IConfiguration configuration, IWebHostEnvironment env)
{
ConfigureJson(services);
services.AddEndpointsApiExplorer();
services.AddAppOpenApi();
services.AddAppProblemDetails();
services.AddHealthChecks();
services.AddHttpContextAccessor();
services.Configure<HostOptions>(options =>
{
options.ShutdownTimeout = TimeSpan.FromMinutes(1);
});
services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders = ForwardedHeaders.All;
options.ForwardLimit = 2;
options.KnownProxies.Clear();
options.KnownIPNetworks.Clear();
});
// Walk the assembly for IFeatureServices implementers and register feature-internal services.
// Called LAST so feature registrations may override anything registered above.
services.AddFeatureServices(configuration, env);
}
void ConfigureMiddleware(WebApplication app, IWebHostEnvironment env)
{
app.UseForwardedHeaders();
if (app.Environment.IsDevelopment() || app.Environment.IsStaging())
{
app.UseAppOpenApi();
}
app.UseAppRequestLogging();
app.UseExceptionHandler();
}
void ConfigureEndpoints(IEndpointRouteBuilder app)
{
app.MapHealthChecks("/health");
app.MapAllEndpoints();
}
void ConfigureJson(IServiceCollection services)
{
services.Configure<JsonOptions>(opts =>
{
opts.SerializerOptions.Converters.Add(new JsonStringEnumConverter());
opts.SerializerOptions.Converters.Add(new DateTimeJsonConverter());
opts.SerializerOptions.Converters.Add(new DateOnlyJsonConverter());
opts.SerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
opts.SerializerOptions.WriteIndented = false;
opts.SerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
});
}
void ConfigureSerilog(IHostBuilder host)
{
host.UseSerilog((context, services, configuration) =>
{
configuration
.ReadFrom.Configuration(context.Configuration)
.ReadFrom.Services(services)
.Enrich.WithExceptionDetails(new DestructuringOptionsBuilder())
.Enrich.FromLogContext();
});
}