chore 抽象微服务基本类库
This commit is contained in:
@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Swashbuckle.AspNetCore.SwaggerUI;
|
||||
using Volo.Abp.Swashbuckle;
|
||||
|
||||
namespace KonSoft.Shared.Hosting.AspNetCore;
|
||||
|
||||
public static class AbpSwaggerUIBuilderExtensions
|
||||
{
|
||||
public static IApplicationBuilder UseAbpSwaggerWithCustomScriptUI(
|
||||
this IApplicationBuilder app,
|
||||
Action<SwaggerUIOptions>? setupAction = null)
|
||||
{
|
||||
var resolver = app.ApplicationServices.GetService<ISwaggerHtmlResolver>();
|
||||
|
||||
return app.UseSwaggerUI(options =>
|
||||
{
|
||||
options.InjectJavascript("ui/abp.js");
|
||||
options.InjectJavascript("ui/abp.swagger.js");
|
||||
options.InjectJavascript("ui/requestinterceptor.js");
|
||||
options.IndexStream = () => resolver?.Resolver();
|
||||
|
||||
setupAction?.Invoke(options);
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,24 @@
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Serilog;
|
||||
using Volo.Abp.Modularity;
|
||||
|
||||
namespace KonSoft.Shared.Hosting.AspNetCore;
|
||||
|
||||
public static class ApplicationBuilderHelper
|
||||
{
|
||||
public static async Task<WebApplication> BuildApplicationAsync<TStartupModule>(string[] args)
|
||||
where TStartupModule : IAbpModule
|
||||
{
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
builder.Host
|
||||
.AddAppSettingsSecretsJson()
|
||||
.UseAutofac()
|
||||
.UseSerilog();
|
||||
|
||||
await builder.AddApplicationAsync<TStartupModule>();
|
||||
return builder.Build();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,36 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<RootNamespace>KonSoft.Shared.Hosting.AspNetCore</RootNamespace>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\KonSoft.Shared.Hosting\KonSoft.Shared.Hosting.csproj" />
|
||||
<ProjectReference Include="..\KonSoft.Shared.Localization\KonSoft.Shared.Localization.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Serilog.AspNetCore" Version="8.0.1" />
|
||||
<PackageReference Include="Volo.Abp.AspNetCore.Serilog" Version="8.3.4" />
|
||||
<PackageReference Include="Volo.Abp.AspNetCore.MultiTenancy" Version="8.3.4" />
|
||||
<PackageReference Include="Volo.Abp.Swashbuckle" Version="8.3.4" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Remove="wwwroot\swagger\ui\requestinterceptor.js" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="wwwroot\swagger\ui\requestinterceptor.js">
|
||||
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
|
||||
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Include="wwwroot\swagger\ui\requestinterceptor.js" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@ -0,0 +1,11 @@
|
||||
using Volo.Abp.DependencyInjection;
|
||||
using Volo.Abp.Ui.Branding;
|
||||
|
||||
namespace KonSoft.Shared.Hosting.AspNetCore
|
||||
{
|
||||
[Dependency(ReplaceServices = true)]
|
||||
public class KonSoftBrandingProvider : DefaultBrandingProvider
|
||||
{
|
||||
public override string AppName => "KonSoft";
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,8 @@
|
||||
namespace KonSoft.Shared.Hosting.AspNetCore;
|
||||
|
||||
public static class KonSoftConsts
|
||||
{
|
||||
public const string AuthServerAudience = "KonSoft";
|
||||
public const string AnonymousUserClaimName = "anonymous_id";
|
||||
public const bool MultiTenancyEnabled = true;
|
||||
}
|
||||
@ -0,0 +1,32 @@
|
||||
using KonSoft.Shared.Localization;
|
||||
using Volo.Abp.AspNetCore.Serilog;
|
||||
using Volo.Abp.Modularity;
|
||||
using Volo.Abp.MultiTenancy;
|
||||
using Volo.Abp.Swashbuckle;
|
||||
using Volo.Abp.VirtualFileSystem;
|
||||
|
||||
namespace KonSoft.Shared.Hosting.AspNetCore
|
||||
{
|
||||
[DependsOn(
|
||||
typeof(KonSoftSharedLocalizationModule),
|
||||
typeof(KonSoftSharedHostingModule),
|
||||
typeof(AbpAspNetCoreSerilogModule),
|
||||
typeof(AbpSwashbuckleModule),
|
||||
typeof(AbpMultiTenancyModule)
|
||||
)]
|
||||
public class KonSoftSharedHostingAspNetCoreModule : AbpModule
|
||||
{
|
||||
public override void ConfigureServices(ServiceConfigurationContext context)
|
||||
{
|
||||
Configure<AbpVirtualFileSystemOptions>(options =>
|
||||
{
|
||||
options.FileSets.AddEmbedded<KonSoftSharedHostingAspNetCoreModule>("KonSoft.Shared.Hosting.AspNetCore");
|
||||
});
|
||||
|
||||
Configure<AbpMultiTenancyOptions>(options =>
|
||||
{
|
||||
options.IsEnabled = KonSoftConsts.MultiTenancyEnabled;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,24 @@
|
||||
using Serilog;
|
||||
using Serilog.Events;
|
||||
|
||||
namespace KonSoft.Shared.Hosting.AspNetCore;
|
||||
|
||||
public static class SerilogConfigurationHelper
|
||||
{
|
||||
public static void Configure(string applicationName)
|
||||
{
|
||||
Log.Logger = new LoggerConfiguration()
|
||||
#if DEBUG
|
||||
.MinimumLevel.Debug()
|
||||
#else
|
||||
.MinimumLevel.Information()
|
||||
#endif
|
||||
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
|
||||
.MinimumLevel.Override("Microsoft.EntityFrameworkCore", LogEventLevel.Warning)
|
||||
.Enrich.FromLogContext()
|
||||
.Enrich.WithProperty("Application", $"{applicationName}")
|
||||
.WriteTo.Async(c => c.File("Logs/logs.txt"))
|
||||
.WriteTo.Async(c => c.Console())
|
||||
.CreateLogger();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,32 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using Volo.Abp.Modularity;
|
||||
|
||||
namespace KonSoft.Shared.Hosting.AspNetCore;
|
||||
|
||||
public static class SwaggerConfigurationHelper
|
||||
{
|
||||
public static void ConfigureWithOidc(
|
||||
ServiceConfigurationContext context,
|
||||
string authority,
|
||||
string[] scopes,
|
||||
string apiTitle,
|
||||
string apiVersion = "v1",
|
||||
string apiName = "v1",
|
||||
string[]? flows = null,
|
||||
string? discoveryEndpoint = null
|
||||
)
|
||||
{
|
||||
context.Services.AddAbpSwaggerGenWithOidc(
|
||||
authority: authority,
|
||||
scopes: scopes,
|
||||
flows: flows,
|
||||
discoveryEndpoint: discoveryEndpoint,
|
||||
options =>
|
||||
{
|
||||
options.SwaggerDoc(apiName, new OpenApiInfo { Title = apiTitle, Version = apiVersion });
|
||||
options.DocInclusionPredicate((docName, description) => true);
|
||||
options.CustomSchemaIds(type => type.FullName);
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,9 @@
|
||||
const originalFetch = window.fetch;
|
||||
|
||||
window.fetch = function (input, init) {
|
||||
if (init !== undefined && init.headers['RequestVerificationToken'] !== undefined) {
|
||||
delete init.headers['RequestVerificationToken'];
|
||||
}
|
||||
|
||||
return originalFetch.apply(this, arguments);
|
||||
};
|
||||
7
shared/KonSoft.Shared.Hosting.Gateways/Class1.cs
Normal file
7
shared/KonSoft.Shared.Hosting.Gateways/Class1.cs
Normal file
@ -0,0 +1,7 @@
|
||||
namespace KonSoft.Shared.Hosting.Gateways
|
||||
{
|
||||
public class Class1
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@ -2,8 +2,8 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<RootNamespace>KonSoft.Shared.Hosting.Gateways</RootNamespace>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@ -0,0 +1,27 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<RootNamespace>KonSoft.Shared.Hosting.Microservices</RootNamespace>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\KonSoft.Shared.Hosting.AspNetCore\KonSoft.Shared.Hosting.AspNetCore.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.DataProtection.StackExchangeRedis" Version="8.0.20" />
|
||||
<PackageReference Include="DistributedLock.Redis" Version="1.1.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Volo.Abp.AspNetCore.Authentication.JwtBearer" Version="8.3.4" />
|
||||
<PackageReference Include="Volo.Abp.EventBus.RabbitMQ" Version="8.3.4" />
|
||||
<PackageReference Include="Volo.Abp.BackgroundJobs.RabbitMQ" Version="8.3.4" />
|
||||
<PackageReference Include="Volo.Abp.Caching.StackExchangeRedis" Version="8.3.4" />
|
||||
<PackageReference Include="Volo.Abp.EntityFrameworkCore" Version="8.3.4" />
|
||||
<PackageReference Include="Volo.Abp.DistributedLocking" Version="8.3.4" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@ -0,0 +1,100 @@
|
||||
using KonSoft.Shared.Hosting.AspNetCore;
|
||||
using Medallion.Threading;
|
||||
using Medallion.Threading.Redis;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.DataProtection;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using StackExchange.Redis;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Microsoft.AspNetCore.Cors;
|
||||
using Volo.Abp.AspNetCore.Authentication.JwtBearer;
|
||||
using Volo.Abp.BackgroundJobs.RabbitMQ;
|
||||
using Volo.Abp.Caching;
|
||||
using Volo.Abp.Caching.StackExchangeRedis;
|
||||
using Volo.Abp.DistributedLocking;
|
||||
using Volo.Abp.EntityFrameworkCore;
|
||||
using Volo.Abp.EventBus.RabbitMq;
|
||||
using Volo.Abp.Modularity;
|
||||
using Volo.Abp.Security.Claims;
|
||||
|
||||
namespace KonSoft.Shared.Hosting.Microservices
|
||||
{
|
||||
[DependsOn(
|
||||
typeof(KonSoftSharedHostingAspNetCoreModule),
|
||||
typeof(AbpBackgroundJobsRabbitMqModule),
|
||||
typeof(AbpAspNetCoreAuthenticationJwtBearerModule),
|
||||
typeof(AbpEventBusRabbitMqModule),
|
||||
typeof(AbpCachingStackExchangeRedisModule),
|
||||
typeof(AbpDistributedLockingModule),
|
||||
typeof(AbpEntityFrameworkCoreModule)
|
||||
)]
|
||||
public class KonSoftSharedHostingMicroservicesModule : AbpModule
|
||||
{
|
||||
public override void ConfigureServices(ServiceConfigurationContext context)
|
||||
{
|
||||
var configuration = context.Services.GetConfiguration();
|
||||
var hostingEnvironment = context.Services.GetHostingEnvironment();
|
||||
|
||||
ConfigureCache();
|
||||
ConfigureDataProtection(context, configuration);
|
||||
ConfigureAuthentication(context, configuration);
|
||||
ConfigureCors(context, configuration);
|
||||
}
|
||||
|
||||
private void ConfigureCache()
|
||||
{
|
||||
Configure<AbpDistributedCacheOptions>(options => { options.KeyPrefix = "KonSoft:"; });
|
||||
}
|
||||
|
||||
private void ConfigureDataProtection(
|
||||
ServiceConfigurationContext context,
|
||||
IConfiguration configuration)
|
||||
{
|
||||
context.Services.AddDataProtection().SetApplicationName("KonSoft")
|
||||
.PersistKeysToStackExchangeRedis(ConnectionMultiplexer.Connect(configuration["Redis:Configuration"]!),
|
||||
"KonSoft-Protection-Keys");
|
||||
|
||||
context.Services.AddSingleton<IDistributedLockProvider>(_ =>
|
||||
new RedisDistributedSynchronizationProvider(ConnectionMultiplexer
|
||||
.Connect(configuration["Redis:Configuration"]!).GetDatabase()));
|
||||
}
|
||||
|
||||
private void ConfigureAuthentication(ServiceConfigurationContext context, IConfiguration configuration)
|
||||
{
|
||||
context.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
||||
.AddAbpJwtBearer(options =>
|
||||
{
|
||||
options.Authority = configuration["AuthServer:Authority"];
|
||||
options.RequireHttpsMetadata = configuration.GetValue<bool>("AuthServer:RequireHttpsMetadata");
|
||||
options.Audience = KonSoftConsts.AuthServerAudience;
|
||||
});
|
||||
|
||||
context.Services.Configure<AbpClaimsPrincipalFactoryOptions>(options =>
|
||||
{
|
||||
options.IsDynamicClaimsEnabled = true;
|
||||
});
|
||||
}
|
||||
|
||||
private void ConfigureCors(ServiceConfigurationContext context, IConfiguration configuration)
|
||||
{
|
||||
context.Services.AddCors(options =>
|
||||
{
|
||||
options.AddDefaultPolicy(builder =>
|
||||
{
|
||||
builder
|
||||
.WithOrigins(configuration["App:CorsOrigins"]?
|
||||
.Split(",", StringSplitOptions.RemoveEmptyEntries)
|
||||
.Select(o => o.RemovePostFix("/"))
|
||||
.ToArray() ?? [])
|
||||
.WithAbpExposedHeaders()
|
||||
.SetIsOriginAllowedToAllowWildcardSubdomains()
|
||||
.AllowAnyHeader()
|
||||
.AllowAnyMethod()
|
||||
.AllowCredentials();
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
21
shared/KonSoft.Shared.Hosting/KonSoft.Shared.Hosting.csproj
Normal file
21
shared/KonSoft.Shared.Hosting/KonSoft.Shared.Hosting.csproj
Normal file
@ -0,0 +1,21 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<RootNamespace>KonSoft.Shared.Hosting</RootNamespace>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
|
||||
<PackageReference Include="Serilog.Sinks.Async" Version="1.5.0" />
|
||||
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
|
||||
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Volo.Abp.Autofac" Version="8.3.4" />
|
||||
<PackageReference Include="Volo.Abp.Data" Version="8.3.4" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
21
shared/KonSoft.Shared.Hosting/KonSoftSharedHostingModule.cs
Normal file
21
shared/KonSoft.Shared.Hosting/KonSoftSharedHostingModule.cs
Normal file
@ -0,0 +1,21 @@
|
||||
using Volo.Abp.Autofac;
|
||||
using Volo.Abp.Data;
|
||||
using Volo.Abp.Modularity;
|
||||
|
||||
namespace KonSoft.Shared.Hosting
|
||||
{
|
||||
[DependsOn(
|
||||
typeof(AbpAutofacModule),
|
||||
typeof(AbpDataModule)
|
||||
)]
|
||||
public class KonSoftSharedHostingModule : AbpModule
|
||||
{
|
||||
public override void ConfigureServices(ServiceConfigurationContext context)
|
||||
{
|
||||
Configure<AbpDbConnectionOptions>(options =>
|
||||
{
|
||||
// TODO Mapping DbConnections
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,24 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<RootNamespace>KonSoft.Shared.Localization</RootNamespace>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="8.0.4" />
|
||||
<PackageReference Include="Volo.Abp.Validation" Version="8.3.4" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Include="Localization\EShopOnAbp\*.json" />
|
||||
<Content Remove="Localization\EShopOnAbp\*.json" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Include="Localization\KonSoft\en.json" />
|
||||
<EmbeddedResource Include="Localization\KonSoft\zh-Hans.json" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@ -0,0 +1,35 @@
|
||||
using KonSoft.Shared.Localization.Localization;
|
||||
using Volo.Abp.Localization;
|
||||
using Volo.Abp.Modularity;
|
||||
using Volo.Abp.Validation;
|
||||
using Volo.Abp.Validation.Localization;
|
||||
using Volo.Abp.VirtualFileSystem;
|
||||
|
||||
namespace KonSoft.Shared.Localization
|
||||
{
|
||||
|
||||
[DependsOn(
|
||||
typeof(AbpValidationModule)
|
||||
)]
|
||||
public class KonSoftSharedLocalizationModule : AbpModule
|
||||
{
|
||||
public override void ConfigureServices(ServiceConfigurationContext context)
|
||||
{
|
||||
Configure<AbpVirtualFileSystemOptions>(options =>
|
||||
{
|
||||
options.FileSets.AddEmbedded<KonSoftSharedLocalizationModule>();
|
||||
});
|
||||
|
||||
Configure<AbpLocalizationOptions>(options =>
|
||||
{
|
||||
options.Resources
|
||||
.Add<KonSoftResource>("en")
|
||||
.AddBaseTypes(
|
||||
typeof(AbpValidationResource)
|
||||
).AddVirtualJson("/Localization/KonSoft");
|
||||
|
||||
options.DefaultResourceType = typeof(KonSoftResource);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,9 @@
|
||||
{
|
||||
"culture": "en",
|
||||
"texts": {
|
||||
"AppName": "Admin",
|
||||
"Menu:Home": "Home",
|
||||
"Welcome": "Welcome",
|
||||
"LongWelcomeMessage": "Welcome to the application. This is a startup project based on the ABP framework. For more information, visit abp.io."
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,9 @@
|
||||
{
|
||||
"culture": "zh-Hans",
|
||||
"texts": {
|
||||
"AppName": "Admin",
|
||||
"Menu:Home": "首页",
|
||||
"Welcome": "欢迎",
|
||||
"LongWelcomeMessage": "欢迎使用本应用程序。这是一个基于 ABP 框架的启动项目。更多信息,请访问 abp.io。"
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,9 @@
|
||||
using Volo.Abp.Localization;
|
||||
|
||||
namespace KonSoft.Shared.Localization.Localization
|
||||
{
|
||||
[LocalizationResourceName("KonSoft")]
|
||||
public class KonSoftResource
|
||||
{
|
||||
}
|
||||
}
|
||||
@ -1,7 +0,0 @@
|
||||
namespace KonSoft.Shared
|
||||
{
|
||||
public class Class1
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user