Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
175 lines (152 loc) · 5.22 KB
/
Copy pathProgram.cs
File metadata and controls
175 lines (152 loc) · 5.22 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
165
166
167
168
169
170
171
172
173
174
175
using System.Text;
using leapcert_back.Context;
using leapcert_back.Helpers;
using leapcert_back.Interfaces;
using leapcert_back.Mappers;
using leapcert_back.Models;
using leapcert_back.Repository;
using leapcert_back.Responses;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.OpenApi.Models;
using leapcert_back.Context;
using Minio;
var builder = WebApplication.CreateBuilder(args);
const long maxUploadBytes = 5L * 1024 * 1024 * 1024;
builder.Services.Configure<FormOptions>(o =>
{
o.MultipartBodyLengthLimit = maxUploadBytes;
});
builder.WebHost.ConfigureKestrel(o =>
{
o.Limits.MaxRequestBodySize = maxUploadBytes;
});
// Configuração do CORS
builder.Services.AddCors(options =>
{
options.AddPolicy("CorsPolicy", corsBuilder =>
{
var allowedOrigins = builder.Configuration
.GetSection("CORS:AllowedOrigins")
.Get<string[]>();
if (allowedOrigins is null || allowedOrigins.Length == 0)
{
throw new InvalidOperationException("Nenhuma origem foi configurada em CORS:AllowedOrigins.");
}
corsBuilder.WithOrigins(allowedOrigins);
corsBuilder.AllowAnyMethod();
corsBuilder.AllowAnyHeader();
corsBuilder.AllowCredentials();
});
});
// Adiciona controllers
builder.Services.AddControllers();
// Obtém a string de conexão
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
if (string.IsNullOrEmpty(connectionString))
{
throw new InvalidOperationException("A string de conexão não foi encontrada no appsettings.json.");
}
// Configuração do banco de dados SQL server
builder.Services.AddDbContext<ApplicationDbContext>(options =>
{
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"));
});
// Configuração do Swagger + JWT
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo
{
Title = "LeapCertService",
Version = "v1",
Description = "API do LeapCert muito froggers.",
});
// 🔐 Configuração do esquema JWT
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
Description = @"Insira o token JWT desta forma: **Bearer {seu token}**",
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.ApiKey,
Scheme = "Bearer"
});
c.AddSecurityRequirement(new OpenApiSecurityRequirement()
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
},
Scheme = "oauth2",
Name = "Bearer",
In = ParameterLocation.Header
},
new List<string>()
}
});
});
//Configuracao do JWT
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:Secret"]))
};
options.Events = new JwtBearerEvents
{
OnMessageReceived = context =>
{
var token = context.Request.Cookies["accessToken"];
if (!string.IsNullOrEmpty(token))
{
context.Token = token;
}
return Task.CompletedTask;
}
};
});
builder.Services.AddAuthorization();
builder.Services.AddMinio(configureClient => configureClient
.WithEndpoint(builder.Configuration["MinIO:EndPoint"])
.WithCredentials(builder.Configuration["MinIO:AccessKey"], builder.Configuration["MinIO:SecretKey"])
.WithSSL(builder.Configuration.GetValue("MinIO:UseSSL", true))
.Build());
builder.Services.AddScoped<IUserRepository, UserRepository>();
builder.Services.AddScoped<IModulesRepository, ModulesRepository>();
builder.Services.AddScoped<IClassRepository, ClassRepository>();
builder.Services.AddScoped<ITeacherRepository, TeacherRepository>();
builder.Services.AddScoped<IMinIoRepository, MinIoRepository>();
builder.Services.AddScoped<IGeneralRespository, GeneralRepository>();
builder.Services.AddScoped<HelperService>();
builder.Services.AddScoped<JwtService>();
builder.Services.AddScoped<UserMapper>();
var app = builder.Build();
// Middleware do Swagger
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "LeapCert v1");
c.RoutePrefix = string.Empty;
});
// Middleware do CORS
app.UseCors("CorsPolicy");
// Habilita WebSockets
app.UseWebSockets();
app.UseAuthentication();
app.UseAuthorization();
// Mapeia os controllers
app.MapControllers();
// Executa a aplicação
await app.RunAsync();