diff --git a/KonSoft.sln b/KonSoft.sln index 00fccd6..fb66672 100644 --- a/KonSoft.sln +++ b/KonSoft.sln @@ -193,6 +193,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KonSoft.Shared.Hosting.Gate EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KonSoft.Shared.Hosting.Microservices", "shared\KonSoft.Shared.Hosting.Microservices\KonSoft.Shared.Hosting.Microservices.csproj", "{ADF28580-F8A0-4495-96D6-736C6C7CF3FF}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KonSoft.Shared.DbMigrator", "shared\KonSoft.Shared.DbMigrator\KonSoft.Shared.DbMigrator.csproj", "{AFCC65BC-8477-819B-2575-674AC9D2FDDD}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -499,6 +501,10 @@ Global {ADF28580-F8A0-4495-96D6-736C6C7CF3FF}.Debug|Any CPU.Build.0 = Debug|Any CPU {ADF28580-F8A0-4495-96D6-736C6C7CF3FF}.Release|Any CPU.ActiveCfg = Release|Any CPU {ADF28580-F8A0-4495-96D6-736C6C7CF3FF}.Release|Any CPU.Build.0 = Release|Any CPU + {AFCC65BC-8477-819B-2575-674AC9D2FDDD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AFCC65BC-8477-819B-2575-674AC9D2FDDD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AFCC65BC-8477-819B-2575-674AC9D2FDDD}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AFCC65BC-8477-819B-2575-674AC9D2FDDD}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -594,6 +600,7 @@ Global {8CA6B487-3AAF-4E77-ACE0-01D878DE4836} = {7EFFD2C6-2041-4967-A715-0F817D70C433} {BBB1A129-9ED7-4F08-B710-B6C287197BFB} = {7EFFD2C6-2041-4967-A715-0F817D70C433} {ADF28580-F8A0-4495-96D6-736C6C7CF3FF} = {7EFFD2C6-2041-4967-A715-0F817D70C433} + {AFCC65BC-8477-819B-2575-674AC9D2FDDD} = {7EFFD2C6-2041-4967-A715-0F817D70C433} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {28315BFD-90E7-4E14-A2EA-F3D23AF4126F} diff --git a/KonSoft.sln.DotSettings b/KonSoft.sln.DotSettings index cffb367..c044e41 100644 --- a/KonSoft.sln.DotSettings +++ b/KonSoft.sln.DotSettings @@ -21,4 +21,5 @@ False SQL True + True \ No newline at end of file diff --git a/applications/KonSoft.AuthServer/KonSoftAuthServerModule.cs b/applications/KonSoft.AuthServer/KonSoftAuthServerModule.cs index 5bd47c7..8368a45 100644 --- a/applications/KonSoft.AuthServer/KonSoftAuthServerModule.cs +++ b/applications/KonSoft.AuthServer/KonSoftAuthServerModule.cs @@ -60,7 +60,7 @@ public class KonSoftAuthServerModule : AbpModule PreConfigure(serverBuilder => { serverBuilder.AddProductionEncryptionAndSigningCertificate("openiddict.pfx", - configuration["Certificate:Password"]); + "59464dba-b66e-48cd-8b81-2e4a9c08c977"); }); } } diff --git a/modules/admin/src/KonSoft.Admin.Domain/Data/IAdminDbSchemaMigrator.cs b/modules/admin/src/KonSoft.Admin.Domain/Data/IAdminDbSchemaMigrator.cs deleted file mode 100644 index 6989d53..0000000 --- a/modules/admin/src/KonSoft.Admin.Domain/Data/IAdminDbSchemaMigrator.cs +++ /dev/null @@ -1,8 +0,0 @@ -using System.Threading.Tasks; - -namespace KonSoft.Admin.Data; - -public interface IAdminDbSchemaMigrator -{ - Task MigrateAsync(); -} \ No newline at end of file diff --git a/modules/admin/src/KonSoft.Admin.Domain/Data/NullAdminDbSchemaMigrator.cs b/modules/admin/src/KonSoft.Admin.Domain/Data/NullAdminDbSchemaMigrator.cs deleted file mode 100644 index 0d9c225..0000000 --- a/modules/admin/src/KonSoft.Admin.Domain/Data/NullAdminDbSchemaMigrator.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System.Threading.Tasks; -using Volo.Abp.DependencyInjection; - -namespace KonSoft.Admin.Data; - -/* This is used if database provider does't define - * IAdminDbSchemaMigrator implementation. - */ -public class NullAdminDbSchemaMigrator : IAdminDbSchemaMigrator, ITransientDependency -{ - public Task MigrateAsync() - { - return Task.CompletedTask; - } -} \ No newline at end of file diff --git a/modules/admin/src/KonSoft.Admin.Domain/Data/AdminDbMigrationService.cs b/shared/KonSoft.Shared.DbMigrator/Data/DbMigrationService.cs similarity index 94% rename from modules/admin/src/KonSoft.Admin.Domain/Data/AdminDbMigrationService.cs rename to shared/KonSoft.Shared.DbMigrator/Data/DbMigrationService.cs index 2828ccc..77d38d4 100644 --- a/modules/admin/src/KonSoft.Admin.Domain/Data/AdminDbMigrationService.cs +++ b/shared/KonSoft.Shared.DbMigrator/Data/DbMigrationService.cs @@ -13,19 +13,19 @@ using Volo.Abp.Identity; using Volo.Abp.MultiTenancy; using Volo.Abp.TenantManagement; -namespace KonSoft.Admin.Data; +namespace KonSoft.Shared.DbMigrator.Data; -public class AdminDbMigrationService : ITransientDependency +public class DbMigrationService : ITransientDependency { private readonly ICurrentTenant _currentTenant; private readonly IDataSeeder _dataSeeder; - private readonly IEnumerable _dbSchemaMigrators; + private readonly IEnumerable _dbSchemaMigrators; private readonly ITenantRepository _tenantRepository; - public AdminDbMigrationService( + public DbMigrationService( IDataSeeder dataSeeder, - IEnumerable dbSchemaMigrators, + IEnumerable dbSchemaMigrators, ITenantRepository tenantRepository, ICurrentTenant currentTenant) { @@ -34,10 +34,10 @@ public class AdminDbMigrationService : ITransientDependency _tenantRepository = tenantRepository; _currentTenant = currentTenant; - Logger = NullLogger.Instance; + Logger = NullLogger.Instance; } - public ILogger Logger { get; set; } + public ILogger Logger { get; set; } public async Task MigrateAsync() { diff --git a/modules/admin/src/KonSoft.Admin.EntityFrameworkCore/EntityFrameworkCore/EntityFrameworkCoreAdminDbSchemaMigrator.cs b/shared/KonSoft.Shared.DbMigrator/Data/EntityFrameworkCoreDbSchemaMigrator.cs similarity index 76% rename from modules/admin/src/KonSoft.Admin.EntityFrameworkCore/EntityFrameworkCore/EntityFrameworkCoreAdminDbSchemaMigrator.cs rename to shared/KonSoft.Shared.DbMigrator/Data/EntityFrameworkCoreDbSchemaMigrator.cs index 520ac06..3401b8b 100644 --- a/modules/admin/src/KonSoft.Admin.EntityFrameworkCore/EntityFrameworkCore/EntityFrameworkCoreAdminDbSchemaMigrator.cs +++ b/shared/KonSoft.Shared.DbMigrator/Data/EntityFrameworkCoreDbSchemaMigrator.cs @@ -1,18 +1,18 @@ using System; using System.Threading.Tasks; -using KonSoft.Admin.Data; +using KonSoft.Admin.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Volo.Abp.DependencyInjection; -namespace KonSoft.Admin.EntityFrameworkCore; +namespace KonSoft.Shared.DbMigrator.Data; -public class EntityFrameworkCoreAdminDbSchemaMigrator - : IAdminDbSchemaMigrator, ITransientDependency +public class EntityFrameworkCoreDbSchemaMigrator + : IDbSchemaMigrator, ITransientDependency { private readonly IServiceProvider _serviceProvider; - public EntityFrameworkCoreAdminDbSchemaMigrator( + public EntityFrameworkCoreDbSchemaMigrator( IServiceProvider serviceProvider) { _serviceProvider = serviceProvider; diff --git a/shared/KonSoft.Shared.DbMigrator/Data/IDbSchemaMigrator.cs b/shared/KonSoft.Shared.DbMigrator/Data/IDbSchemaMigrator.cs new file mode 100644 index 0000000..cae422d --- /dev/null +++ b/shared/KonSoft.Shared.DbMigrator/Data/IDbSchemaMigrator.cs @@ -0,0 +1,8 @@ +using System.Threading.Tasks; + +namespace KonSoft.Shared.DbMigrator.Data; + +public interface IDbSchemaMigrator +{ + Task MigrateAsync(); +} \ No newline at end of file diff --git a/shared/KonSoft.Shared.DbMigrator/DbMigratorHostedService.cs b/shared/KonSoft.Shared.DbMigrator/DbMigratorHostedService.cs new file mode 100644 index 0000000..76b39e7 --- /dev/null +++ b/shared/KonSoft.Shared.DbMigrator/DbMigratorHostedService.cs @@ -0,0 +1,51 @@ +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Serilog; +using System.Threading; +using System.Threading.Tasks; +using KonSoft.Shared.DbMigrator.Data; +using Volo.Abp; +using Volo.Abp.Data; + +namespace KonSoft.Shared.DbMigrator; + +public class DbMigratorHostedService : IHostedService +{ + private readonly IHostApplicationLifetime _hostApplicationLifetime; + private readonly IConfiguration _configuration; + + public DbMigratorHostedService(IHostApplicationLifetime hostApplicationLifetime, IConfiguration configuration) + { + _hostApplicationLifetime = hostApplicationLifetime; + _configuration = configuration; + } + + public async Task StartAsync(CancellationToken cancellationToken) + { + using (var application = await AbpApplicationFactory.CreateAsync(options => + { + options.Services.ReplaceConfiguration(_configuration); + options.UseAutofac(); + options.Services.AddLogging(c => c.AddSerilog()); + options.AddDataMigrationEnvironment(); + })) + { + await application.InitializeAsync(); + + await application + .ServiceProvider + .GetRequiredService() + .MigrateAsync(); + + await application.ShutdownAsync(); + + _hostApplicationLifetime.StopApplication(); + } + } + + public Task StopAsync(CancellationToken cancellationToken) + { + return Task.CompletedTask; + } +} diff --git a/shared/KonSoft.Shared.DbMigrator/Dockerfile b/shared/KonSoft.Shared.DbMigrator/Dockerfile new file mode 100644 index 0000000..a563b7f --- /dev/null +++ b/shared/KonSoft.Shared.DbMigrator/Dockerfile @@ -0,0 +1,26 @@ +FROM mcr.microsoft.com/dotnet/runtime:9.0 AS base +USER $APP_UID +WORKDIR /app + +FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build +ARG BUILD_CONFIGURATION=Release +COPY ["./common.props", "."] +COPY ["./NuGet.Config", "."] +COPY ["./src/KonSoft.SnowClub.DbMigrator/KonSoft.SnowClub.DbMigrator.csproj", "KonSoft.SnowClub.DbMigrator/"] +COPY ["./src/KonSoft.SnowClub.Application.Contracts/KonSoft.SnowClub.Application.Contracts.csproj", "KonSoft.SnowClub.Application.Contracts/"] +COPY ["./src/KonSoft.SnowClub.Domain.Shared/KonSoft.SnowClub.Domain.Shared.csproj", "KonSoft.SnowClub.Domain.Shared/"] +COPY ["./src/KonSoft.SnowClub.EntityFrameworkCore/KonSoft.SnowClub.EntityFrameworkCore.csproj", "KonSoft.SnowClub.EntityFrameworkCore/"] +COPY ["./src/KonSoft.SnowClub.Domain/KonSoft.SnowClub.Domain.csproj", "KonSoft.SnowClub.Domain/"] +RUN dotnet restore "./KonSoft.SnowClub.DbMigrator/KonSoft.SnowClub.DbMigrator.csproj" +COPY . . +WORKDIR "/src/KonSoft.SnowClub.DbMigrator" +RUN dotnet build "./KonSoft.SnowClub.DbMigrator.csproj" -c $BUILD_CONFIGURATION -o /app/build + +FROM build AS publish +ARG BUILD_CONFIGURATION=Release +RUN dotnet publish "./KonSoft.SnowClub.DbMigrator.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false + +FROM base AS final +WORKDIR /app +COPY --from=publish /app/publish . +ENTRYPOINT ["dotnet", "KonSoft.SnowClub.DbMigrator.dll"] \ No newline at end of file diff --git a/shared/KonSoft.Shared.DbMigrator/KonSoft.Shared.DbMigrator.csproj b/shared/KonSoft.Shared.DbMigrator/KonSoft.Shared.DbMigrator.csproj new file mode 100644 index 0000000..245bf11 --- /dev/null +++ b/shared/KonSoft.Shared.DbMigrator/KonSoft.Shared.DbMigrator.csproj @@ -0,0 +1,31 @@ + + + + Exe + net8.0 + enable + + + + + + PreserveNewest + Always + + + + + + + + + + + + + + + + + + diff --git a/shared/KonSoft.Shared.DbMigrator/KonSoftSharedDbMigratorModule.cs b/shared/KonSoft.Shared.DbMigrator/KonSoftSharedDbMigratorModule.cs new file mode 100644 index 0000000..e2a09f8 --- /dev/null +++ b/shared/KonSoft.Shared.DbMigrator/KonSoftSharedDbMigratorModule.cs @@ -0,0 +1,20 @@ +using KonSoft.Admin; +using KonSoft.Admin.EntityFrameworkCore; +using KonSoft.Shared.Hosting; +using Volo.Abp.Caching; +using Volo.Abp.Modularity; + +namespace KonSoft.Shared.DbMigrator; + +[DependsOn( + typeof(KonSoftSharedHostingModule), + typeof(AdminEntityFrameworkCoreModule), + typeof(AdminApplicationContractsModule) + )] +public class KonSoftSharedDbMigratorModule : AbpModule +{ + public override void ConfigureServices(ServiceConfigurationContext context) + { + Configure(options => { options.KeyPrefix = "KonSoft:"; }); + } +} diff --git a/shared/KonSoft.Shared.DbMigrator/OpenIddict/OpenIddictApplication.cs b/shared/KonSoft.Shared.DbMigrator/OpenIddict/OpenIddictApplication.cs new file mode 100644 index 0000000..ec387fc --- /dev/null +++ b/shared/KonSoft.Shared.DbMigrator/OpenIddict/OpenIddictApplication.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace KonSoft.Shared.DbMigrator.OpenIddict +{ + public class OpenIddictApplicationOptions + { + public string Name { get; set; } + public string Type { get; set; } + public string ConsentType { get; set; } + public string DisplayName { get; set; } + public string? Secret { get; set; } + public List GrantTypes { get; set; } + public string? PostLogoutRedirectUri { get; set; } + } +} diff --git a/modules/admin/src/KonSoft.Admin.Domain/OpenIddict/OpenIddictDataSeedContributor.cs b/shared/KonSoft.Shared.DbMigrator/OpenIddict/OpenIddictDataSeedContributor.cs similarity index 85% rename from modules/admin/src/KonSoft.Admin.Domain/OpenIddict/OpenIddictDataSeedContributor.cs rename to shared/KonSoft.Shared.DbMigrator/OpenIddict/OpenIddictDataSeedContributor.cs index f805d73..c38c0c7 100644 --- a/modules/admin/src/KonSoft.Admin.Domain/OpenIddict/OpenIddictDataSeedContributor.cs +++ b/shared/KonSoft.Shared.DbMigrator/OpenIddict/OpenIddictDataSeedContributor.cs @@ -16,7 +16,7 @@ using Volo.Abp.OpenIddict.Scopes; using Volo.Abp.PermissionManagement; using Volo.Abp.Uow; -namespace KonSoft.Admin.OpenIddict; +namespace KonSoft.Shared.DbMigrator.OpenIddict; /* Creates initial data that is needed to property run the application * and make client-to-server communication possible. @@ -62,7 +62,49 @@ public class OpenIddictDataSeedContributor : IDataSeedContributor, ITransientDep { await _scopeManager.CreateAsync(new OpenIddictScopeDescriptor { - Name = "Admin", DisplayName = "Admin API", Resources = { "Admin" } + Name = "Admin", + DisplayName = "Admin API", + Resources = { "Admin" } + }); + } + + if (await _openIddictScopeRepository.FindByNameAsync("Dispatch") == null) + { + await _scopeManager.CreateAsync(new OpenIddictScopeDescriptor + { + Name = "Dispatch", + DisplayName = "Dispatch API", + Resources = { "Dispatch" } + }); + } + + if (await _openIddictScopeRepository.FindByNameAsync("Payment") == null) + { + await _scopeManager.CreateAsync(new OpenIddictScopeDescriptor + { + Name = "Payment", + DisplayName = "Payment API", + Resources = { "Payment" } + }); + } + + if (await _openIddictScopeRepository.FindByNameAsync("Report") == null) + { + await _scopeManager.CreateAsync(new OpenIddictScopeDescriptor + { + Name = "Report", + DisplayName = "Report API", + Resources = { "Report" } + }); + } + + if (await _openIddictScopeRepository.FindByNameAsync("TenantManagement") == null) + { + await _scopeManager.CreateAsync(new OpenIddictScopeDescriptor + { + Name = "TenantManagement", + DisplayName = "TenantManagement API", + Resources = { "TenantManagement" } }); } } @@ -76,29 +118,20 @@ public class OpenIddictDataSeedContributor : IDataSeedContributor, ITransientDep OpenIddictConstants.Permissions.Scopes.Phone, OpenIddictConstants.Permissions.Scopes.Profile, OpenIddictConstants.Permissions.Scopes.Roles, - "Admin" + "Admin", + "Dispatch", + "Payment", + "Report", + "TenantManagement" }; - var configurationSection = _configuration.GetSection("OpenIddict:Applications"); - - - // Swagger Client - var swaggerClientId = configurationSection["Admin_Swagger:ClientId"]; - if (!swaggerClientId.IsNullOrWhiteSpace()) + var apps = new List(); + _configuration.GetSection("OpenIddict:Applications").Bind(apps); + foreach (var openIddictApplication in apps) { - var swaggerRootUrl = configurationSection["Admin_Swagger:RootUrl"]?.TrimEnd('/'); - - await CreateApplicationAsync( - swaggerClientId!, - OpenIddictConstants.ClientTypes.Public, - OpenIddictConstants.ConsentTypes.Implicit, - "Swagger Application", - null, - [OpenIddictConstants.GrantTypes.AuthorizationCode], - commonScopes, - redirectUri: $"{swaggerRootUrl}/swagger/oauth2-redirect.html", - clientUri: swaggerRootUrl - ); + await CreateApplicationAsync(openIddictApplication.Name, openIddictApplication.Type, + openIddictApplication.ConsentType, openIddictApplication.DisplayName, openIddictApplication.Secret, + openIddictApplication.GrantTypes, commonScopes, openIddictApplication.PostLogoutRedirectUri); } } diff --git a/shared/KonSoft.Shared.DbMigrator/Program.cs b/shared/KonSoft.Shared.DbMigrator/Program.cs new file mode 100644 index 0000000..6edaeef --- /dev/null +++ b/shared/KonSoft.Shared.DbMigrator/Program.cs @@ -0,0 +1,44 @@ +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using Serilog; +using Serilog.Events; +using System; +using System.Threading.Tasks; + +namespace KonSoft.Shared.DbMigrator; + +class Program +{ + static async Task Main(string[] args) + { + Log.Logger = new LoggerConfiguration() + .MinimumLevel.Information() + .MinimumLevel.Override("Microsoft", LogEventLevel.Warning) + .MinimumLevel.Override("Volo.Abp", LogEventLevel.Warning) +#if DEBUG + .MinimumLevel.Override("KonSoft.Clean", LogEventLevel.Debug) +#else + .MinimumLevel.Override("KonSoft.Clean", LogEventLevel.Information) +#endif + .Enrich.FromLogContext() + .WriteTo.Async(c => c.File("Logs/logs.txt")) + .WriteTo.Async(c => c.Console()) + .CreateLogger(); + + await CreateHostBuilder(args).RunConsoleAsync(); + } + + private static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .UseAgileConfig(options => + { + options.ENV = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Development"; + }) + .AddAppSettingsSecretsJson() + .ConfigureLogging((context, logging) => logging.ClearProviders()) + .ConfigureServices((hostContext, services) => + { + services.AddHostedService(); + }); +} diff --git a/shared/KonSoft.Shared.DbMigrator/Properties/launchSettings.json b/shared/KonSoft.Shared.DbMigrator/Properties/launchSettings.json new file mode 100644 index 0000000..a5e90df --- /dev/null +++ b/shared/KonSoft.Shared.DbMigrator/Properties/launchSettings.json @@ -0,0 +1,7 @@ +{ + "profiles": { + "KonSoft.SnowClub.DbMigrator": { + "commandName": "Project" + } + } +} \ No newline at end of file diff --git a/shared/KonSoft.Shared.DbMigrator/appsettings.json b/shared/KonSoft.Shared.DbMigrator/appsettings.json new file mode 100644 index 0000000..668fcd9 --- /dev/null +++ b/shared/KonSoft.Shared.DbMigrator/appsettings.json @@ -0,0 +1,8 @@ +{ + "AgileConfig": { + "appId": "KonSoft.Shared.DbMigrator", + "name": "KonSoft.Shared.DbMigrator", + "nodes": "https://config.konsoft.top/", + "secret": "DBE31703-14F9-4B01-893D-900B8380CE04" + } +}