-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathProgram.cs
More file actions
144 lines (118 loc) · 4.35 KB
/
Program.cs
File metadata and controls
144 lines (118 loc) · 4.35 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
using AutoMapper;
using DesignPatternSamples.Application.Decorators;
using DesignPatternSamples.Application.Implementations;
using DesignPatternSamples.Application.Repository;
using DesignPatternSamples.Application.Services;
using DesignPatternSamples.Infra.Repository.Detran;
using DesignPatternSamples.WebAPI.Middlewares;
using DesignPatternSamples.WebAPI.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.OpenApi.Models;
using Serilog;
using Workbench.DependencyInjection.Extensions;
// Configure Serilog
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.Build())
.CreateLogger();
try
{
var builder = WebApplication.CreateBuilder(args);
// Add Serilog
builder.Host.UseSerilog();
// Add services to the container
var services = builder.Services;
// Health checks
services.AddHealthChecks();
// Swagger
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "DesignPatternSamples", Version = "v1" });
});
// Add dependency injection and AutoMapper
services.AddDependencyInjection()
.AddAutoMapper();
// Add distributed memory cache (FAKE distributed cache)
services.AddDistributedMemoryCache();
// Add controllers
services.AddControllers(options =>
{
options.Filters.Add(new ProducesResponseTypeAttribute(typeof(FailureResultModel), 500));
});
// Add API explorer for Swagger
services.AddEndpointsApiExplorer();
var app = builder.Build();
// Configure the HTTP request pipeline
const string HEALTH_PATH = "/health";
// Health checks
app.UseHealthChecks(HEALTH_PATH);
// Swagger
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "API");
});
// Development exception page
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
// HTTPS redirection
app.UseHttpsRedirection();
// Routing
app.UseRouting();
// Authorization
app.UseAuthorization();
// Custom middleware
app.UseDetranVerificadorDebitosFactory();
app.UseMiddleware<ExceptionHandlingMiddleware>();
// Map controllers
app.MapControllers();
app.Run();
}
catch (Exception ex)
{
Log.Fatal(ex, "Application terminated unexpectedly");
}
finally
{
Log.CloseAndFlush();
}
// Extension methods
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddDependencyInjection(this IServiceCollection services)
{
return services
.AddTransient<IDetranVerificadorDebitosService, DetranVerificadorDebitosServices>()
.Decorate<IDetranVerificadorDebitosService, DetranVerificadorDebitosDecoratorCache>()
.Decorate<IDetranVerificadorDebitosService, DetranVerificadorDebitosDecoratorLogger>()
.AddSingleton<IDetranVerificadorDebitosFactory, DetranVerificadorDebitosFactory>()
.AddTransient<DetranPEVerificadorDebitosRepository>()
.AddTransient<DetranSPVerificadorDebitosRepository>()
.AddTransient<DetranRJVerificadorDebitosRepository>()
.AddTransient<DetranRSVerificadorDebitosRepository>()
.AddScoped<ExceptionHandlingMiddleware>();
}
public static IServiceCollection AddAutoMapper(this IServiceCollection services)
{
var types = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(a => a.GetTypes())
.Where(t => t.IsSubclassOf(typeof(Profile)));
return services.AddAutoMapper(types.ToArray());
}
}
public static class ApplicationBuilderExtensions
{
public static IApplicationBuilder UseDetranVerificadorDebitosFactory(this IApplicationBuilder app)
{
app.ApplicationServices.GetService<IDetranVerificadorDebitosFactory>()?
.Register("PE", typeof(DetranPEVerificadorDebitosRepository))
.Register("RJ", typeof(DetranRJVerificadorDebitosRepository))
.Register("SP", typeof(DetranSPVerificadorDebitosRepository))
.Register("RS", typeof(DetranRSVerificadorDebitosRepository));
return app;
}
}