333 lines
14 KiB
C#
333 lines
14 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Data.Common;
|
|||
|
using System.Linq;
|
|||
|
using System.Reflection;
|
|||
|
using System.Threading;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using Autofac;
|
|||
|
using HealthChecks.UI.Client;
|
|||
|
using Microsoft.ApplicationInsights.Extensibility;
|
|||
|
using Microsoft.ApplicationInsights.ServiceFabric;
|
|||
|
using Microsoft.AspNetCore.Builder;
|
|||
|
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
|
|||
|
using Microsoft.AspNetCore.Hosting;
|
|||
|
using Microsoft.AspNetCore.Http;
|
|||
|
using Microsoft.AspNetCore.Mvc;
|
|||
|
using Microsoft.Azure.ServiceBus;
|
|||
|
using Microsoft.EntityFrameworkCore;
|
|||
|
using Microsoft.EntityFrameworkCore.Diagnostics;
|
|||
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus;
|
|||
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
|||
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ;
|
|||
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBusServiceBus;
|
|||
|
using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.Services;
|
|||
|
using Microsoft.Extensions.Configuration;
|
|||
|
using Microsoft.Extensions.DependencyInjection;
|
|||
|
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
|||
|
using Microsoft.Extensions.Logging;
|
|||
|
using RabbitMQ.Client;
|
|||
|
using Swashbuckle.AspNetCore.Swagger;
|
|||
|
using Webhooks.API.Infrastructure;
|
|||
|
using Webhooks.API.Services;
|
|||
|
|
|||
|
namespace Webhooks.API
|
|||
|
{
|
|||
|
public class Startup
|
|||
|
{
|
|||
|
public IConfiguration Configuration { get; }
|
|||
|
|
|||
|
public Startup(IConfiguration configuration)
|
|||
|
{
|
|||
|
Configuration = configuration;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
public void ConfigureServices(IServiceCollection services)
|
|||
|
{
|
|||
|
services
|
|||
|
.AddAppInsight(Configuration)
|
|||
|
.AddCustomMVC(Configuration)
|
|||
|
.AddCustomDbContext(Configuration)
|
|||
|
.AddSwagger(Configuration)
|
|||
|
.AddCustomHealthCheck(Configuration)
|
|||
|
.AddHttpClientServices(Configuration)
|
|||
|
.AddIntegrationServices(Configuration)
|
|||
|
.AddEventBus(Configuration)
|
|||
|
.AddTransient<IIdentityService, IdentityService>()
|
|||
|
.AddTransient<IGrantUrlTesterService, GrantUrlTesterService>();
|
|||
|
}
|
|||
|
|
|||
|
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
|
|||
|
{
|
|||
|
loggerFactory.AddAzureWebAppDiagnostics();
|
|||
|
loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Trace);
|
|||
|
|
|||
|
var pathBase = Configuration["PATH_BASE"];
|
|||
|
|
|||
|
if (!string.IsNullOrEmpty(pathBase))
|
|||
|
{
|
|||
|
loggerFactory.CreateLogger("init").LogDebug($"Using PATH BASE '{pathBase}'");
|
|||
|
app.UsePathBase(pathBase);
|
|||
|
}
|
|||
|
|
|||
|
app.UseHealthChecks("/hc", new HealthCheckOptions()
|
|||
|
{
|
|||
|
Predicate = _ => true,
|
|||
|
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
|
|||
|
});
|
|||
|
|
|||
|
app.UseHealthChecks("/liveness", new HealthCheckOptions
|
|||
|
{
|
|||
|
Predicate = r => r.Name.Contains("self")
|
|||
|
});
|
|||
|
|
|||
|
app.UseCors("CorsPolicy");
|
|||
|
|
|||
|
app.UseMvcWithDefaultRoute();
|
|||
|
|
|||
|
app.UseSwagger()
|
|||
|
.UseSwaggerUI(c =>
|
|||
|
{
|
|||
|
c.SwaggerEndpoint($"{ (!string.IsNullOrEmpty(pathBase) ? pathBase : string.Empty) }/swagger/v1/swagger.json", "Webhooks.API V1");
|
|||
|
c.OAuthClientId("webhooksswaggerui");
|
|||
|
c.OAuthAppName("WebHooks Service Swagger UI");
|
|||
|
});
|
|||
|
|
|||
|
ConfigureEventBus(app);
|
|||
|
}
|
|||
|
|
|||
|
protected virtual void ConfigureEventBus(IApplicationBuilder app)
|
|||
|
{
|
|||
|
var eventBus = app.ApplicationServices.GetRequiredService<IEventBus>();
|
|||
|
// eventBus.Subscribe<OrderStatusChangedToAwaitingValidationIntegrationEvent, OrderStatusChangedToAwaitingValidationIntegrationEventHandler>();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
static class CustomExtensionMethods
|
|||
|
{
|
|||
|
public static IServiceCollection AddAppInsight(this IServiceCollection services, IConfiguration configuration)
|
|||
|
{
|
|||
|
services.AddApplicationInsightsTelemetry(configuration);
|
|||
|
var orchestratorType = configuration.GetValue<string>("OrchestratorType");
|
|||
|
|
|||
|
if (orchestratorType?.ToUpper() == "K8S")
|
|||
|
{
|
|||
|
// Enable K8s telemetry initializer
|
|||
|
services.EnableKubernetes();
|
|||
|
}
|
|||
|
if (orchestratorType?.ToUpper() == "SF")
|
|||
|
{
|
|||
|
// Enable SF telemetry initializer
|
|||
|
services.AddSingleton<ITelemetryInitializer>((serviceProvider) =>
|
|||
|
new FabricTelemetryInitializer());
|
|||
|
}
|
|||
|
|
|||
|
return services;
|
|||
|
}
|
|||
|
|
|||
|
public static IServiceCollection AddCustomMVC(this IServiceCollection services, IConfiguration configuration)
|
|||
|
{
|
|||
|
services.AddMvc(options =>
|
|||
|
{
|
|||
|
options.Filters.Add(typeof(HttpGlobalExceptionFilter));
|
|||
|
})
|
|||
|
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
|
|||
|
.AddControllersAsServices();
|
|||
|
|
|||
|
services.AddCors(options =>
|
|||
|
{
|
|||
|
options.AddPolicy("CorsPolicy",
|
|||
|
builder => builder
|
|||
|
.SetIsOriginAllowed((host) => true)
|
|||
|
.AllowAnyMethod()
|
|||
|
.AllowAnyHeader()
|
|||
|
.AllowCredentials());
|
|||
|
});
|
|||
|
|
|||
|
return services;
|
|||
|
}
|
|||
|
|
|||
|
public static IServiceCollection AddCustomDbContext(this IServiceCollection services, IConfiguration configuration)
|
|||
|
{
|
|||
|
services.AddDbContext<WebhooksContext>(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: 10, 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
|
|||
|
});
|
|||
|
|
|||
|
return services;
|
|||
|
}
|
|||
|
|
|||
|
public static IServiceCollection AddSwagger(this IServiceCollection services, IConfiguration configuration)
|
|||
|
{
|
|||
|
services.AddSwaggerGen(options =>
|
|||
|
{
|
|||
|
options.DescribeAllEnumsAsStrings();
|
|||
|
options.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info
|
|||
|
{
|
|||
|
Title = "eShopOnContainers - Webhooks HTTP API",
|
|||
|
Version = "v1",
|
|||
|
Description = "The Webhooks Microservice HTTP API. This is a simple webhooks CRUD registration entrypoint",
|
|||
|
TermsOfService = "Terms Of Service"
|
|||
|
});
|
|||
|
|
|||
|
options.AddSecurityDefinition("oauth2", new OAuth2Scheme
|
|||
|
{
|
|||
|
Type = "oauth2",
|
|||
|
Flow = "implicit",
|
|||
|
AuthorizationUrl = $"{configuration.GetValue<string>("IdentityUrlExternal")}/connect/authorize",
|
|||
|
TokenUrl = $"{configuration.GetValue<string>("IdentityUrlExternal")}/connect/token",
|
|||
|
Scopes = new Dictionary<string, string>()
|
|||
|
{
|
|||
|
{ "webhooks", "Webhooks.API" }
|
|||
|
}
|
|||
|
});
|
|||
|
|
|||
|
});
|
|||
|
|
|||
|
|
|||
|
|
|||
|
return services;
|
|||
|
}
|
|||
|
public static IServiceCollection AddEventBus(this IServiceCollection services, IConfiguration configuration)
|
|||
|
{
|
|||
|
var subscriptionClientName = configuration["SubscriptionClientName"];
|
|||
|
|
|||
|
if (configuration.GetValue<bool>("AzureServiceBusEnabled"))
|
|||
|
{
|
|||
|
services.AddSingleton<IEventBus, EventBusServiceBus>(sp =>
|
|||
|
{
|
|||
|
var serviceBusPersisterConnection = sp.GetRequiredService<IServiceBusPersisterConnection>();
|
|||
|
var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>();
|
|||
|
var logger = sp.GetRequiredService<ILogger<EventBusServiceBus>>();
|
|||
|
var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>();
|
|||
|
|
|||
|
return new EventBusServiceBus(serviceBusPersisterConnection, logger,
|
|||
|
eventBusSubcriptionsManager, subscriptionClientName, iLifetimeScope);
|
|||
|
});
|
|||
|
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
services.AddSingleton<IEventBus, EventBusRabbitMQ>(sp =>
|
|||
|
{
|
|||
|
var rabbitMQPersistentConnection = sp.GetRequiredService<IRabbitMQPersistentConnection>();
|
|||
|
var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>();
|
|||
|
var logger = sp.GetRequiredService<ILogger<EventBusRabbitMQ>>();
|
|||
|
var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>();
|
|||
|
|
|||
|
var retryCount = 5;
|
|||
|
if (!string.IsNullOrEmpty(configuration["EventBusRetryCount"]))
|
|||
|
{
|
|||
|
retryCount = int.Parse(configuration["EventBusRetryCount"]);
|
|||
|
}
|
|||
|
|
|||
|
return new EventBusRabbitMQ(rabbitMQPersistentConnection, logger, iLifetimeScope, eventBusSubcriptionsManager, subscriptionClientName, retryCount);
|
|||
|
});
|
|||
|
}
|
|||
|
|
|||
|
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();
|
|||
|
|
|||
|
//services.AddTransient<OrderStatusChangedToAwaitingValidationIntegrationEventHandler>();
|
|||
|
|
|||
|
return services;
|
|||
|
}
|
|||
|
|
|||
|
public static IServiceCollection AddCustomHealthCheck(this IServiceCollection services, IConfiguration configuration)
|
|||
|
{
|
|||
|
var accountName = configuration.GetValue<string>("AzureStorageAccountName");
|
|||
|
var accountKey = configuration.GetValue<string>("AzureStorageAccountKey");
|
|||
|
|
|||
|
var hcBuilder = services.AddHealthChecks();
|
|||
|
|
|||
|
hcBuilder
|
|||
|
.AddCheck("self", () => HealthCheckResult.Healthy())
|
|||
|
.AddSqlServer(
|
|||
|
configuration["ConnectionString"],
|
|||
|
name: "WebhooksApiDb-check",
|
|||
|
tags: new string[] { "webhooksdb" });
|
|||
|
|
|||
|
return services;
|
|||
|
}
|
|||
|
|
|||
|
public static IServiceCollection AddHttpClientServices(this IServiceCollection services, IConfiguration configuration)
|
|||
|
{
|
|||
|
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
|
|||
|
|
|||
|
|
|||
|
//register delegating handlers
|
|||
|
//services.AddTransient<HttpClientAuthorizationDelegatingHandler>();
|
|||
|
|
|||
|
//InfinteTimeSpan -> See: https://github.com/aspnet/HttpClientFactory/issues/194
|
|||
|
services.AddHttpClient("extendedhandlerlifetime").SetHandlerLifetime(Timeout.InfiniteTimeSpan);
|
|||
|
|
|||
|
//add http client services
|
|||
|
services.AddHttpClient("GrantClient")
|
|||
|
.SetHandlerLifetime(TimeSpan.FromMinutes(5));
|
|||
|
//.AddHttpMessageHandler<HttpClientAuthorizationDelegatingHandler>();
|
|||
|
|
|||
|
return services;
|
|||
|
}
|
|||
|
|
|||
|
public static IServiceCollection AddIntegrationServices(this IServiceCollection services, IConfiguration configuration)
|
|||
|
{
|
|||
|
services.AddTransient<Func<DbConnection, IIntegrationEventLogService>>(
|
|||
|
sp => (DbConnection c) => new IntegrationEventLogService(c));
|
|||
|
|
|||
|
// services.AddTransient<ICatalogIntegrationEventService, CatalogIntegrationEventService>();
|
|||
|
|
|||
|
if (configuration.GetValue<bool>("AzureServiceBusEnabled"))
|
|||
|
{
|
|||
|
services.AddSingleton<IServiceBusPersisterConnection>(sp =>
|
|||
|
{
|
|||
|
var logger = sp.GetRequiredService<ILogger<DefaultServiceBusPersisterConnection>>();
|
|||
|
var serviceBusConnection = new ServiceBusConnectionStringBuilder(configuration["EventBusConnection"]);
|
|||
|
return new DefaultServiceBusPersisterConnection(serviceBusConnection, logger);
|
|||
|
});
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
services.AddSingleton<IRabbitMQPersistentConnection>(sp =>
|
|||
|
{
|
|||
|
var logger = sp.GetRequiredService<ILogger<DefaultRabbitMQPersistentConnection>>();
|
|||
|
|
|||
|
var factory = new ConnectionFactory()
|
|||
|
{
|
|||
|
HostName = configuration["EventBusConnection"]
|
|||
|
};
|
|||
|
|
|||
|
if (!string.IsNullOrEmpty(configuration["EventBusUserName"]))
|
|||
|
{
|
|||
|
factory.UserName = configuration["EventBusUserName"];
|
|||
|
}
|
|||
|
|
|||
|
if (!string.IsNullOrEmpty(configuration["EventBusPassword"]))
|
|||
|
{
|
|||
|
factory.Password = configuration["EventBusPassword"];
|
|||
|
}
|
|||
|
|
|||
|
var retryCount = 5;
|
|||
|
if (!string.IsNullOrEmpty(configuration["EventBusRetryCount"]))
|
|||
|
{
|
|||
|
retryCount = int.Parse(configuration["EventBusRetryCount"]);
|
|||
|
}
|
|||
|
|
|||
|
return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount);
|
|||
|
});
|
|||
|
}
|
|||
|
|
|||
|
return services;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|