|
1 | | -using Dotnet.Samples.AspNetCore.WebApi.Configurations; |
2 | | -using Dotnet.Samples.AspNetCore.WebApi.Data; |
3 | | -using Dotnet.Samples.AspNetCore.WebApi.Mappings; |
4 | | -using Dotnet.Samples.AspNetCore.WebApi.Models; |
5 | | -using Dotnet.Samples.AspNetCore.WebApi.Repositories; |
6 | | -using Dotnet.Samples.AspNetCore.WebApi.Services; |
7 | | -using Dotnet.Samples.AspNetCore.WebApi.Validators; |
8 | | -using FluentValidation; |
9 | | -using Microsoft.EntityFrameworkCore; |
10 | | -using Microsoft.OpenApi.Models; |
| 1 | +using Dotnet.Samples.AspNetCore.WebApi.Extensions; |
11 | 2 | using Serilog; |
12 | 3 |
|
13 | | -var builder = WebApplication.CreateBuilder(args); |
14 | | - |
15 | 4 | /* ----------------------------------------------------------------------------- |
16 | | - * Configuration |
| 5 | + * Web Application |
| 6 | + * https://learn.microsoft.com/en-us/aspnet/core/fundamentals/startup |
17 | 7 | * -------------------------------------------------------------------------- */ |
| 8 | + |
| 9 | +var builder = WebApplication.CreateBuilder(args); |
| 10 | + |
| 11 | +/* Configurations ----------------------------------------------------------- */ |
| 12 | + |
18 | 13 | builder |
19 | 14 | .Configuration.SetBasePath(AppContext.BaseDirectory) |
20 | 15 | .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) |
21 | 16 | .AddEnvironmentVariables(); |
22 | 17 |
|
23 | | -/* ----------------------------------------------------------------------------- |
24 | | - * Logging |
25 | | - * -------------------------------------------------------------------------- */ |
26 | | -Log.Logger = new LoggerConfiguration().ReadFrom.Configuration(builder.Configuration).CreateLogger(); |
| 18 | +/* Logging ------------------------------------------------------------------ */ |
27 | 19 |
|
28 | | -/* Serilog ------------------------------------------------------------------ */ |
| 20 | +Log.Logger = new LoggerConfiguration().ReadFrom.Configuration(builder.Configuration).CreateLogger(); |
29 | 21 | builder.Host.UseSerilog(); |
30 | 22 |
|
31 | | -/* ----------------------------------------------------------------------------- |
32 | | - * Services |
33 | | - * -------------------------------------------------------------------------- */ |
| 23 | +/* Controllers -------------------------------------------------------------- */ |
| 24 | + |
34 | 25 | builder.Services.AddControllers(); |
| 26 | +builder.Services.AddCorsDefaultPolicy(); |
| 27 | +builder.Services.AddHealthChecks(); |
| 28 | +builder.Services.AddValidators(); |
35 | 29 |
|
36 | | -/* Entity Framework Core ---------------------------------------------------- */ |
37 | | -builder.Services.AddDbContextPool<PlayerDbContext>(options => |
| 30 | +if (builder.Environment.IsDevelopment()) |
38 | 31 | { |
39 | | - var dataSource = Path.Combine(AppContext.BaseDirectory, "storage", "players-sqlite3.db"); |
40 | | - |
41 | | - options.UseSqlite($"Data Source={dataSource}"); |
| 32 | + builder.Services.AddSwaggerConfiguration(builder.Configuration); |
| 33 | +} |
42 | 34 |
|
43 | | - if (builder.Environment.IsDevelopment()) |
44 | | - { |
45 | | - options.EnableSensitiveDataLogging(); |
46 | | - options.LogTo(Log.Logger.Information, LogLevel.Information); |
47 | | - } |
48 | | -}); |
| 35 | +/* Services ----------------------------------------------------------------- */ |
49 | 36 |
|
50 | | -builder.Services.AddScoped<IPlayerRepository, PlayerRepository>(); |
51 | | -builder.Services.AddScoped<IPlayerService, PlayerService>(); |
| 37 | +builder.Services.RegisterPlayerService(); |
52 | 38 | builder.Services.AddMemoryCache(); |
53 | | -builder.Services.AddHealthChecks(); |
| 39 | +builder.Services.AddMappings(); |
54 | 40 |
|
55 | | -/* AutoMapper --------------------------------------------------------------- */ |
56 | | -builder.Services.AddAutoMapper(typeof(PlayerMappingProfile)); |
| 41 | +/* Repositories ------------------------------------------------------------- */ |
57 | 42 |
|
58 | | -/* FluentValidation --------------------------------------------------------- */ |
59 | | -builder.Services.AddScoped<IValidator<PlayerRequestModel>, PlayerRequestModelValidator>(); |
| 43 | +builder.Services.RegisterPlayerRepository(); |
60 | 44 |
|
61 | | -if (builder.Environment.IsDevelopment()) |
62 | | -{ |
63 | | - /* Swagger UI ----------------------------------------------------------- */ |
64 | | - // https://learn.microsoft.com/en-us/aspnet/core/tutorials/getting-started-with-swashbuckle |
65 | | - builder.Services.AddSwaggerGen(options => |
66 | | - { |
67 | | - options.SwaggerDoc("v1", builder.Configuration.GetSection("SwaggerDoc").Get<OpenApiInfo>()); |
68 | | - options.IncludeXmlComments(SwaggerGenDefaults.ConfigureXmlCommentsFilePath()); |
69 | | - options.AddSecurityDefinition("Bearer", SwaggerGenDefaults.ConfigureSecurityDefinition()); |
70 | | - options.OperationFilter<AuthorizeCheckOperationFilter>(); |
71 | | - }); |
72 | | -} |
| 45 | +/* Data --------------------------------------------------------------------- */ |
| 46 | + |
| 47 | +builder.Services.AddDbContextPoolWithSqlite(builder.Environment); |
73 | 48 |
|
74 | 49 | var app = builder.Build(); |
75 | 50 |
|
76 | 51 | /* ----------------------------------------------------------------------------- |
77 | 52 | * Middlewares |
78 | 53 | * https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware |
79 | 54 | * -------------------------------------------------------------------------- */ |
| 55 | + |
80 | 56 | app.UseSerilogRequestLogging(); |
81 | 57 |
|
82 | 58 | if (app.Environment.IsDevelopment()) |
|
85 | 61 | app.UseSwaggerUI(); |
86 | 62 | } |
87 | 63 |
|
88 | | -// https://learn.microsoft.com/en-us/aspnet/core/security/enforcing-ssl |
89 | 64 | app.UseHttpsRedirection(); |
90 | | - |
91 | | -// https://learn.microsoft.com/en-us/aspnet/core/security/cors |
92 | 65 | app.UseCors(); |
93 | | - |
94 | | -// https://learn.microsoft.com/en-us/aspnet/core/fundamentals/routing#endpoints |
95 | | -app.MapControllers(); |
96 | | - |
97 | | -// https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/health-checks |
| 66 | +app.UseRateLimiter(); |
98 | 67 | app.MapHealthChecks("/health"); |
| 68 | +app.MapControllers(); |
99 | 69 |
|
100 | 70 | await app.RunAsync(); |
0 commit comments