This repository was archived by the owner on Nov 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
168 lines (152 loc) · 6.38 KB
/
Copy pathProgram.cs
File metadata and controls
168 lines (152 loc) · 6.38 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
using BackEnd.Models;
using BackEnd.Repository;
using BackEnd.Service;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using Newtonsoft.Json;
using System.Text;
using Microsoft.Extensions.Options;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.AspNetCore.Diagnostics;
using System.Net;
using BackEnd.Middlewares;
using VNPAY_CS_ASPX;
using BackEnd.Repository.RepositoryImpl;
using BackEnd.Service.ServiceImpl;
// Khởi tạo ứng dụng
var builder = WebApplication.CreateBuilder(args);
// Thêm dịch vụ vào container
builder.Services.AddControllers()
.AddNewtonsoftJson(options =>
{
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
});
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// Cấu hình xác thực JWT Bearer
builder.Services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = 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:Key"]))
};
})
.AddGoogle(options =>
{
options.ClientId = builder.Configuration["Google:ClientId"];
options.ClientSecret = builder.Configuration["Google:ClientSecret"];
});
builder.Services.AddAuthorization(options =>
{
options.AddPolicy("AdminRole", policy => policy.RequireRole("ADMIN"));
options.AddPolicy("UserRole", policy => policy.RequireRole("USER"));
});
// Thêm DbContext
builder.Services.AddDbContext<BookStoreContext>(options =>
options.UseMySql(builder.Configuration.GetConnectionString("BookStoreContext"),
ServerVersion.AutoDetect(builder.Configuration.GetConnectionString("BookStoreContext"))));
// CORS: cho phép các nguồn cụ thể
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowSpecificOrigins",
builder => builder.WithOrigins("http://localhost:5173", "http://localhost:5174", "http://localhost:5001", "http://localhost:5000")
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials()
.WithExposedHeaders("Authorization")); // Cho phép expose Authorization header nếu cần
});
// Thêm các dịch vụ và repository
builder.Services.AddScoped<ICollectionRepository, CollectionRepository>();
builder.Services.AddScoped<ICollectionService, CollectionService>();
builder.Services.AddScoped<IBookRepository, BookRepository>();
builder.Services.AddScoped<IBookService, BookService>();
builder.Services.AddScoped<IUserRepository, UserRepository>();
builder.Services.AddScoped<IUserService, UserService>();
builder.Services.AddScoped<IEmailService, EmailService>();
builder.Services.AddScoped<IOrderRepository, OrderRepository>();
builder.Services.AddScoped<IOrderService, OrderService>();
builder.Services.AddScoped<IPostCategoryRepository, PostCategoryRepository>();
builder.Services.AddScoped<IPostCategoryService, PostCategoryService>();
builder.Services.AddScoped<IPostRepository, PostRepository>();
builder.Services.AddScoped<IPostService, PostService>();
builder.Services.AddScoped<IPublisherRepository, PublisherRepository>();
builder.Services.AddScoped<IPublisherService, PublisherService>();
builder.Services.AddScoped<IAuthorRepository, AuthorRepository>();
builder.Services.AddScoped<IAuthorService, AuthorService>();
builder.Services.AddScoped<ISliderRepository, SliderRepository>();
builder.Services.AddScoped<ISliderService, SliderService>();
builder.Services.AddScoped<IJwtService, JwtService>(); // Register JWTService
builder.Services.AddScoped<IFeedBackRepository, FeedBackRepository>();
builder.Services.AddScoped<IFeedBackService,FeedBackService>();
builder.Services.AddSignalR();
builder.Services.AddScoped<IWishListRepository, WishlistRepository>();
builder.Services.AddScoped<IWishlistService, WishlistService>();
builder.Services.AddScoped<ICartService, CartService>();
builder.Services.AddScoped<IRefreshTokenService, RefreshTokenService>();
builder.Services.AddScoped<IRefreshTokenRepository, RefreshTokenRepository>();
builder.Services.AddScoped<IOrderDetailRepository, OrderDetailRepository>();
builder.Services.AddScoped<Payment, Payment>();
builder.Services.AddHttpContextAccessor();
builder.Services.AddScoped<IAdsRepository, AdsRepository>();
builder.Services.AddScoped<IAdsService, AdsService>();
builder.Services.AddScoped<IChartRepository, ChartRepository>();
builder.Services.AddScoped<IChartService, ChartService>();
var app = builder.Build();
// Cấu hình pipeline HTTP request
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler(errorApp =>
{
errorApp.Run(async context =>
{
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
context.Response.ContentType = "application/json";
var exceptionHandlerPathFeature = context.Features.Get<IExceptionHandlerPathFeature>();
var errorResponse = new
{
message = "Đã xảy ra lỗi!",
detail = exceptionHandlerPathFeature?.Error.Message
};
await context.Response.WriteAsync(JsonConvert.SerializeObject(errorResponse));
});
});
}
app.UseHttpsRedirection();
app.UseRouting();
// Áp dụng CORS
app.UseCors("AllowSpecificOrigins");
// Kích hoạt middleware xác thực
app.UseAuthentication();
app.UseAuthorization();
// Đăng ký middleware xác thực token (nếu có)
app.UseMiddleware<TokenValidationMiddleware>();
// app.UseEndpoints(endpoints =>
// {
// endpoints.MapControllers();
// endpoints.MapHub<CommentHub>("/commentHub");
// });
app.MapControllers();
app.MapHub<CommentHub>("/commentHub");
app.Run();