-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
79 lines (62 loc) · 2.27 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
using web_service.Models;
using web_service.Services;
using Microsoft.Extensions.Options;
var builder = WebApplication.CreateBuilder(args);
// Load the ENV package before creating the builder
if (File.Exists(".env"))
{
DotNetEnv.Env.Load();
}
// Add services to the container.
// Configure MongoDB settings from appsettings.json or .env
builder.Services.Configure<MongoDBSettings>(
builder.Configuration.GetSection("MongoDB"));
// Register the Service as a singleton
builder.Services.AddSingleton<OrderService>();
builder.Services.AddSingleton<PaymentService>();
builder.Services.AddSingleton<ProductService>();
builder.Services.AddSingleton<UserService>();
builder.Services.AddSingleton<CartService>();
builder.Services.AddSingleton<NotificationService>();
builder.Services.AddSingleton<CategoryService>();
builder.Services.AddSingleton<RatingService>();
// Add controllers and configure Newtonsoft.Json
builder.Services.AddControllers()
.AddNewtonsoftJson(options =>
{
options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
});
// Optional: Register Swagger services for API documentation
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// Optional: Configure CORS if accessing from a different domain (e.g., React frontend)
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowReactApp",
policy => policy.WithOrigins("http://localhost:3000")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
});
// For all requests
// builder.Services.AddCors(options =>
// {
// options.AddPolicy("AllowReactApp",
// policy => policy.WithOrigins("*") // Allow all origins
// .AllowAnyMethod()
// .AllowAnyHeader());
// });
var app = builder.Build();
// Configure the HTTP request pipeline.
// Use Swagger middleware if in development environment
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
// Optional: Use CORS policy
app.UseCors("AllowReactApp");
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();