-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
56 lines (48 loc) · 1.86 KB
/
Program.cs
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
using Invoices_Manager_API;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using System.Text;
// Create builder.
var builder = WebApplication.CreateBuilder(args);
// Get the connection string from the appsettings.json file
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
// Check if the connection string is valid
if (connectionString is null)
throw new Exception("Connection string is null");
// Add services to the builder.
builder.Services.AddControllers();
builder.Services.Configure<FormOptions>(options =>
{
options.MultipartBodyLengthLimit = 33554432; // 32 MB
});
builder.Services.AddDbContext<DataBaseContext>(options =>
{
options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString));
}, ServiceLifetime.Transient);
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = builder.Configuration["Jwt:Issuer"],
ValidAudience = builder.Configuration["Jwt:Audience"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["Jwt:SymmetricSecurityKey"]))
};
});
// Will clear the Cache folder, since the files are no longer assigned to a thread anyway (zombie data)
await Task.Run(() => FileCore.ClearCacheFolder());
// Create app (API)
var app = builder.Build();
// Configure the HTTP request pipeline.
app.UseHttpsRedirection();
app.UseAuthorization();
app.UseRouting();
app.MapControllers();
// Run app
app.Run();