-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
95 lines (77 loc) · 2.67 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
91
92
93
94
95
using System;
using System.Text;
using DotNetEnv;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using MotelAPI.Configurations;
using MotelAPI.Services;
Env.Load();
var builder = WebApplication.CreateBuilder(args);
var dbPassword = Environment.GetEnvironmentVariable("DB_PASSWORD");
var secretKey = Environment.GetEnvironmentVariable("SECRET_KEY");
if (string.IsNullOrEmpty(dbPassword))
{
throw new InvalidOperationException("A senha do banco de dados não foi fornecida.");
}
if (string.IsNullOrEmpty(secretKey))
{
throw new InvalidOperationException("A chave secreta não foi fornecida.");
}
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
if (string.IsNullOrEmpty(connectionString))
{
throw new InvalidOperationException("A string de conexão não foi fornecida.");
}
connectionString = connectionString.Replace("#{DB_PASSWORD}#", dbPassword);
builder.Services.AddDbContext<MotelAPI.Data.MotelDbContext>(options =>
options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString))
);
builder.Services.Configure<JwtSettings>(builder.Configuration.GetSection("JwtSettings"));
builder.Services.AddScoped<IReservationService, ReservationService>();
builder.Services.AddScoped<FinanceService>();
builder.Services.AddScoped<AuthService>();
builder
.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = builder.Configuration["JwtSettings:Issuer"],
ValidAudience = builder.Configuration["JwtSettings:Audience"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secretKey)),
};
});
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(options =>
{
options.SwaggerDoc(
"v1",
new Microsoft.OpenApi.Models.OpenApiInfo
{
Title = "Motel API",
Version = "v1",
Description = "API para gerenciamento de reservas e motéis.",
}
);
});
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Motel API v1");
c.RoutePrefix = string.Empty;
});
}
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();