- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
Latest commit
124 lines (102 loc) · 3.96 KB
/
Copy pathProgram.cs
File metadata and controls
124 lines (102 loc) · 3.96 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
usingAutoMapper;
usingAutoMapper.EquivalencyExpression;
usingMicrosoft.AspNetCore.Authentication.JwtBearer;
usingMicrosoft.EntityFrameworkCore;
usingMicrosoft.IdentityModel.Tokens;
usingMicrosoft.OpenApi.Models;
usingMicrosoft.Extensions.Logging.Abstractions;
usingNorthWindAPI.Controllers.Mappers;
usingNorthWindAPI.Data.Context;
usingNorthWindAPI.Data.Repositories;
usingNorthWindAPI.Data.RepositoryInterfaces;
usingNorthWindAPI.Services;
usingNorthWindAPI.Services.Interfaces;
usingNorthWindAPI.Services.Mappers;
usingNorthWindAPI.Views;
usingNorthWindAPI.Views.Interfaces;
usingSystem.Text;
varbuilder=WebApplication.CreateBuilder(args);
varconfiguration=builder.Configuration;
/////////////////////////////////////////////////////////////
// Token Auth Configuration
/////////////////////////////////////////////////////////////
varjwtIssuer=configuration.GetSection("Jwt:Issuer").Get<string>();
varjwtKey=configuration.GetSection("Jwt:Key").Get<string>();
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options =>
{
options.TokenValidationParameters=newTokenValidationParameters
{
ValidateIssuer=true,
ValidateAudience=true,
ValidateLifetime=true,
ValidateIssuerSigningKey=true,
ValidIssuer=jwtIssuer,
ValidAudience=jwtIssuer,
IssuerSigningKey=newSymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtKey))
};
});
/////////////////////////////////////////////////////////////
// Register Servies
/////////////////////////////////////////////////////////////
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseSqlite(configuration.GetConnectionString("SqliteConnection")));
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1",newOpenApiInfo{
Title="Northwind API",
Description="REST API backend for NorthWindWeb",
Version="v1"
});
c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory,"NorthWindAPIAnnotation.xml"));
});
//Repositories
builder.Services.AddScoped<IOrderRepository,OrderRepository>();
builder.Services.AddScoped<IEmployeeRepository,EmployeeRepository>();
builder.Services.AddScoped<ICustomerRepository,CustomerRepository>();
builder.Services.AddScoped<IProductRepository,ProductRepository>();
//Services
builder.Services.AddCors();
builder.Services.AddScoped<IAuthService,AuthService>();
builder.Services.AddScoped<IOrderService,OrderService>();
builder.Services.AddScoped<ICustomerService,CustomerService>();
builder.Services.AddScoped<IUserService,UserService>();
builder.Services.AddScoped<IProductService,ProductService>();
//Views
builder.Services.AddScoped<IDashboardView,DashboardView>();
// Auto Mapper Config
varmapperConfig=newMapperConfiguration(cfg =>
{
cfg.AddProfile(newOrderDtoMap());
cfg.AddProfile(newOrderRequestMap());
cfg.AddProfile(newCustomerDtoMap());
cfg.AddProfile(newCustomerRequestMap());
cfg.AddProfile(newCustomerResponseMap());
cfg.AddProfile(newEmployeeResponseMap());
cfg.AddProfile(newProductDtoMap());
cfg.AddProfile(newProductResponseMap());
cfg.AddProfile(newOrderResponseMap());
cfg.AddProfile(newUserDtoMap());
cfg.AddCollectionMappers();
cfg.UseEntityFrameworkCoreModel<AppDbContext>();
},NullLoggerFactory.Instance);
IMappermapper=mapperConfig.CreateMapper();
builder.Services.AddSingleton(mapper);
varapp=builder.Build();
// Configure the HTTP request pipeline
if(app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI(options =>
{
options.DefaultModelsExpandDepth(-1);
});
}
app.UseHttpsRedirection();
//Enable CORS to allow anything from dev host
app.UseCors(x =>x.AllowAnyHeader().AllowAnyMethod().WithOrigins("http://localhost:3000","http://localhost:8000"));
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();