-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
90 lines (77 loc) · 3.06 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
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
using FM_API.Persistance.Database;
using FMAPI.Persistance.Repositories;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption;
using Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ConfigurationModel;
using Microsoft.IdentityModel.Tokens;
using System.Text;
using System.Text.Json.Serialization;
var builder = WebApplication.CreateBuilder(args);
string port = Environment.GetEnvironmentVariable("PORT") ?? "8080";
string url = $"http://0.0.0.0:{port}";
string dbconnection = Environment.GetEnvironmentVariable("FMDATABASE");
#region Cors
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAnyOrigin", policy =>
{
policy.AllowAnyHeader();
policy.AllowAnyMethod();
policy.AllowAnyOrigin();
policy.SetIsOriginAllowed(origin => true);
});
});
#endregion
#region Project Senttings
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options =>
{
options.RequireHttpsMetadata = false;
options.SaveToken = true;
options.TokenValidationParameters = new TokenValidationParameters()
{
ValidateIssuer = true,
ValidateAudience = true,
ValidAudience = builder.Configuration["Jwt:Audience"],
ValidIssuer = builder.Configuration["Jwt:Issuer"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"]))
};
});
builder.Services.AddControllersWithViews().AddControllersAsServices().AddJsonOptions(option =>
{
option.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
});
builder.Services.AddRazorPages().AddRazorRuntimeCompilation();
builder.Services.AddDbContext<DbContext, FMContext>(options => options.UseNpgsql(dbconnection));
builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
#endregion
// Rotativa Config
Rotativa.AspNetCore.RotativaConfiguration.Setup(builder.Environment.ContentRootPath);
#region Region Repository
builder.Services.AddTransient<EstimateRepository>();
builder.Services.AddTransient<BudgetRepository>();
builder.Services.AddTransient<RolRepository>();
builder.Services.AddTransient<TransactionRepository>();
builder.Services.AddTransient<UserRepository>();
builder.Services.AddTransient<CategoryRepository>();
builder.Services.AddTransient<TypeRepository>();
#endregion
builder.Services.AddDataProtection().PersistKeysToFileSystem(new DirectoryInfo(@"temp-keys\"))
.UseCryptographicAlgorithms(new AuthenticatedEncryptorConfiguration()
{
EncryptionAlgorithm = EncryptionAlgorithm.AES_256_CBC,
ValidationAlgorithm = ValidationAlgorithm.HMACSHA256
});
var app = builder.Build();
app.UseCors("AllowAnyOrigin");
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run(url);