-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
145 lines (118 loc) · 5.5 KB
/
Program.cs
File metadata and controls
145 lines (118 loc) · 5.5 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
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using System.Text;
using SPlaceBackend.Infrastructure;
using SPlaceBackend.Infrastructure.Repositories;
using SPlaceBackend.Application.Services;
using SPlaceBackend.Infrastructure.Hubs;
using SPlaceBackend.Application.Interfaces.Repositories;
using SPlaceBackend.Application.Interfaces.Authentication;
using SPlaceBackend.Infrastructure.Authentication;
using SPlaceBackend.Application.Interfaces.Services;
using SPlaceBackend.Application.ExceptionsHandler;
using SPlaceBackend.Application.Interfaces.Infrastructure;
namespace SPlaceBackend
{
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo { Title = "SPlace API", Version = "v1" });
var jwtSecurityScheme = new OpenApiSecurityScheme
{
BearerFormat = "JWT",
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.Http,
Scheme = JwtBearerDefaults.AuthenticationScheme,
Description = "Enter 'Bearer <your-token>'",
Reference = new OpenApiReference
{
Id = JwtBearerDefaults.AuthenticationScheme,
Type = ReferenceType.SecurityScheme,
}
};
options.AddSecurityDefinition("Bearer", jwtSecurityScheme);
options.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{ jwtSecurityScheme, Array.Empty<string>() }
});
});
builder.Services.AddDbContext<SPlaceContext>(options =>
{
options.UseSqlServer(builder.Configuration.GetConnectionString("SqlServerConnStr"));
});
builder.Services.Configure<JwtOptions>(builder.Configuration.GetSection(nameof(JwtOptions)));
builder.Services.AddScoped<IUsersRepository, UsersRepository>()
.AddScoped<IGroupsRepository, GroupsRepository>()
.AddScoped<IUserGroupsRepository, UserGroupsRepository>()
.AddScoped<IUserMessagesRepository, UserMessagesRepository>()
.AddScoped<IQuizzesRepository, QuizzesRepository>()
.AddScoped<IQuestionsRepository, QuestionsRepository>()
.AddScoped<IAnswersRepository, AnswersRepository>()
.AddScoped<IScoresRepository, ScoresRepository>();
builder.Services.AddScoped<IPasswordHasher, PasswordHasher>()
.AddScoped<IJwtGenerator, JwtGenerator>()
.AddScoped<IUnitOfWork, UnitOfWork>();
builder.Services.AddScoped<IUserService, UserService>()
.AddScoped<IGroupService, GroupService>()
.AddScoped<IMessageService, MessageService>()
.AddScoped<IQuizService, QuizService>();
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = false,
ValidateAudience = false,
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["JwtOptions:SecretKey"]!)),
ValidateLifetime = true,
};
});
builder.Services.AddCors(option =>
{
option.AddDefaultPolicy(builder =>
{
builder
.WithOrigins("http://localhost:4200")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
builder
.WithOrigins("https://splaceclient.azurewebsites.net")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
});
builder.Services.AddSignalR();
builder.Services.AddSingleton<ApplicationHub>();
builder.Services.AddControllers();
builder.Services.AddProblemDetails()
.AddExceptionHandler<GlobalExceptionsHandler>();
var app = builder.Build();
if (builder.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "SPlaceBackend V1"));
}
app.UseHttpsRedirection();
app.UseExceptionHandler();
app.UseCors();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapHub<ApplicationHub>("/application");
app.MapControllers();
app.Run();
}
}
}