-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
165 lines (139 loc) · 5.21 KB
/
Program.cs
File metadata and controls
165 lines (139 loc) · 5.21 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
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using System.Text.Json.Serialization;
using PraOndeFoi.Data;
using PraOndeFoi.Repository;
using PraOndeFoi.Services;
using Supabase;
using Quartz;
using QuestPDF.Infrastructure;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();
builder.Services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
});
QuestPDF.Settings.License = LicenseType.Community;
builder.Services.AddSingleton<Client>(provider =>
{
var url = builder.Configuration["Supabase:Url"];
var key = builder.Configuration["Supabase:AnonKey"];
if (string.IsNullOrWhiteSpace(url) || string.IsNullOrWhiteSpace(key))
{
throw new InvalidOperationException("Configuração do Supabase ausente.");
}
return new Client(url, key);
});
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
builder.Services.AddMemoryCache(options =>
{
options.SizeLimit = 1024;
});
const long uploadMaxBytes = 49L * 1024 * 1024;
builder.Services.Configure<FormOptions>(options =>
{
options.MultipartBodyLengthLimit = uploadMaxBytes;
options.ValueLengthLimit = (int)uploadMaxBytes;
options.MultipartHeadersLengthLimit = 64 * 1024;
});
builder.WebHost.ConfigureKestrel(options =>
{
options.Limits.MaxRequestBodySize = uploadMaxBytes;
});
builder.Services.AddScoped<IContaService, ContaService>();
builder.Services.AddScoped<IFinancasService, FinancasService>();
builder.Services.AddScoped<IExportacaoService, ExportacaoService>();
builder.Services.AddScoped<IImportacaoService, ImportacaoService>();
builder.Services.AddScoped<IContaCacheService, ContaCacheService>();
builder.Services.AddScoped<IAnexoStorageService, SupabaseAnexoStorageService>();
builder.Services.AddScoped<IContaRepository, ContaRepository>();
builder.Services.AddScoped<IFinancasRepository, FinancasRepository>();
var intervaloRecorrencias = builder.Configuration.GetValue<int?>("Jobs:Recorrencias:IntervalMinutes") ?? 60;
intervaloRecorrencias = Math.Max(1, intervaloRecorrencias);
builder.Services.AddQuartz(options =>
{
var jobKey = new JobKey("RecorrenciasJob");
options.AddJob<RecorrenciasJob>(job => job.WithIdentity(jobKey));
options.AddTrigger(trigger => trigger
.ForJob(jobKey)
.WithIdentity("RecorrenciasJob-trigger")
.StartNow()
.WithSimpleSchedule(schedule => schedule
.WithInterval(TimeSpan.FromMinutes(intervaloRecorrencias))
.RepeatForever()));
});
builder.Services.AddQuartzHostedService(options =>
{
options.WaitForJobsToComplete = true;
});
builder.Services.AddAuthorization();
builder.Services.AddCors(options =>
{
var allowedOriginsSetting = builder.Configuration["AllowedOrigins"];
string[] allowedOrigins = Array.Empty<string>();
if (!string.IsNullOrWhiteSpace(allowedOriginsSetting))
{
allowedOrigins = allowedOriginsSetting.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
}
else if (builder.Environment.IsDevelopment())
{
allowedOrigins = new[] { "http://localhost:4200" };
}
options.AddPolicy("DefaultCorsPolicy", policy =>
{
if (allowedOrigins.Length == 0)
{
policy.AllowAnyMethod()
.AllowAnyHeader();
}
else if (allowedOrigins.Length == 1 && allowedOrigins[0] == "*")
{
policy.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
}
else
{
policy.WithOrigins(allowedOrigins)
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
}
});
});
builder.Services.AddAuthentication().AddJwtBearer(options =>
{
options.Authority = builder.Configuration["Authentication:Authority"];
options.RequireHttpsMetadata = true;
options.SaveToken = true;
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidIssuer = builder.Configuration["Authentication:ValidIssuer"],
ValidateAudience = true,
ValidAudience = builder.Configuration["Authentication:ValidAudience"],
ValidateLifetime = true,
ValidateIssuerSigningKey = true
};
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}
app.UseHttpsRedirection();
var _corsOrigins = builder.Configuration["AllowedOrigins"];
var _logger = app.Services.GetRequiredService<ILogger<Program>>();
_logger.LogInformation("CORS configurado. AllowedOrigins: {AllowedOrigins}", string.IsNullOrWhiteSpace(_corsOrigins) ? (app.Environment.IsDevelopment() ? "http://localhost:4200 (dev default)" : "none") : _corsOrigins);
app.UseCors("DefaultCorsPolicy");
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();