-
Notifications
You must be signed in to change notification settings - Fork 354
/
Copy pathStartup.cs
160 lines (134 loc) · 6.64 KB
/
Startup.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
151
152
153
154
155
156
157
158
159
160
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
using OWSData.Repositories.Interfaces;
using OWSShared.Implementations;
using OWSShared.Interfaces;
using OWSShared.Middleware;
using SimpleInjector;
namespace OWSCharacterPersistence
{
public class Startup
{
//Container container;
private Container container = new SimpleInjector.Container();
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
container.Options.ResolveUnregisteredConcreteTypes = false;
Configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddEnvironmentVariables()
.Build();
}
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddDataProtection().PersistKeysToFileSystem(new DirectoryInfo("./temp/DataProtection-Keys"));
services.AddHttpContextAccessor();
services.AddMvcCore(config =>
{
config.EnableEndpointRouting = false;
//IHttpRequestStreamReaderFactory readerFactory = services.BuildServiceProvider().GetRequiredService<IHttpRequestStreamReaderFactory>();
//config.ModelBinderProviders.Insert(0, new Microsoft.AspNetCore.Mvc.ModelBinding.Binders.BodyModelBinderProvider(config.InputFormatters, readerFactory));
//config.ModelBinderProviders.Insert(0, new QueryModelBinderProvider(container));
})
.AddViews()
.AddApiExplorer();
services.AddSimpleInjector(container, options => {
options.AddAspNetCore()
.AddControllerActivation()
.AddViewComponentActivation();
//.AddPageModelActivation()
//.AddTagHelperActivation();
});
services.AddSwaggerGen(c => {
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Open World Server Character Persistence API", Version = "v1" });
c.AddSecurityDefinition("X-CustomerGUID", new OpenApiSecurityScheme
{
Description = "Authorization header using the X-CustomerGUID scheme",
Name = "X-CustomerGUID",
In = ParameterLocation.Header,
Type = SecuritySchemeType.ApiKey,
Scheme = "X-CustomerGUID"
});
c.OperationFilter<SwaggerSecurityRequirementsDocumentFilter>();
var filePath = Path.Combine(System.AppContext.BaseDirectory, "OWSCharacterPersistence.xml");
c.IncludeXmlComments(filePath);
});
services.Configure<OWSShared.Options.StorageOptions>(Configuration.GetSection(OWSShared.Options.StorageOptions.SectionName));
services.Configure<OWSShared.Options.APIPathOptions>(Configuration.GetSection(OWSShared.Options.APIPathOptions.SectionName));
services.Configure<OWSShared.Options.RabbitMQOptions>(Configuration.GetSection(OWSShared.Options.RabbitMQOptions.SectionName));
InitializeContainer(services);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseSimpleInjector(container);
app.UseMiddleware<StoreCustomerGUIDMiddleware>(container);
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
//app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
//app.UseAuthorization();
app.UseMvc();
app.UseSwagger(/*c =>
{
c.RouteTemplate =
"api-docs/{documentName}/swagger.json";
}*/);
app.UseSwaggerUI(c =>
{
//c.RoutePrefix = "api-docs";
c.SwaggerEndpoint("./v1/swagger.json", "Open World Server Character Persistence API");
});
container.Verify();
}
private void InitializeContainer(IServiceCollection services)
{
var OWSStorageConfig = Configuration.GetSection("OWSStorageConfig");
if (OWSStorageConfig.Exists())
{
string dbBackend = OWSStorageConfig.GetValue<string>("OWSDBBackend");
switch (dbBackend)
{
case "postgres":
container.Register<ICharactersRepository, OWSData.Repositories.Implementations.Postgres.CharactersRepository>(Lifestyle.Scoped);
container.Register<IUsersRepository, OWSData.Repositories.Implementations.Postgres.UsersRepository>(Lifestyle.Scoped);
break;
case "mysql":
container.Register<ICharactersRepository, OWSData.Repositories.Implementations.MySQL.CharactersRepository>(Lifestyle.Scoped);
container.Register<IUsersRepository, OWSData.Repositories.Implementations.MySQL.UsersRepository>(Lifestyle.Scoped);
break;
default: // Default to MSSQL
container.Register<ICharactersRepository, OWSData.Repositories.Implementations.MSSQL.CharactersRepository>(Lifestyle.Scoped);
container.Register<IUsersRepository, OWSData.Repositories.Implementations.MSSQL.UsersRepository>(Lifestyle.Scoped);
break;
}
}
container.Register<IHeaderCustomerGUID, HeaderCustomerGUID>(Lifestyle.Scoped);
var provider = services.BuildServiceProvider();
container.RegisterInstance<IServiceProvider>(provider);
}
}
}