-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
112 lines (93 loc) · 3.18 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
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.SqlServer;
using VisentiaTwin_API.DataModels;
using VisentiaTwin_API.DataContexts;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.IgnoreCycles;
});
builder.Services.AddProblemDetails();
Action<DbContextOptionsBuilder> option;
if (builder.Environment.IsDevelopment())
{
Console.WriteLine("DBContext: Using VisentiaTwinLocalDb");
option = new Action<DbContextOptionsBuilder>(opt => opt.UseSqlServer(
builder.Configuration.GetConnectionString("VisentiaTwinLocalDb"),
sqlOptions =>
{
sqlOptions.EnableRetryOnFailure(
maxRetryCount: 5,
maxRetryDelay: TimeSpan.FromSeconds(30),
errorNumbersToAdd: null);
}));
}
else
{
Console.WriteLine("DBContext: Using VisentiaTwinAzureDb");
option = new Action<DbContextOptionsBuilder>(opt => opt.UseSqlServer(
builder.Configuration.GetConnectionString("VisentiaTwinAzureDb"),
sqlOptions =>
{
sqlOptions.EnableRetryOnFailure(
maxRetryCount: 5,
maxRetryDelay: TimeSpan.FromSeconds(30),
errorNumbersToAdd: null);
}));
}
builder.Services
.AddDbContext<VTSystemContext>(option)
.AddDbContext<YBUserContext>(option)
.AddDbContext<YBCarContext>(option)
.AddDbContext<YBRentContext>(option)
.AddCors(options =>
{
options.AddPolicy("AllowSpecificOrigin", builder =>
{
//builder.WithOrigins("https://your-allowed-domain.com")
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
});
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
app.UseExceptionHandler();
app.UseStatusCodePages();
// Configure the HTTP request pipeline.
app.UseSwagger();
app.UseSwaggerUI();
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseMigrationsEndPoint();
}
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
var systemContext = services.GetRequiredService<VTSystemContext>();
systemContext.Database.EnsureCreated();
DbInitializer.InitializeSystem(systemContext);
var userContext = services.GetRequiredService<YBUserContext>();
userContext.Database.EnsureCreated();
DbInitializer.InitializeUsers(userContext);
var carContext = services.GetRequiredService<YBCarContext>();
carContext.Database.EnsureCreated();
DbInitializer.InitializeCars(carContext);
var orderContext = services.GetRequiredService<YBRentContext>();
orderContext.Database.EnsureCreated();
DbInitializer.InitializeOrders(orderContext);
}
//app.UseHttpsRedirection();
app.UseAuthorization();
app.UseCors("AllowSpecificOrigin");
app.MapControllers();
app.Run();