From 9868dece7e9d68b82d99abec71e2316266b28d98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Wed, 4 Mar 2020 17:07:26 +0300 Subject: [PATCH] Create TenantManagementService.Host application. --- samples/MicroserviceDemo/MicroserviceDemo.sln | 11 +- .../BloggingService.Host/Program.cs | 4 +- .../BloggingService.Host/Startup.cs | 4 +- .../TenantManagementService.Host/Program.cs | 64 ++++++++++ .../Properties/launchSettings.json | 27 ++++ .../TenantManagementService.Host/Startup.cs | 20 +++ .../TenantManagementService.Host.csproj | 43 +++++++ .../TenantManagementServiceHostModule.cs | 118 ++++++++++++++++++ .../appsettings.Development.json | 9 ++ .../appsettings.json | 10 ++ 10 files changed, 303 insertions(+), 7 deletions(-) create mode 100644 samples/MicroserviceDemo/microservices/TenantManagementService.Host/Program.cs create mode 100644 samples/MicroserviceDemo/microservices/TenantManagementService.Host/Properties/launchSettings.json create mode 100644 samples/MicroserviceDemo/microservices/TenantManagementService.Host/Startup.cs create mode 100644 samples/MicroserviceDemo/microservices/TenantManagementService.Host/TenantManagementService.Host.csproj create mode 100644 samples/MicroserviceDemo/microservices/TenantManagementService.Host/TenantManagementServiceHostModule.cs create mode 100644 samples/MicroserviceDemo/microservices/TenantManagementService.Host/appsettings.Development.json create mode 100644 samples/MicroserviceDemo/microservices/TenantManagementService.Host/appsettings.json diff --git a/samples/MicroserviceDemo/MicroserviceDemo.sln b/samples/MicroserviceDemo/MicroserviceDemo.sln index 65c254b4c6..311744e4a9 100644 --- a/samples/MicroserviceDemo/MicroserviceDemo.sln +++ b/samples/MicroserviceDemo/MicroserviceDemo.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -VisualStudioVersion = 15.0.28307.168 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.29609.76 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "microservices", "microservices", "{3B26D176-390B-4F51-BE52-E9B422C28A88}" EndProject @@ -61,6 +61,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleClientDemo", "applic EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PublicWebSite.Host", "applications\PublicWebSite.Host\PublicWebSite.Host.csproj", "{29A3A4EF-DEC4-4E43-B6B8-D565667F85EC}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TenantManagementService.Host", "microservices\TenantManagementService.Host\TenantManagementService.Host.csproj", "{3E1DDEDA-A897-470F-81C2-5A3AFA6D0B61}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -155,6 +157,10 @@ Global {29A3A4EF-DEC4-4E43-B6B8-D565667F85EC}.Debug|Any CPU.Build.0 = Debug|Any CPU {29A3A4EF-DEC4-4E43-B6B8-D565667F85EC}.Release|Any CPU.ActiveCfg = Release|Any CPU {29A3A4EF-DEC4-4E43-B6B8-D565667F85EC}.Release|Any CPU.Build.0 = Release|Any CPU + {3E1DDEDA-A897-470F-81C2-5A3AFA6D0B61}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3E1DDEDA-A897-470F-81C2-5A3AFA6D0B61}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3E1DDEDA-A897-470F-81C2-5A3AFA6D0B61}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3E1DDEDA-A897-470F-81C2-5A3AFA6D0B61}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -185,6 +191,7 @@ Global {2199D857-C926-4948-A7F7-A11E344F90AA} = {8F6834D7-E6FA-4A04-83BB-955F68EA0A0A} {BB8FA269-460D-42BE-90A0-E97D6A3FDEB1} = {8F6834D7-E6FA-4A04-83BB-955F68EA0A0A} {29A3A4EF-DEC4-4E43-B6B8-D565667F85EC} = {8F6834D7-E6FA-4A04-83BB-955F68EA0A0A} + {3E1DDEDA-A897-470F-81C2-5A3AFA6D0B61} = {3B26D176-390B-4F51-BE52-E9B422C28A88} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {37474F0D-2E52-4D2F-B39B-7FE3FF31B4EC} diff --git a/samples/MicroserviceDemo/microservices/BloggingService.Host/Program.cs b/samples/MicroserviceDemo/microservices/BloggingService.Host/Program.cs index 4a2f3d30c2..e09fc7b1cc 100644 --- a/samples/MicroserviceDemo/microservices/BloggingService.Host/Program.cs +++ b/samples/MicroserviceDemo/microservices/BloggingService.Host/Program.cs @@ -37,13 +37,13 @@ namespace BloggingService.Host try { - Log.Information("Starting IdentityService.Host."); + Log.Information("Starting BloggingService.Host."); CreateHostBuilder(args).Build().Run(); return 0; } catch (Exception ex) { - Log.Fatal(ex, "IdentityService.Host terminated unexpectedly!"); + Log.Fatal(ex, "BloggingService.Host terminated unexpectedly!"); return 1; } finally diff --git a/samples/MicroserviceDemo/microservices/BloggingService.Host/Startup.cs b/samples/MicroserviceDemo/microservices/BloggingService.Host/Startup.cs index e3ad346cb7..4d3cc2b90a 100644 --- a/samples/MicroserviceDemo/microservices/BloggingService.Host/Startup.cs +++ b/samples/MicroserviceDemo/microservices/BloggingService.Host/Startup.cs @@ -1,9 +1,7 @@ -using System; -using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; -using Volo.Abp; namespace BloggingService.Host { diff --git a/samples/MicroserviceDemo/microservices/TenantManagementService.Host/Program.cs b/samples/MicroserviceDemo/microservices/TenantManagementService.Host/Program.cs new file mode 100644 index 0000000000..2f0d621979 --- /dev/null +++ b/samples/MicroserviceDemo/microservices/TenantManagementService.Host/Program.cs @@ -0,0 +1,64 @@ +using System; +using System.IO; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; +using Serilog; +using Serilog.Events; +using Serilog.Sinks.Elasticsearch; + +namespace TenantManagementService.Host +{ + public class Program + { + public static int Main(string[] args) + { + //TODO: Temporary: it's not good to read appsettings.json here just to configure logging + var configuration = new ConfigurationBuilder() + .SetBasePath(Directory.GetCurrentDirectory()) + .AddJsonFile("appsettings.json") + .AddEnvironmentVariables() + .Build(); + + Log.Logger = new LoggerConfiguration() + .MinimumLevel.Debug() + .MinimumLevel.Override("Microsoft", LogEventLevel.Information) + .Enrich.WithProperty("Application", "TenantManagementService") + .Enrich.FromLogContext() + .WriteTo.File("Logs/logs.txt") + .WriteTo.Elasticsearch( + new ElasticsearchSinkOptions(new Uri(configuration["ElasticSearch:Url"])) + { + AutoRegisterTemplate = true, + AutoRegisterTemplateVersion = AutoRegisterTemplateVersion.ESv6, + IndexFormat = "msdemo-log-{0:yyyy.MM}" + }) + .CreateLogger(); + + try + { + Log.Information("Starting TenantManagementService.Host."); + CreateHostBuilder(args).Build().Run(); + return 0; + } + catch (Exception ex) + { + Log.Fatal(ex, "TenantManagementService.Host terminated unexpectedly!"); + return 1; + } + finally + { + Log.CloseAndFlush(); + } + } + + internal static IHostBuilder CreateHostBuilder(string[] args) => + Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + }) + .UseAutofac() + .UseSerilog(); + } +} diff --git a/samples/MicroserviceDemo/microservices/TenantManagementService.Host/Properties/launchSettings.json b/samples/MicroserviceDemo/microservices/TenantManagementService.Host/Properties/launchSettings.json new file mode 100644 index 0000000000..9c88d1143c --- /dev/null +++ b/samples/MicroserviceDemo/microservices/TenantManagementService.Host/Properties/launchSettings.json @@ -0,0 +1,27 @@ +{ + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:59835", + "sslPort": 0 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "TenantManagementService.Host": { + "commandName": "Project", + "launchBrowser": true, + "applicationUrl": "http://localhost:5000", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/samples/MicroserviceDemo/microservices/TenantManagementService.Host/Startup.cs b/samples/MicroserviceDemo/microservices/TenantManagementService.Host/Startup.cs new file mode 100644 index 0000000000..da1f2ae6ef --- /dev/null +++ b/samples/MicroserviceDemo/microservices/TenantManagementService.Host/Startup.cs @@ -0,0 +1,20 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; + +namespace TenantManagementService.Host +{ + public class Startup + { + public void ConfigureServices(IServiceCollection services) + { + services.AddApplication(); + } + + public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory) + { + app.InitializeApplication(); + } + } +} \ No newline at end of file diff --git a/samples/MicroserviceDemo/microservices/TenantManagementService.Host/TenantManagementService.Host.csproj b/samples/MicroserviceDemo/microservices/TenantManagementService.Host/TenantManagementService.Host.csproj new file mode 100644 index 0000000000..c900402598 --- /dev/null +++ b/samples/MicroserviceDemo/microservices/TenantManagementService.Host/TenantManagementService.Host.csproj @@ -0,0 +1,43 @@ + + + + netcoreapp3.1 + $(AssetTargetFallback);portable-net45+win8+wp8+wpa81; + true + true + true + true + false + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/samples/MicroserviceDemo/microservices/TenantManagementService.Host/TenantManagementServiceHostModule.cs b/samples/MicroserviceDemo/microservices/TenantManagementService.Host/TenantManagementServiceHostModule.cs new file mode 100644 index 0000000000..946de5b37e --- /dev/null +++ b/samples/MicroserviceDemo/microservices/TenantManagementService.Host/TenantManagementServiceHostModule.cs @@ -0,0 +1,118 @@ +using System.Collections.Generic; +using System.Linq; +using System.Security.Claims; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.DataProtection; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.OpenApi.Models; +using StackExchange.Redis; +using Volo.Abp; +using Volo.Abp.Auditing; +using Volo.Abp.AuditLogging.EntityFrameworkCore; +using Volo.Abp.Autofac; +using Volo.Abp.EntityFrameworkCore; +using Volo.Abp.EntityFrameworkCore.SqlServer; +using Volo.Abp.EventBus.RabbitMq; +using Volo.Abp.Localization; +using Volo.Abp.Modularity; +using Volo.Abp.PermissionManagement.EntityFrameworkCore; +using Volo.Abp.Security.Claims; +using Volo.Abp.SettingManagement.EntityFrameworkCore; +using Volo.Abp.TenantManagement; +using Volo.Abp.TenantManagement.EntityFrameworkCore; + +namespace TenantManagementService.Host +{ + [DependsOn( + typeof(AbpAutofacModule), + typeof(AbpEventBusRabbitMqModule), + typeof(AbpEntityFrameworkCoreSqlServerModule), + typeof(AbpAuditLoggingEntityFrameworkCoreModule), + typeof(AbpPermissionManagementEntityFrameworkCoreModule), + typeof(AbpSettingManagementEntityFrameworkCoreModule), + typeof(AbpTenantManagementHttpApiModule), + typeof(AbpTenantManagementEntityFrameworkCoreModule), + typeof(AbpTenantManagementApplicationModule) + )] + public class TenantManagementServiceHostModule : AbpModule + { + public override void ConfigureServices(ServiceConfigurationContext context) + { + var configuration = context.Services.GetConfiguration(); + + context.Services.AddAuthentication("Bearer") + .AddIdentityServerAuthentication(options => + { + options.Authority = configuration["AuthServer:Authority"]; + options.ApiName = configuration["AuthServer:ApiName"]; + options.RequireHttpsMetadata = false; + }); + + context.Services.AddSwaggerGen(options => + { + options.SwaggerDoc("v1", new OpenApiInfo {Title = "Identity Service API", Version = "v1"}); + options.DocInclusionPredicate((docName, description) => true); + options.CustomSchemaIds(type => type.FullName); + }); + + Configure(options => + { + options.Languages.Add(new LanguageInfo("en", "en", "English")); + }); + + Configure(options => + { + options.UseSqlServer(); + }); + + context.Services.AddStackExchangeRedisCache(options => + { + options.Configuration = configuration["Redis:Configuration"]; + }); + + Configure(options => + { + options.IsEnabledForGetRequests = true; + options.ApplicationName = "IdentityService"; + }); + + var redis = ConnectionMultiplexer.Connect(configuration["Redis:Configuration"]); + context.Services.AddDataProtection() + .PersistKeysToStackExchangeRedis(redis, "MsDemo-DataProtection-Keys"); + } + + public override void OnApplicationInitialization(ApplicationInitializationContext context) + { + var app = context.GetApplicationBuilder(); + + app.UseCorrelationId(); + app.UseVirtualFiles(); + app.UseRouting(); + app.UseAuthentication(); + + app.Use(async (ctx, next) => + { + var currentPrincipalAccessor = ctx.RequestServices.GetRequiredService(); + var map = new Dictionary() + { + { "sub", AbpClaimTypes.UserId }, + { "role", AbpClaimTypes.Role }, + { "email", AbpClaimTypes.Email }, + //any other map + }; + var mapClaims = currentPrincipalAccessor.Principal.Claims.Where(p => map.Keys.Contains(p.Type)).ToList(); + currentPrincipalAccessor.Principal.AddIdentity(new ClaimsIdentity(mapClaims.Select(p => new Claim(map[p.Type], p.Value, p.ValueType, p.Issuer)))); + await next(); + }); + + app.UseAbpRequestLocalization(); //TODO: localization? + app.UseSwagger(); + app.UseSwaggerUI(options => + { + options.SwaggerEndpoint("/swagger/v1/swagger.json", "Identity Service API"); + }); + app.UseAuditing(); + app.UseMvcWithDefaultRouteAndArea(); + } + } +} diff --git a/samples/MicroserviceDemo/microservices/TenantManagementService.Host/appsettings.Development.json b/samples/MicroserviceDemo/microservices/TenantManagementService.Host/appsettings.Development.json new file mode 100644 index 0000000000..8983e0fc1c --- /dev/null +++ b/samples/MicroserviceDemo/microservices/TenantManagementService.Host/appsettings.Development.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git a/samples/MicroserviceDemo/microservices/TenantManagementService.Host/appsettings.json b/samples/MicroserviceDemo/microservices/TenantManagementService.Host/appsettings.json new file mode 100644 index 0000000000..d9d9a9bff6 --- /dev/null +++ b/samples/MicroserviceDemo/microservices/TenantManagementService.Host/appsettings.json @@ -0,0 +1,10 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "AllowedHosts": "*" +}