Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
66 lines (54 loc) · 2.51 KB
/
Copy pathProgram.cs
File metadata and controls
66 lines (54 loc) · 2.51 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
using System.Text;
using System.Text.Json.Serialization;
using BulkDataProcessingPlatform.Api.Data;
using BulkDataProcessingPlatform.Api.Entities;
using BulkDataProcessingPlatform.Api.OpenApi;
using BulkDataProcessingPlatform.Api.Processing;
using BulkDataProcessingPlatform.Api.Services;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers().AddJsonOptions(o => o.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()));
builder.Services.AddOpenApi(o => o.AddDocumentTransformer<BearerSecuritySchemeTransformer>());
builder.Services.AddDbContext<AppDbContext>(o => o.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
builder.Services.Configure<ProcessingOptions>(builder.Configuration.GetSection("Processing"));
builder.Services.Configure<FormOptions>(o => o.MultipartBodyLengthLimit = long.MaxValue);
builder.Services.AddSingleton<IPasswordHasher<User>, PasswordHasher<User>>();
builder.Services.AddSingleton<FileStorage>();
builder.Services.AddScoped<AuthService>();
builder.Services.AddScoped<AuditService>();
builder.Services.AddScoped<JobService>();
builder.Services.AddScoped<JobImporter>();
builder.Services.AddHostedService<JobWorker>();
var jwt = builder.Configuration.GetSection("Jwt");
var signingKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(
jwt["Key"] ?? throw new InvalidOperationException("Jwt:Key is not configured.")));
builder.Services.AddSingleton(signingKey);
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(o =>
o.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidIssuer = jwt["Issuer"],
ValidateAudience = true,
ValidAudience = jwt["Audience"],
ValidateLifetime = true,
IssuerSigningKey = signingKey,
ClockSkew = TimeSpan.Zero
});
var app = builder.Build();
using (var scope = app.Services.CreateScope())
await scope.ServiceProvider.GetRequiredService<AppDbContext>().Database.MigrateAsync();
app.UseHttpsRedirection();
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
app.UseSwaggerUI(o => o.SwaggerEndpoint("/openapi/v1.json", "v1"));
}
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();