2016-11-17 10:34:10 +01:00
|
|
|
|
using System;
|
2017-04-13 13:06:22 -03:00
|
|
|
|
using System.IO;
|
2016-11-17 10:34:10 +01:00
|
|
|
|
using Microsoft.AspNetCore.Antiforgery;
|
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
2017-04-13 13:06:22 -03:00
|
|
|
|
using Microsoft.Extensions.HealthChecks;
|
2016-11-17 10:34:10 +01:00
|
|
|
|
using Newtonsoft.Json.Serialization;
|
2017-01-10 11:37:36 +01:00
|
|
|
|
using eShopOnContainers.WebSPA;
|
2017-06-08 17:45:07 +02:00
|
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks;
|
2017-06-20 12:54:32 -07:00
|
|
|
|
using WebSPA.Infrastructure;
|
2016-11-17 10:34:10 +01:00
|
|
|
|
|
|
|
|
|
namespace eShopConContainers.WebSPA
|
|
|
|
|
{
|
|
|
|
|
public class Startup
|
|
|
|
|
{
|
|
|
|
|
private IHostingEnvironment _hostingEnv;
|
|
|
|
|
public Startup(IHostingEnvironment env)
|
|
|
|
|
{
|
|
|
|
|
_hostingEnv = env;
|
|
|
|
|
|
|
|
|
|
var builder = new ConfigurationBuilder()
|
|
|
|
|
.SetBasePath(env.ContentRootPath)
|
|
|
|
|
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
|
|
|
|
|
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
|
|
|
|
|
.AddEnvironmentVariables();
|
|
|
|
|
|
|
|
|
|
if (env.IsDevelopment())
|
|
|
|
|
{
|
|
|
|
|
// For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709
|
|
|
|
|
builder.AddUserSecrets();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Configuration = builder.Build();
|
2017-03-17 15:38:18 -07:00
|
|
|
|
|
|
|
|
|
var localPath = new Uri(Configuration["ASPNETCORE_URLS"])?.LocalPath ?? "/";
|
|
|
|
|
Configuration["BaseUrl"] = localPath;
|
2016-11-17 10:34:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-10 11:37:36 +01:00
|
|
|
|
public static IConfigurationRoot Configuration { get; set;}
|
2016-11-17 10:34:10 +01:00
|
|
|
|
// 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 http://go.microsoft.com/fwlink/?LinkID=398940
|
|
|
|
|
public void ConfigureServices(IServiceCollection services)
|
|
|
|
|
{
|
2017-03-28 16:16:01 +02:00
|
|
|
|
services.AddHealthChecks(checks =>
|
|
|
|
|
{
|
2017-05-09 13:54:45 +02:00
|
|
|
|
var minutes = 1;
|
|
|
|
|
if (int.TryParse(Configuration["HealthCheck:Timeout"], out var minutesParsed))
|
|
|
|
|
{
|
|
|
|
|
minutes = minutesParsed;
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-10 15:01:02 +02:00
|
|
|
|
checks.AddUrlCheck(Configuration["CatalogUrlHC"], TimeSpan.FromMinutes(minutes));
|
|
|
|
|
checks.AddUrlCheck(Configuration["OrderingUrlHC"], TimeSpan.FromMinutes(minutes));
|
|
|
|
|
checks.AddUrlCheck(Configuration["BasketUrlHC"], TimeSpan.FromMinutes(minutes));
|
|
|
|
|
checks.AddUrlCheck(Configuration["IdentityUrlHC"], TimeSpan.FromMinutes(minutes));
|
2017-03-28 16:16:01 +02:00
|
|
|
|
});
|
|
|
|
|
|
2017-01-10 11:37:36 +01:00
|
|
|
|
services.Configure<AppSettings>(Configuration);
|
|
|
|
|
|
2017-06-08 17:45:07 +02:00
|
|
|
|
if (Configuration.GetValue<string>("IsClusterEnv") == bool.TrueString)
|
2017-03-15 08:57:01 -07:00
|
|
|
|
{
|
2017-06-08 17:45:07 +02:00
|
|
|
|
services.AddDataProtection(opts =>
|
|
|
|
|
{
|
|
|
|
|
opts.ApplicationDiscriminator = "eshop.webspa";
|
|
|
|
|
})
|
|
|
|
|
.PersistKeysToRedis(Configuration["DPConnectionString"]);
|
|
|
|
|
}
|
2017-03-15 08:57:01 -07:00
|
|
|
|
|
2016-11-17 10:34:10 +01:00
|
|
|
|
services.AddAntiforgery(options => options.HeaderName = "X-XSRF-TOKEN");
|
|
|
|
|
|
|
|
|
|
services.AddMvc()
|
|
|
|
|
.AddJsonOptions(options =>
|
|
|
|
|
{
|
|
|
|
|
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 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, IAntiforgery antiforgery)
|
|
|
|
|
{
|
|
|
|
|
if (env.IsDevelopment())
|
|
|
|
|
{
|
|
|
|
|
app.UseDeveloperExceptionPage();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Configure XSRF middleware, This pattern is for SPA style applications where XSRF token is added on Index page
|
|
|
|
|
// load and passed back token on every subsequent async request
|
2017-04-13 13:06:22 -03:00
|
|
|
|
// app.Use(async (context, next) =>
|
|
|
|
|
// {
|
|
|
|
|
// if (string.Equals(context.Request.Path.Value, "/", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
// {
|
|
|
|
|
// var tokens = antiforgery.GetAndStoreTokens(context);
|
|
|
|
|
// context.Response.Cookies.Append("XSRF-TOKEN", tokens.RequestToken, new CookieOptions() { HttpOnly = false });
|
|
|
|
|
// }
|
|
|
|
|
// await next.Invoke();
|
|
|
|
|
// });
|
|
|
|
|
|
2017-06-20 12:54:32 -07:00
|
|
|
|
//Seed Data
|
|
|
|
|
WebContextSeed.Seed(app, env, loggerFactory);
|
|
|
|
|
|
2016-11-17 10:34:10 +01:00
|
|
|
|
app.Use(async (context, next) =>
|
|
|
|
|
{
|
2017-04-13 13:06:22 -03:00
|
|
|
|
await next();
|
|
|
|
|
|
|
|
|
|
// If there's no available file and the request doesn't contain an extension, we're probably trying to access a page.
|
|
|
|
|
// Rewrite request to use app root
|
|
|
|
|
if (context.Response.StatusCode == 404 && !Path.HasExtension(context.Request.Path.Value) && !context.Request.Path.Value.StartsWith("/api"))
|
2016-11-17 10:34:10 +01:00
|
|
|
|
{
|
2017-04-13 13:06:22 -03:00
|
|
|
|
context.Request.Path = "/index.html";
|
|
|
|
|
context.Response.StatusCode = 200; // Make sure we update the status code, otherwise it returns 404
|
|
|
|
|
await next();
|
2016-11-17 10:34:10 +01:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2017-04-13 13:06:22 -03:00
|
|
|
|
app.UseDefaultFiles();
|
2016-11-17 10:34:10 +01:00
|
|
|
|
app.UseStaticFiles();
|
|
|
|
|
|
2017-04-17 09:11:43 -03:00
|
|
|
|
app.UseMvcWithDefaultRoute();
|
2016-11-17 10:34:10 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|