-
Notifications
You must be signed in to change notification settings - Fork 9
/
Startup.cs
231 lines (198 loc) · 9.22 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
using ExpressBase.Common;
using ExpressBase.Common.ServiceClients;
using ExpressBase.Web.Filters;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Cors.Internal;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Http.Features;
using ServiceStack;
using ServiceStack.Redis;
using System;
using System.Reflection;
using System.Collections.Generic;
namespace ExpressBase.Web2
{
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables();
if (env.IsDevelopment())
{
builder.AddApplicationInsightsSettings(developerMode: true);
}
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddApplicationInsightsTelemetry(Configuration);
services.AddDataProtection(opts =>
{
opts.ApplicationDiscriminator = "expressbase.web";
}); // for antiforgery checking
services.AddCors(options =>
{
options.AddPolicy("AllowSpecificOrigin",
builder => builder
.SetIsOriginAllowedToAllowWildcardSubdomains()
.WithOrigins("https://*.expressbase.com", "https://*." + RoutingConstants.STAGEHOST)
.AllowAnyMethod());
});
services.AddMvc();
services.Configure<MvcOptions>(options =>
{
options.Filters.Add(new CorsAuthorizationFilterFactory("AllowSpecificOrigin"));
});
services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders =
ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
});
services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardLimit = 5;
options.ForwardedForHeaderName = "Eb-X-Forwarded-For";
});
services.Configure<FormOptions>(options =>
{
options.ValueCountLimit = 512; // 512 items max
options.ValueLengthLimit = 1024 * 1024 * 100; // 100MB max len form data
});
services.AddSingleton<AreaRouter>();
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
// Added - uses IOptions<T> for your settings.
services.AddOptions();
services.AddScoped<IServiceClient, JsonServiceClient>(serviceProvider =>
{
JsonServiceClient client = new JsonServiceClient
{
BaseUri = Environment.GetEnvironmentVariable(EnvironmentConstants.EB_SERVICESTACK_EXT_URL),
RefreshTokenUri = Environment.GetEnvironmentVariable(EnvironmentConstants.EB_GET_ACCESS_TOKEN_URL)
};
client.Timeout = new TimeSpan(0, 3, 0);
return client;
});
services.AddScoped<IEbMqClient, EbMqClient>(serviceProvider =>
{
return new EbMqClient();
});
services.AddScoped<IEbAuthClient, EbAuthClient>(serviceProvider =>
{
return new EbAuthClient();
});
services.AddScoped<IEbStaticFileClient, EbStaticFileClient>(serviceProvider =>
{
return new EbStaticFileClient();
});
services.AddScoped<IEbServerEventClient, EbServerEventClient>(serviceProvider =>
{
return new EbServerEventClient();
});
// StripeConfiguration.SetApiKey("sk_test_eOhkZcaSagCU9Hh33lcS6wQs");
string env = Environment.GetEnvironmentVariable(EnvironmentConstants.ASPNETCORE_ENVIRONMENT);
var redisServer = Environment.GetEnvironmentVariable(EnvironmentConstants.EB_REDIS_SERVER);
var redisPassword = Environment.GetEnvironmentVariable(EnvironmentConstants.EB_REDIS_PASSWORD);
var redisPort = Environment.GetEnvironmentVariable(EnvironmentConstants.EB_REDIS_PORT);
//if (env == "Development")
//{
// services.AddScoped<IRedisClient, RedisClient>(serviceProvider =>
// {
// return new RedisClient(redisServer, Convert.ToInt32(redisPort), redisPassword);
// });
//}
//else
//{
var redisConnectionString = string.Format("redis://{0}@{1}:{2}", redisPassword, redisServer, redisPort);
var redisManager = new RedisManagerPool(redisConnectionString);
services.AddScoped<IRedisClient, IRedisClient>(serviceProvider =>
{
return redisManager.GetClient();
});
var listRWRedis = new List<string>() { redisConnectionString }; var listRORedis = new List<string>() { redisConnectionString.Replace("-master", "-replicas") };
PooledRedisClientManager pooledRedisManager = new PooledRedisClientManager(listRWRedis, listRORedis);
services.AddSingleton<PooledRedisClientManager, PooledRedisClientManager>(serviceProvider =>
{
return pooledRedisManager;
});
//}
////Setting Assembly version in Redis
//AssemblyName assembly = Assembly.GetExecutingAssembly().GetName();
//String version = assembly.Name.ToString() + " - " + assembly.Version.ToString();
//client.Set("WebAssembly", version);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, AreaRouter areaRouter)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
//app.UseForwardedHeaders(new ForwardedHeadersOptions
//{
// ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto | ForwardedHeaders.XForwardedHost
//});
app.UseForwardedHeaders();
app.UseApplicationInsightsRequestTelemetry();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
//app.UseHsts();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseApplicationInsightsExceptionTelemetry();
app.UseStatusCodePagesWithReExecute("/StatusCode/{0}");
app.UseStaticFiles(new StaticFileOptions()
{
OnPrepareResponse = (context) =>
{
var headers = context.Context.Response.GetTypedHeaders();
headers.CacheControl = new Microsoft.Net.Http.Headers.CacheControlHeaderValue
{
Private = true,
MaxAge = TimeSpan.FromDays(15)
};
}
});
app.UseMvc(routes =>
{
routes.DefaultHandler = areaRouter;
//routes.MapRoute(
// name: "developer",
// template: "dev",
// defaults: new { controller = RoutingConstants.EXTCONTROLLER, action = "DevSignIn" }
//);
routes.MapRoute(
name: "default",
template: "{controller=Ext}/{action=Index}");
});
app.UseCors("AllowSpecificOrigin");
app.Use(async (context, next) =>
{
context.Response.Headers.Remove("X-Frame-Options");
if (env.IsStaging())
{
context.Response.Headers.Add("X-Frame-Options", $"ALLOW-FROM SAMEDOMAIN *.{RoutingConstants.STAGEHOST}");
context.Response.Headers.Add("Content-Security-Policy", $"frame-ancestors 'self' {RoutingConstants.STAGEHOST} *.{RoutingConstants.STAGEHOST};");
}
if (env.IsProduction())
context.Response.Headers.Add("X-Frame-Options", "ALLOW-FROM SAMEDOMAIN");
await next();
}); // for web forwarding with masking
}
}
}