-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
114 lines (94 loc) · 3.22 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using System.Text;
using KnowledgeFlowApi.Attributes;
using KnowledgeFlowApi.Data;
using KnowledgeFlowApi.Handlers;
using KnowledgeFlowApi.Options;
using KnowledgeFlowApi.Services.CommentServices;
using KnowledgeFlowApi.Services.FileItemServices;
using KnowledgeFlowApi.Services.UserServices;
using LibraryManagementSystemAPI.Services.SendEmailServices;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
/*
* 2850 lines of code
* future features:
* Implement virus scanning
* enable users to like or dislike a file
* update user password
*/
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
#region database connection
builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(
builder.Configuration.GetConnectionString("Default")
));
#endregion
#region options
builder.Services.Configure<JwtOptions>(builder.Configuration.GetSection("JwtOptions"));
builder.Services.Configure<EmailOptions>(builder.Configuration.GetSection("EmailOptions"));
#endregion
#region dependency injection
builder.Services.AddControllers();
builder.Services.AddSingleton<FileHandler>();
builder.Services.AddScoped<FileService>();
builder.Services.AddScoped<AuthUserService>();
builder.Services.AddScoped<SendEmailService>();
builder.Services.AddScoped<UserProfileService>();
builder.Services.AddScoped<CommentService>();
builder.Services.AddScoped<RatingService>();
builder.Services.AddScoped<ReportService>();
builder.Services.AddSignalR();
#endregion
#region Attributes
builder.Services.AddControllers(options =>
{
options.Filters.Add<BannedUserAttribute>();
});
#endregion
#region Cors
builder.Services.AddCors(options =>
{
options.AddDefaultPolicy(
policy =>
{
policy.WithOrigins("*").AllowAnyMethod().AllowAnyHeader(); ;
});
});
#endregion
#region Jwt Auth
var jwtOptions = builder.Configuration.GetSection("JwtOptions").Get<JwtOptions>();
object value = builder.Services.AddAuthentication()
.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options =>
{
options.SaveToken = true; // to allow getting token string from HttpContext
options.TokenValidationParameters = new TokenValidationParameters
{
ClockSkew = TimeSpan.Zero, // to expire token directly after finishing its determined duration
ValidateIssuer = true,
ValidIssuer = jwtOptions.Issuer,
ValidateAudience = true,
ValidAudience = jwtOptions.Audience,
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtOptions.SigningKey))
};
});
#endregion
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseStaticFiles();
app.UseCors();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.MapHub<CommentsHub>("/hubs/commentsHub");
app.UseHttpsRedirection();
app.Run();