mirror of https://github.com/abpframework/eventhub
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
236 lines
9.0 KiB
236 lines
9.0 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
using EventHub.Admin.Events;
|
|
using EventHub.Admin.Organizations;
|
|
using EventHub.Admin.Utils;
|
|
using EventHub.EntityFrameworkCore;
|
|
using EventHub.Web;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Cors;
|
|
using Microsoft.AspNetCore.DataProtection;
|
|
using Microsoft.AspNetCore.Localization;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.OpenApi.Models;
|
|
using StackExchange.Redis;
|
|
using Volo.Abp;
|
|
using Volo.Abp.AspNetCore.Mvc;
|
|
using Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic;
|
|
using Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared;
|
|
using Volo.Abp.AspNetCore.Serilog;
|
|
using Volo.Abp.Autofac;
|
|
using Volo.Abp.BackgroundJobs;
|
|
using Volo.Abp.Caching;
|
|
using Volo.Abp.Caching.StackExchangeRedis;
|
|
using Volo.Abp.Localization;
|
|
using Volo.Abp.Modularity;
|
|
using Volo.Abp.Swashbuckle;
|
|
using Volo.Abp.VirtualFileSystem;
|
|
|
|
namespace EventHub.Admin
|
|
{
|
|
[DependsOn(
|
|
typeof(EventHubAdminApplicationModule),
|
|
typeof(EventHubAdminHttpApiModule),
|
|
typeof(EventHubEntityFrameworkCoreModule),
|
|
typeof(AbpAutofacModule),
|
|
typeof(AbpCachingStackExchangeRedisModule),
|
|
typeof(AbpAspNetCoreSerilogModule),
|
|
typeof(AbpSwashbuckleModule),
|
|
typeof(AbpAspNetCoreMvcUiBasicThemeModule)
|
|
)]
|
|
public class EventHubAdminHttpApiHostModule : AbpModule
|
|
{
|
|
private const string DefaultCorsPolicyName = "Default";
|
|
|
|
public override void ConfigureServices(ServiceConfigurationContext context)
|
|
{
|
|
var configuration = context.Services.GetConfiguration();
|
|
var hostingEnvironment = context.Services.GetHostingEnvironment();
|
|
|
|
ConfigureAuthentication(context, configuration);
|
|
ConfigureLocalization();
|
|
ConfigureCache(configuration);
|
|
ConfigureVirtualFileSystem(context);
|
|
ConfigureRedis(context, configuration);
|
|
ConfigureCors(context, configuration);
|
|
ConfigureCookies(context);
|
|
ConfigureSwaggerServices(context, configuration);
|
|
ConfigureBackgroundJobs();
|
|
ConfigureAutoApiControllers();
|
|
}
|
|
|
|
private void ConfigureAutoApiControllers()
|
|
{
|
|
Configure<AbpAspNetCoreMvcOptions>(options =>
|
|
{
|
|
options.ConventionalControllers.FormBodyBindingIgnoredTypes.Add(typeof(UpdateOrganizationDto));
|
|
options.ConventionalControllers.FormBodyBindingIgnoredTypes.Add(typeof(UpdateEventDto));
|
|
});
|
|
}
|
|
|
|
private void ConfigureBackgroundJobs()
|
|
{
|
|
Configure<AbpBackgroundJobOptions>(options =>
|
|
{
|
|
options.IsJobExecutionEnabled = false;
|
|
});
|
|
}
|
|
|
|
private void ConfigureCache(IConfiguration configuration)
|
|
{
|
|
Configure<AbpDistributedCacheOptions>(options => { options.KeyPrefix = "EventHub:"; });
|
|
}
|
|
|
|
private void ConfigureVirtualFileSystem(ServiceConfigurationContext context)
|
|
{
|
|
var hostingEnvironment = context.Services.GetHostingEnvironment();
|
|
|
|
Configure<AbpVirtualFileSystemOptions>(options =>
|
|
{
|
|
if (hostingEnvironment.IsDevelopment())
|
|
{
|
|
options.FileSets.ReplaceEmbeddedByPhysical<EventHubDomainSharedModule>(
|
|
Path.Combine(hostingEnvironment.ContentRootPath,
|
|
$"..{Path.DirectorySeparatorChar}EventHub.Domain.Shared"));
|
|
options.FileSets.ReplaceEmbeddedByPhysical<EventHubDomainModule>(
|
|
Path.Combine(hostingEnvironment.ContentRootPath,
|
|
$"..{Path.DirectorySeparatorChar}EventHub.Domain"));
|
|
options.FileSets.ReplaceEmbeddedByPhysical<EventHubAdminApplicationContractsModule>(
|
|
Path.Combine(hostingEnvironment.ContentRootPath,
|
|
$"..{Path.DirectorySeparatorChar}EventHub.Admin.Application.Contracts"));
|
|
options.FileSets.ReplaceEmbeddedByPhysical<EventHubAdminApplicationModule>(
|
|
Path.Combine(hostingEnvironment.ContentRootPath,
|
|
$"..{Path.DirectorySeparatorChar}EventHub.Admin.Application"));
|
|
}
|
|
});
|
|
}
|
|
|
|
private void ConfigureAuthentication(ServiceConfigurationContext context, IConfiguration configuration)
|
|
{
|
|
context.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|
.AddJwtBearer(options =>
|
|
{
|
|
options.Authority = configuration["AuthServer:Authority"];
|
|
options.RequireHttpsMetadata = Convert.ToBoolean(configuration["AuthServer:RequireHttpsMetadata"]);
|
|
options.Audience = "EventHubAdmin";
|
|
});
|
|
}
|
|
|
|
private static void ConfigureSwaggerServices(ServiceConfigurationContext context, IConfiguration configuration)
|
|
{
|
|
context.Services.AddAbpSwaggerGenWithOAuth(
|
|
EventHubUrlOptions.GetAccountConfigValue(configuration),
|
|
new Dictionary<string, string>
|
|
{
|
|
{"EventHubAdmin", "EventHub Admin API"}
|
|
},
|
|
options =>
|
|
{
|
|
options.SwaggerDoc("v1", new OpenApiInfo {Title = "EventHub Admin API", Version = "v1"});
|
|
options.DocInclusionPredicate((docName, description) => true);
|
|
options.CustomSchemaIds(type => type.FullName);
|
|
});
|
|
}
|
|
|
|
private void ConfigureLocalization()
|
|
{
|
|
Configure<AbpLocalizationOptions>(options =>
|
|
{
|
|
options.Languages.Add(new LanguageInfo("en", "en", "English", "gb"));
|
|
});
|
|
}
|
|
|
|
private void ConfigureRedis(
|
|
ServiceConfigurationContext context,
|
|
IConfiguration configuration)
|
|
{
|
|
var redis = ConnectionMultiplexer.Connect(configuration["Redis:Configuration"]);
|
|
context.Services
|
|
.AddDataProtection()
|
|
.PersistKeysToStackExchangeRedis(redis, "EventHub-Protection-Keys");
|
|
}
|
|
|
|
private void ConfigureCors(ServiceConfigurationContext context, IConfiguration configuration)
|
|
{
|
|
context.Services.AddCors(options =>
|
|
{
|
|
options.AddPolicy(DefaultCorsPolicyName, builder =>
|
|
{
|
|
builder
|
|
.WithOrigins(
|
|
EventHubUrlOptions.GetAdminConfigValue(configuration)
|
|
)
|
|
.WithAbpExposedHeaders()
|
|
.SetIsOriginAllowedToAllowWildcardSubdomains()
|
|
.AllowAnyHeader()
|
|
.AllowAnyMethod()
|
|
.AllowCredentials();
|
|
});
|
|
});
|
|
}
|
|
|
|
private void ConfigureCookies(ServiceConfigurationContext context)
|
|
{
|
|
context.Services.AddSameSiteCookiePolicy();
|
|
}
|
|
|
|
public override void OnApplicationInitialization(ApplicationInitializationContext context)
|
|
{
|
|
var app = context.GetApplicationBuilder();
|
|
var env = context.GetEnvironment();
|
|
|
|
if (env.IsDevelopment())
|
|
{
|
|
app.UseDeveloperExceptionPage();
|
|
}
|
|
|
|
var supportedCultures = new[]
|
|
{
|
|
new CultureInfo("en")
|
|
};
|
|
|
|
app.UseAbpRequestLocalization(options =>
|
|
{
|
|
options.DefaultRequestCulture = new RequestCulture("en");
|
|
options.SupportedCultures = supportedCultures;
|
|
options.SupportedUICultures = supportedCultures;
|
|
options.RequestCultureProviders = new List<IRequestCultureProvider>
|
|
{
|
|
new QueryStringRequestCultureProvider(),
|
|
new CookieRequestCultureProvider()
|
|
};
|
|
});
|
|
|
|
if (!env.IsDevelopment())
|
|
{
|
|
app.UseErrorPage();
|
|
}
|
|
|
|
app.UseCookiePolicy();
|
|
app.UseCorrelationId();
|
|
app.UseStaticFiles();
|
|
app.UseRouting();
|
|
app.UseCors(DefaultCorsPolicyName);
|
|
app.UseAuthentication();
|
|
app.UseAuthorization();
|
|
app.UseSwagger();
|
|
app.UseAbpSwaggerUI(options =>
|
|
{
|
|
options.SwaggerEndpoint("/swagger/v1/swagger.json", "EventHub Admin API");
|
|
|
|
var configuration = context.GetConfiguration();
|
|
options.OAuthClientId(configuration["AuthServer:SwaggerClientId"]);
|
|
options.OAuthClientSecret(configuration["AuthServer:SwaggerClientSecret"]);
|
|
});
|
|
app.UseAuditing();
|
|
app.UseAbpSerilogEnrichers();
|
|
app.UseUnitOfWork();
|
|
app.UseConfiguredEndpoints();
|
|
}
|
|
}
|
|
}
|
|
|