-
Notifications
You must be signed in to change notification settings - Fork 84
/
Startup.cs
182 lines (148 loc) · 6.27 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using FakeServer.Authentication;
using FakeServer.Authentication.Basic;
using FakeServer.Authentication.Custom;
using FakeServer.Authentication.Jwt;
using FakeServer.Common;
using FakeServer.GraphQL;
using FakeServer.Jobs;
using FakeServer.Simulate;
using FakeServer.WebSockets;
using JsonFlatFileDataStore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.PlatformAbstractions;
using Swashbuckle.AspNetCore.Swagger;
using Swashbuckle.AspNetCore.SwaggerUI;
using System.IO;
namespace FakeServer
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
var folder = Configuration["staticFolder"];
if (!string.IsNullOrEmpty(folder))
{
services.AddSpaStaticFiles((spa) =>
{
spa.RootPath = folder;
});
// No need to define anything else as this can only be used as a SPA server
return;
}
var jsonFilePath = Path.Combine(Configuration["currentPath"], Configuration["file"]);
services.AddSingleton<IDataStore>(new DataStore(jsonFilePath, reloadBeforeGetCollection: Configuration.GetValue<bool>("Common:EagerDataReload")));
services.AddSingleton<IMessageBus, MessageBus>();
services.AddSingleton<JobsService>();
services.Configure<AuthenticationSettings>(Configuration.GetSection("Authentication"));
services.Configure<ApiSettings>(Configuration.GetSection("Api"));
services.Configure<JobsSettings>(Configuration.GetSection("Jobs"));
services.Configure<SimulateSettings>(Configuration.GetSection("Simulate"));
services.AddCors(options =>
{
options.AddPolicy("AllowAnyPolicy",
builder => builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
});
var useAuthentication = Configuration.GetValue<bool>("Authentication:Enabled");
if (useAuthentication)
{
if (Configuration["Authentication:AuthenticationType"] == "token")
{
var blacklistService = new TokenBlacklistService();
services.AddSingleton(blacklistService);
TokenConfiguration.Configure(services);
}
else
{
BasicAuthenticationConfiguration.Configure(services);
}
}
else
{
AllowAllAuthenticationConfiguration.Configure(services);
}
services.AddMvc();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "Fake JSON API", Version = "v1" });
var basePath = PlatformServices.Default.Application.ApplicationBasePath;
var xmlPath = Path.Combine(basePath, "FakeServer.xml");
c.IncludeXmlComments(xmlPath);
if (useAuthentication)
{
c.OperationFilter<AddAuthorizationHeaderParameterOperationFilter>();
if (Configuration["Authentication:AuthenticationType"] == "token")
c.DocumentFilter<AuthTokenOperation>();
}
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime appLifetime)
{
var folder = Configuration["staticFolder"];
app.UseDefaultFiles();
if (string.IsNullOrEmpty(folder))
{
app.UseStaticFiles();
}
else
{
app.UseSpa(spa =>
{
spa.ApplicationBuilder.UseSpaStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(folder)
});
});
// No need to define anything else as this can only be used as a SPA server
return;
}
app.UseCors("AllowAnyPolicy");
app.UseMiddleware<HttpOptionsMiddleware>();
if (Configuration.GetValue<bool>("Simulate:Delay:Enabled"))
{
app.UseMiddleware<DelayMiddleware>();
}
if (Configuration.GetValue<bool>("Simulate:Error:Enabled"))
{
app.UseMiddleware<ErrorMiddleware>();
}
app.UseWebSockets();
app.UseMiddleware<NotifyWebSocketMiddlerware>();
app.UseMiddleware<WebSocketMiddleware>();
// Authentication must be always used as we have Authorize attributes in use
// When Authentication is turned off, special AllowAll hander is used
app.UseAuthentication();
var useAuthentication = Configuration.GetValue<bool>("Authentication:Enabled");
if (useAuthentication && Configuration["Authentication:AuthenticationType"] == "token")
{
TokenConfiguration.UseTokenProviderMiddleware(app);
}
if (Configuration.GetValue<bool>("Caching:ETag:Enabled"))
{
app.UseMiddleware<ETagMiddleware>();
}
app.UseMiddleware<GraphQLMiddleware>(
app.ApplicationServices.GetRequiredService<IDataStore>(),
app.ApplicationServices.GetRequiredService<IMessageBus>(),
useAuthentication);
app.UseMvc();
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Fake JSON API V1");
c.SupportedSubmitMethods(SubmitMethod.Get, SubmitMethod.Head, SubmitMethod.Post, SubmitMethod.Put, SubmitMethod.Patch, SubmitMethod.Delete);
});
}
}
}