-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
192 lines (167 loc) · 7.46 KB
/
Program.cs
File metadata and controls
192 lines (167 loc) · 7.46 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using FoodAppG4.Data;
using FoodAppG4.Models;
using FoodAppG4.Services;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using Serilog;
using MongoDB.Driver;
public class Program
{
public static async Task Main(string[] args)
{
try
{
var builder = WebApplication.CreateBuilder(args);
// Configure Serilog
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(builder.Configuration)
.Enrich.FromLogContext()
.CreateLogger();
builder.Host.UseSerilog();
// Register MongoDB client
var mongoConnectionString = builder.Configuration.GetConnectionString("MongoDB");
builder.Services.AddSingleton<IMongoClient>(sp => new MongoClient(mongoConnectionString));
// Add services to the container.
builder.Services.AddDbContext<FoodAppG4Context>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
// Register application services
builder.Services.AddScoped<CookService>();
builder.Services.AddScoped<CustomerService>();
builder.Services.AddScoped<CyclistService>();
builder.Services.AddScoped<OrderService>();
builder.Services.AddScoped<DishService>();
builder.Services.AddScoped<OrderDetailService>();
builder.Services.AddScoped<TripService>();
builder.Services.AddScoped<TripStopService>();
builder.Services.AddScoped<SalaryService>();
builder.Services.AddScoped<RatingService>();
builder.Services.AddScoped<QueryService>();
builder.Services.AddScoped<LogService>();
// Add Identity
builder.Services.AddIdentity<ApiUser, IdentityRole>(options =>
{
options.Password.RequireDigit = true;
options.Password.RequireLowercase = true;
options.Password.RequireUppercase = true;
options.Password.RequireNonAlphanumeric = true;
options.Password.RequiredLength = 8;
})
.AddEntityFrameworkStores<FoodAppG4Context>();
// Add JWT authentication
builder.Services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme =
options.DefaultChallengeScheme =
options.DefaultForbidScheme =
options.DefaultScheme =
options.DefaultSignInScheme =
options.DefaultSignOutScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options =>
{ // Enable JWT bearer authentication
options.TokenValidationParameters = new TokenValidationParameters
{ // Configure the JWT validation, issuing, and lifetime settings
ValidateIssuer = true,
ValidIssuer = builder.Configuration["JWT:Issuer"],
ValidateAudience = true,
ValidAudience = builder.Configuration["JWT:Audience"],
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(
System.Text.Encoding.UTF8.GetBytes(
builder.Configuration["JWT:SigningKey"]))
};
});
// Authorization Policies...
builder.Services.AddAuthorization(options =>
{
options.AddPolicy("AdminOnly", policy =>
policy.RequireAssertion(context =>
context.User.HasClaim(c => c.Type == "IsAdmin" && c.Value == "true")));
options.AddPolicy("AdminOrManagerOnly", policy =>
policy.RequireAssertion(context =>
context.User.HasClaim(c => c.Type == "IsAdmin" && c.Value == "true") ||
context.User.HasClaim(c => c.Type == "IsManager" && c.Value == "true")));
options.AddPolicy("AdminOrCookOnly", policy =>
policy.RequireAssertion(context =>
context.User.HasClaim(c => c.Type == "IsAdmin" && c.Value == "true") ||
context.User.HasClaim(c => c.Type == "IsCook" && c.Value == "true")));
options.AddPolicy("AdminOrCyclistOnly", policy =>
policy.RequireAssertion(context =>
context.User.HasClaim(c => c.Type == "IsAdmin" && c.Value == "true") ||
context.User.HasClaim(c => c.Type == "IsCyclist" && c.Value == "true")));
});
// Add controllers
builder.Services.AddControllers();
// Swagger Configuration
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(options =>
{
options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
In = ParameterLocation.Header,
Description = "Please enter JWT with Bearer prefix",
Name = "Authorization",
Type = SecuritySchemeType.Http,
BearerFormat = "JWT",
Scheme = "bearer"
});
options.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
},
Array.Empty<string>()
}
});
});
// Ensure the app listens on all network interfaces
builder.WebHost.ConfigureKestrel(options =>
{
options.ListenAnyIP(80); // Listen on port 80 on all network interfaces
});
var app = builder.Build();
// Configure the HTTP request pipeline.
app.UseSwagger();
app.UseSwaggerUI();
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
using (var scope = app.Services.CreateScope())
{
// Get the service provider
var serviceProvider = scope.ServiceProvider;
// Apply migrations at startup
var dbContext = serviceProvider.GetRequiredService<FoodAppG4Context>();
dbContext.Database.Migrate();
// Seed the database with users
var userManager = serviceProvider.GetService<UserManager<ApiUser>>();
if (userManager != null)
{
await SeedData.SeedUsersAsync(userManager);
}
else
{
throw new Exception("Unable to get UserManager!");
}
}
app.MapControllers();
await app.RunAsync();
}
catch (Exception ex)
{
Console.WriteLine("An error occurred: {0}", ex.Message);
}
finally
{
Console.WriteLine("Application shutting down...");
}
}
}