-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
125 lines (111 loc) · 3.74 KB
/
Program.cs
File metadata and controls
125 lines (111 loc) · 3.74 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
using Microsoft.OpenApi.Models;
using MultiplayerARPG.MMO;
using Newtonsoft.Json.Serialization;
var builder = WebApplication.CreateBuilder(args);
// Config Manager
if (!int.TryParse(builder.Configuration["ConfigManager"], out int configManager))
configManager = 0;
switch (configManager)
{
default:
builder.Services.AddSingleton<IConfigManager, DefaultConfigManager>();
break;
}
// User login manager
if (!int.TryParse(builder.Configuration["UserLoginManager"], out int userLoginManager))
userLoginManager = 0;
switch (userLoginManager)
{
default:
DefaultDatabaseUserLoginConfig defaultUserLoginConfig = builder.Configuration.GetValue<DefaultDatabaseUserLoginConfig>("UserLoginManagerConfig");
builder.Services.AddSingleton<IDatabaseUserLogin>(provider => new DefaultDatabaseUserLogin(defaultUserLoginConfig));
break;
}
// Cache manager
if (!int.TryParse(builder.Configuration["CacheManager"], out int cacheManager))
cacheManager = 0;
switch (cacheManager)
{
case 1:
builder.Services.AddSingleton<IDatabaseCache, RedisDatabaseCache>();
break;
default:
builder.Services.AddSingleton<IDatabaseCache, LocalDatabaseCache>();
break;
}
// Database server
if (!int.TryParse(builder.Configuration["DatabaseServer"], out int databaseServer))
databaseServer = 0;
switch (databaseServer)
{
case 2:
builder.Services.AddSingleton<IDatabase, PostgreSQLDatabase>();
break;
case 1:
builder.Services.AddSingleton<IDatabase, SQLiteDatabase>();
break;
default:
builder.Services.AddSingleton<IDatabase, MySQLDatabase>();
break;
}
// Api
builder.Services.AddControllers().AddNewtonsoftJson(options =>
{
// Use the default property (Pascal) casing
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
options.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
});
// App secret
string apiSecretKey = builder.Configuration["ApiSecretKey"];
if (apiSecretKey == null)
apiSecretKey = string.Empty;
builder.Services.AddAuthentication(AppSecretAuthenticationHandler.SCHEME)
.AddScheme<AppSecretAuthenticationSchemeOptions, AppSecretAuthenticationHandler>(AppSecretAuthenticationHandler.SCHEME, o =>
{
o.AppSecret = apiSecretKey;
});
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(o =>
{
o.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
Description = "Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.Http,
Scheme = "bearer", // must be lower-case
BearerFormat = "Unknow"
});
o.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
},
Array.Empty<string>()
}
});
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI(o =>
{
o.SwaggerEndpoint("/swagger/v1/swagger.json", "MMORPG KIT DB SERVICE - API");
o.RoutePrefix = "__dev/api"; // This makes Swagger UI available at /__dev/api/index.html
});
}
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
IDatabase database = app.Services.GetService<IDatabase>();
await database.DoMigration();
app.Run();