-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathProgram.cs
More file actions
54 lines (40 loc) · 1.6 KB
/
Copy pathProgram.cs
File metadata and controls
54 lines (40 loc) · 1.6 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
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Models;
using mongodb_dotnet_example.Models;
using mongodb_dotnet_example.Services;
var builder = WebApplication.CreateBuilder(args);
builder.Services.Configure<GamesDatabaseSettings>(builder.Configuration.GetSection(nameof(GamesDatabaseSettings)));
builder.Services.Configure<StartupBehaviorSettings>(builder.Configuration.GetSection(nameof(StartupBehaviorSettings)));
builder.Services.AddSingleton<IGamesDatabaseSettings>(sp => sp.GetRequiredService<IOptions<GamesDatabaseSettings>>().Value);
builder.Services.AddSingleton<IGamesService, GamesService>();
builder.Services.AddControllers();
builder.Services.AddHealthChecks();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "mongodb_dotnet_example", Version = "v1" });
});
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "mongodb_dotnet_example v1"));
if (!app.Environment.IsDevelopment())
{
app.UseHttpsRedirection();
}
app.UseRouting();
app.UseAuthorization();
var startupBehaviorOptions = app.Services.GetRequiredService<IOptions<StartupBehaviorSettings>>();
if (startupBehaviorOptions.Value.SeedOnStartup)
{
var gamesService = app.Services.GetRequiredService<IGamesService>();
gamesService.SeedIfEmpty(GameSeedData.DefaultGames);
}
app.MapControllers();
app.MapHealthChecks("/health");
app.Run();