-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
executable file
·77 lines (68 loc) · 2.26 KB
/
Program.cs
File metadata and controls
executable file
·77 lines (68 loc) · 2.26 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
using Microsoft.EntityFrameworkCore;
using PUMP_BACKEND.Data;
using PUMP_BACKEND.Middlewares;
using Microsoft.IdentityModel.Tokens;
using System.Text;
using PUMP_BACKEND.Services.Interfaces;
using PUMP_BACKEND.Data.Configurations;
var builder = WebApplication.CreateBuilder(args);
string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
builder.Services.AddCors(options =>
{
options.AddPolicy(name: MyAllowSpecificOrigins,
builder =>
{
builder.WithOrigins(
"http://localhost:3000",
"http://tenant1.localhost:3000",
"http://tenant2.localhost:3000"
)
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
});
//Register the DbContext
builder.Services.AddDbContext<PumpDbContext>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection"))
);
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddScoped<ITokenService, TokenService>();
builder.Services.AddHttpContextAccessor();
builder.Services.AddTransient<DataSeeder>();
builder.Services.AddAuthentication("Bearer")
.AddJwtBearer("Bearer", options =>
{
var secretKey = builder.Configuration["Jwt:Secret"];
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = false,
ValidateAudience = false,
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(secretKey!)),
ClockSkew = TimeSpan.Zero
};
});
var app = builder.Build();
// Seed the database before app.Run()
using (var scope = app.Services.CreateScope())
{
var seeder = scope.ServiceProvider.GetRequiredService<DataSeeder>();
seeder.Seed();
}
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
//Middleware pipeline
app.UseAuthentication();
app.UseAuthorization();
app.UseMiddleware<TenantResolutionMiddleware>();
app.UseCors(MyAllowSpecificOrigins);
app.UseHttpsRedirection();
app.MapControllers();
app.Run();