-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
150 lines (120 loc) · 4.98 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
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
using Fitster.API.Clothing.Domain.Repositories;
using Fitster.API.Clothing.Domain.Services;
using Fitster.API.Clothing.Persistence.Repositories;
using Fitster.API.Clothing.Services;
using Fitster.API.Security.Authorization.Handlers.Implementations;
using Fitster.API.Security.Authorization.Handlers.Interfaces;
using Fitster.API.Security.Authorization.Middleware;
using Fitster.API.Security.Authorization.Settings;
using Fitster.API.Shared.Domain.Repositories;
using Fitster.API.Shared.Persistence.Contexts;
using Fitster.API.Shared.Persistence.Repositories;
using Fitster.API.Shopping.Domain.Repositories;
using Fitster.API.Shopping.Domain.Services;
using Fitster.API.Shopping.Persistence.Repositories;
using Fitster.API.Shopping.Services;
using Fitster.API.Users.Domain.Repositories;
using Fitster.API.Users.Domain.Services;
using Fitster.API.Users.Persistence.Repositories;
using Fitster.API.Users.Services;
using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Models;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
// Add CORS service
builder.Services.AddCors();
// AppSettings Configuration
builder.Services.Configure<AppSettings>(builder.Configuration.GetSection("AppSettings"));
builder.Services.AddSwaggerGen(options =>
{
// Add API Documentation Information
options.SwaggerDoc("v1", new OpenApiInfo
{
Version = "v1",
Title = "Fitster Project API",
Description = "Fitster Project RESTful API",
TermsOfService = new Uri("https://fitster.com"),
Contact = new OpenApiContact
{
Name = "Fitster Project",
Url = new Uri("https://fitster.com")
},
License = new OpenApiLicense
{
Name = "Fitster Project Resources License",
Url = new Uri("https://fitster.com/license")
}
});
options.EnableAnnotations();
});
// Add Database Connection
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
// Database Connection with Standard Level for Information and Errors
builder.Services.AddDbContext<AppDbContext>(options => options.UseMySQL(connectionString));
// Add lowercase routes
builder.Services.AddRouting(options =>
options.LowercaseUrls = true);
// Dependency Injection Configuration
// Shared Injection Configuration
builder.Services.AddScoped<IUnitOfWork, UnitOfWork>();
builder.Services.AddScoped<IProductRepository, ProductRepository>();
builder.Services.AddScoped<IProductService, ProductService>();
builder.Services.AddScoped<IUserRepository, UserRepository>();
builder.Services.AddScoped<IUserService, UserService>();
builder.Services.AddScoped<IShoppingCartRepository, ShoppingCartRepository>();
builder.Services.AddScoped<IShoppingCartService, ShoppingCartService>();
builder.Services.AddScoped<IShoppingCartItemRepository, ShoppingCartItemRepository>();
builder.Services.AddScoped<IShoppingCartItemService, ShoppingCartItemService>();
builder.Services.AddScoped<IOrderRepository, OrderRepository>();
builder.Services.AddScoped<IOrderService, OrderService>();
builder.Services.AddScoped<IOrderItemRepository, OrderItemRepository>();
builder.Services.AddScoped<IOrderItemService, OrderItemService>();
builder.Services.AddScoped<IWishListItemRepository, WishListItemRepository>();
builder.Services.AddScoped<IWishListRepository, WishListRepository>();
builder.Services.AddScoped<IWishListItemService, WishListItemService>();
builder.Services.AddScoped<IWishListService, WishListService>();
// Security Injection Configuration
builder.Services.AddScoped<IJwtHandler, JwtHandler>();
// AutoMapper Configuration
builder.Services.AddAutoMapper(
typeof(Fitster.API.Clothing.Mapping.ModelToResourceProfile),
typeof(Fitster.API.Clothing.Mapping.ResourceToModelProfile),
typeof(Fitster.API.Shopping.Mapping.ModelToResourceProfile),
typeof(Fitster.API.Shopping.Mapping.ResourceToModelProfile),
typeof(Fitster.API.Users.Mapping.ModelToResourceProfile),
typeof(Fitster.API.Users.Mapping.ResourceToModelProfile)
);
var app = builder.Build();
// Configure CORS
app.UseCors(x => x
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader());
// Configure Error Handler Middleware
app.UseMiddleware<ErrorHandlerMiddleware>();
// Configure JWT Handling Middleware
app.UseMiddleware<JwtMiddleware>();
// Validation for ensuring Database Objects are created
using (var scope = app.Services.CreateScope())
using (var context = scope.ServiceProvider.GetService<AppDbContext>())
{
context.Database.EnsureCreated();
}
// Configure the HTTP request pipeline.
if (app.Environment.IsProduction
())
{
app.UseSwagger();
app.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("v1/swagger.json", "v1");
options.RoutePrefix = "swagger";
});
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();