126 lines
4.9 KiB
C#
126 lines
4.9 KiB
C#
namespace Microsoft.eShopOnContainers.Services.Marketing.API
|
|
{
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
using System.Reflection;
|
|
using System;
|
|
|
|
public class Startup
|
|
{
|
|
public Startup(IHostingEnvironment env)
|
|
{
|
|
var builder = new ConfigurationBuilder()
|
|
.SetBasePath(env.ContentRootPath)
|
|
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
|
|
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
|
|
.AddEnvironmentVariables();
|
|
|
|
if (env.IsDevelopment())
|
|
{
|
|
builder.AddUserSecrets(typeof(Startup).GetTypeInfo().Assembly);
|
|
}
|
|
|
|
builder.AddEnvironmentVariables();
|
|
|
|
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)
|
|
{
|
|
//services.AddHealthChecks(checks =>
|
|
//{
|
|
// var minutes = 1;
|
|
// if (int.TryParse(Configuration["HealthCheck:Timeout"], out var minutesParsed))
|
|
// {
|
|
// minutes = minutesParsed;
|
|
// }
|
|
// checks.AddSqlCheck("MarketingDb", Configuration["ConnectionString"], TimeSpan.FromMinutes(minutes));
|
|
//});
|
|
|
|
// Add framework services.
|
|
services.AddMvc();
|
|
|
|
services.AddDbContext<MarketingContext>(options =>
|
|
{
|
|
options.UseSqlServer(Configuration["ConnectionString"],
|
|
sqlServerOptionsAction: sqlOptions =>
|
|
{
|
|
sqlOptions.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name);
|
|
//Configuring Connection Resiliency: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency
|
|
sqlOptions.EnableRetryOnFailure(maxRetryCount: 5, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null);
|
|
});
|
|
|
|
// Changing default behavior when client evaluation occurs to throw.
|
|
// Default in EF Core would be to log a warning when client evaluation is performed.
|
|
options.ConfigureWarnings(warnings => warnings.Throw(RelationalEventId.QueryClientEvaluationWarning));
|
|
//Check Client vs. Server evaluation: https://docs.microsoft.com/en-us/ef/core/querying/client-eval
|
|
});
|
|
|
|
// Add framework services.
|
|
services.AddSwaggerGen(options =>
|
|
{
|
|
options.DescribeAllEnumsAsStrings();
|
|
options.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info
|
|
{
|
|
Title = "Marketing HTTP API",
|
|
Version = "v1",
|
|
Description = "The Marketing Service HTTP API",
|
|
TermsOfService = "Terms Of Service"
|
|
});
|
|
});
|
|
|
|
services.AddCors(options =>
|
|
{
|
|
options.AddPolicy("CorsPolicy",
|
|
builder => builder.AllowAnyOrigin()
|
|
.AllowAnyMethod()
|
|
.AllowAnyHeader()
|
|
.AllowCredentials());
|
|
});
|
|
}
|
|
|
|
// 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)
|
|
{
|
|
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
|
|
loggerFactory.AddDebug();
|
|
|
|
app.UseCors("CorsPolicy");
|
|
|
|
ConfigureAuth(app);
|
|
|
|
app.UseMvcWithDefaultRoute();
|
|
|
|
app.UseSwagger()
|
|
.UseSwaggerUI(c =>
|
|
{
|
|
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
|
|
});
|
|
|
|
//MarketingContextSeed.SeedAsync(app, loggerFactory)
|
|
// .Wait();
|
|
}
|
|
|
|
|
|
protected virtual void ConfigureAuth(IApplicationBuilder app)
|
|
{
|
|
var identityUrl = Configuration.GetValue<string>("IdentityUrl");
|
|
app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
|
|
{
|
|
Authority = identityUrl.ToString(),
|
|
ApiName = "marketing",
|
|
RequireHttpsMetadata = false
|
|
});
|
|
}
|
|
}
|
|
}
|