-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
195 lines (172 loc) · 6.12 KB
/
Program.cs
File metadata and controls
195 lines (172 loc) · 6.12 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
using System.Globalization;
using System.Reflection;
using System.Text;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Localization;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using PlayOffsApi.HostedService;
using PlayOffsApi.Middleware;
using PlayOffsApi.Models;
using PlayOffsApi.Services;
var builder = WebApplication.CreateBuilder(args);
var config = builder.Configuration;
builder.Logging.ClearProviders();
builder.Logging.AddConsole();
var ISSUER = config["JwtSettings:Issuer"];
var AUDIENCE = config["JwtSettings:Audience"];
var KEY = config["JwtSettings:Key"];
// var CRYPT_KEY = config["CryptKey"].ToUtf8Bytes();
var jwtSettings = new JwtSettings
{
Issuer = ISSUER,
Audience = AUDIENCE,
Key = KEY
};
if (builder.Environment.IsProduction())
{
ISSUER = Environment.GetEnvironmentVariable("AUTH_ISSUER");
AUDIENCE = Environment.GetEnvironmentVariable("AUTH_AUDIENCE");
KEY = Environment.GetEnvironmentVariable("AUTH_KEY");
// CRYPT_KEY = Environment.GetEnvironmentVariable("CRYPT_KEY").ToUtf8Bytes();
}
var audience = new[] { AUDIENCE, "https://localhost:5173", "https://127.0.0.1:5173", "http://localhost" };
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(con =>
{
con.TokenValidationParameters = new()
{
ValidIssuer = ISSUER,
ValidAudiences = audience,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(KEY)),
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true
};
con.Events = new JwtBearerEvents
{
OnMessageReceived = context =>
{
context.Token = context.Request.Cookies["playoffs-token"];
return Task.CompletedTask;
}
};
});
builder.Services.AddAuthorization();
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddSingleton(jwtSettings);
builder.Services.AddHostedService<BackgroundJobs>();
builder.Services.AddSingleton<IBackgroundJobsService, BackgroundJobs>();
builder.Services.AddSingleton<RedisService>();
builder.Services.AddScoped<DbService>();
builder.Services.AddScoped<ChampionshipService>();
builder.Services.AddSingleton<ElasticService>();
builder.Services.AddScoped<TeamService>();
builder.Services.AddScoped<PlayerTempProfileService>();
builder.Services.AddScoped<PlayerService>();
builder.Services.AddScoped<BracketingService>();
builder.Services.AddScoped<MatchService>();
builder.Services.AddScoped<GoalService>();
builder.Services.AddScoped<PenaltyService>();
builder.Services.AddScoped<ImageService>();
builder.Services.AddScoped<EmailService>();
builder.Services.AddScoped<ErrorLogService>();
builder.Services.AddScoped<ChampionshipActivityLogService>();
builder.Services.AddScoped<CaptchaService>();
builder.Services.AddScoped<OrganizerService>();
builder.Services.AddScoped<StatisticsService>();
builder.Services.AddScoped<FoulService>();
builder.Services.AddScoped<ReplacementService>();
builder.Services.AddScoped<FirstStringService>();
// builder.Services.AddScoped<WoService>();
builder.Services.AddScoped<BracketingMatchService>();
builder.Services.AddHttpContextAccessor();
builder.Services.AddScoped<WoService>();
builder.Services.AddScoped<Logger<User>>();
builder.Services.AddScoped<TodoService>();
builder.Services.AddScoped<ReportService>();
builder.Services.AddScoped(sp => new AuthService(KEY, ISSUER, AUDIENCE, sp.GetRequiredService<DbService>(), sp.GetRequiredService<ElasticService>(), sp.GetRequiredService<Logger<User>>()));
builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");
builder.Services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new[] { new CultureInfo("en"), new CultureInfo("pt-BR") };
options.DefaultRequestCulture = new("pt-BR");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
options.AddInitialRequestCultureProvider(new CustomRequestCultureProvider(context =>
{
var acceptLanguageHeader = context.Request.Headers["Accept-Language"].ToString();
var culture = GetTrueLanguage(acceptLanguageHeader);
return Task.FromResult(new ProviderCultureResult(culture));
}));
});
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo
{
Version = "v1",
Title = "PlayOffs API",
Description = "Uma API ASP.NET Core de administração esportiva",
TermsOfService = new Uri("https://www.playoffs.app.br/pages/termos-de-uso.html"),
Contact = new OpenApiContact
{
Name = "Email",
Email = "equiperodamo@gmail.com"
},
License = new OpenApiLicense
{
Name = "Licença MIT",
Url = new Uri("https://github.com/RoDaMo/pds-back-end/blob/main/LICENSE")
}
});
c.UseAllOfToExtendReferenceSchemas();
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
Description = "JWT Authorization header. É necessário fazer uma requsição POST /auth para obter o token de autenticação.",
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.Http,
Scheme = "Bearer"
});
var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename));
});
builder.Services.AddCors(options =>
{
options.AddPolicy("cors", policy =>
{
policy.WithOrigins("https://localhost:5173", "https://127.0.0.1:5173", AUDIENCE);
policy.AllowAnyHeader();
policy.AllowAnyMethod();
policy.AllowCredentials();
});
});
var app = builder.Build();
// Configure the HTTP request pipeline.
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "PlayOffs API V1");
});
if (app.Environment.IsDevelopment())
{
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseCors("cors");
app.UseAuthentication();
app.UseAuthorization();
app.UseRequestLocalization(app.Services.GetRequiredService<IOptions<RequestLocalizationOptions>>().Value);
app.UseMiddleware<ErrorMiddleware>();
app.MapControllers();
app.Run();
static string GetTrueLanguage(string falseLanguage) => falseLanguage switch
{
"ptbr" => "pt-BR",
"en" => "en",
_ => "pt-BR",
};