diff --git a/src/Services/Masa.Dcc.Service/Infrastructure/ServiceBaseExtensions.cs b/src/Services/Masa.Dcc.Service/Infrastructure/ServiceBaseExtensions.cs new file mode 100644 index 0000000..4666708 --- /dev/null +++ b/src/Services/Masa.Dcc.Service/Infrastructure/ServiceBaseExtensions.cs @@ -0,0 +1,27 @@ +// Copyright (c) MASA Stack All rights reserved. +// Licensed under the Apache License. See LICENSE.txt in the project root for license information. + +using Masa.BuildingBlocks.Data; +using Masa.Contrib.Service.MinimalAPIs; +using Microsoft.Extensions.Options; + +namespace Microsoft.AspNetCore.Builder; + +public static class ServiceBaseExtensions +{ + public static RouteHandlerBuilder MapGet(this ServiceBase service, string pattern, Delegate handler) => MapMethods(service, HttpMethods.Get, pattern, handler); + + public static RouteHandlerBuilder MapPost(this ServiceBase service, string pattern, Delegate handler) => MapMethods(service, HttpMethods.Post, pattern, handler); + + public static RouteHandlerBuilder MapPut(this ServiceBase service, string pattern, Delegate handler) => MapMethods(service, HttpMethods.Put, pattern, handler); + + public static RouteHandlerBuilder MapDelete(this ServiceBase service, string pattern, Delegate handler) => MapMethods(service, HttpMethods.Delete, pattern, handler); + + private static RouteHandlerBuilder MapMethods(this ServiceBase service, string method, string pattern, Delegate handler) + { + var builder = service.App.MapMethods(pattern, [method], handler); + var options = MasaApp.GetRequiredService>().Value; + options.RouteHandlerBuilder?.Invoke(builder); + return builder; + } +} diff --git a/src/Services/Masa.Dcc.Service/Program.cs b/src/Services/Masa.Dcc.Service/Program.cs index ebd3605..d796568 100644 --- a/src/Services/Masa.Dcc.Service/Program.cs +++ b/src/Services/Masa.Dcc.Service/Program.cs @@ -81,7 +81,8 @@ builder.Services .AddValidatorsFromAssemblyContaining() - .AddFluentValidationAutoValidation(configuration => { + .AddFluentValidationAutoValidation(configuration => + { configuration.OverrideDefaultResultFactoryWith(); }); @@ -167,46 +168,43 @@ .UseRepository(); }); + +builder.Services.AddAutoInject([typeof(IAppConfigObjectRepository).Assembly, typeof(LabelDomainService).Assembly, typeof(Masa.Dcc.Service.Admin.Services.AppService).Assembly]); + + builder.Services.AddI18n(Path.Combine("Assets", "I18n")); //seed data await builder.SeedDataAsync(); -var app = builder.Services.AddServices(builder, [typeof(IAppConfigObjectRepository).Assembly, typeof(LabelDomainService).Assembly, typeof(Masa.Dcc.Service.Admin.Services.AppService).Assembly]); -if (app.Environment.IsDevelopment()) +var app = builder.AddServices(config => { - app.UseMasaExceptionHandler(options => + config.DisableAutoMapRoute = true; + config.RouteHandlerBuilder = (builder) => { - options.ExceptionHandler = context => - { - if (context.Exception is not UserFriendlyException) - { - context.ToResult(context.Exception.Message); - } - }; - }); -} -else + builder.RequireAuthorization(); + builder.AddFluentValidationAutoValidation(); + }; +}); + +app.UseMasaExceptionHandler(opt => { - app.UseMasaExceptionHandler(opt => + opt.ExceptionHandler = context => { - opt.ExceptionHandler = context => + if (context.Exception is UserFriendlyException userFriendlyException) { - if (context.Exception is UserFriendlyException userFriendlyException) - { - context.ToResult(userFriendlyException.ErrorCode!, 299); - } - else if (context.Exception is ValidationException validationException) - { - context.ToResult(validationException.Errors.Select(err => err.ToString()).FirstOrDefault()!); - } - else if (context.Exception is UserStatusException userStatusException) - { - context.ToResult(userStatusException.Message, 293); - } - }; - }); -} + context.ToResult(userFriendlyException.ErrorCode!, 299); + } + else if (context.Exception is ValidationException validationException) + { + context.ToResult(validationException.Errors.Select(err => err.ToString()).FirstOrDefault()!); + } + else if (context.Exception is UserStatusException userStatusException) + { + context.ToResult(userStatusException.Message, 293); + } + }; +}); // Configure the HTTP request pipeline. #if DEBUG diff --git a/src/Services/Masa.Dcc.Service/Services/AppService.cs b/src/Services/Masa.Dcc.Service/Services/AppService.cs index eeece36..8236424 100644 --- a/src/Services/Masa.Dcc.Service/Services/AppService.cs +++ b/src/Services/Masa.Dcc.Service/Services/AppService.cs @@ -3,19 +3,17 @@ namespace Masa.Dcc.Service.Admin.Services; -[Authorize] public class AppService : ServiceBase { public AppService() - { - RouteOptions.DisableAutoMapRoute = true; - App.MapGet("api/v1/app/{id}", GetAsync); - App.MapPost("api/v1/projects/app", GetListByProjectIdsAsync); - App.MapGet("api/v1/appWithEnvCluster/{id}", GetWithEnvironmentClusterAsync); - App.MapPost("api/v1/app/pin/{appId}", AddAppPinAsync); - App.MapDelete("api/v1/app/pin/{appId}", RemoveAppPinAsync); - App.MapGet("api/v1/app/pin", GetAppPinListAsync); - App.MapPost("api/v1/app/latestReleaseConfig", GetLatestReleaseConfigByAppAsync); + { + this.MapGet("api/v1/app/{id}", GetAsync); + this.MapPost("api/v1/projects/app", GetListByProjectIdsAsync); + this.MapGet("api/v1/appWithEnvCluster/{id}", GetWithEnvironmentClusterAsync); + this.MapPost("api/v1/app/pin/{appId}", AddAppPinAsync); + this.MapDelete("api/v1/app/pin/{appId}", RemoveAppPinAsync); + this.MapGet("api/v1/app/pin", GetAppPinListAsync); + this.MapPost("api/v1/app/latestReleaseConfig", GetLatestReleaseConfigByAppAsync); } public Task GetAsync(IPmClient pmClient, int id) diff --git a/src/Services/Masa.Dcc.Service/Services/BizConfigService.cs b/src/Services/Masa.Dcc.Service/Services/BizConfigService.cs index 5e5a379..c41ed4e 100644 --- a/src/Services/Masa.Dcc.Service/Services/BizConfigService.cs +++ b/src/Services/Masa.Dcc.Service/Services/BizConfigService.cs @@ -7,11 +7,11 @@ public class BizConfigService : ServiceBase { public BizConfigService() { - RouteOptions.DisableAutoMapRoute = true; - App.MapPost("api/v1/bizConfig", AddAsync).AddFluentValidationAutoValidation(); - App.MapPut("api/v1/bizConfig", UpdateAsync).AddFluentValidationAutoValidation(); - App.MapGet("api/v1/bizConfig/{identity}", GetAsync); - App.MapPost("api/v1/bizConfig/latestReleaseConfig", GetLatestReleaseConfigByProjectAsync); + + this.MapPost("api/v1/bizConfig", AddAsync); + this.MapPut("api/v1/bizConfig", UpdateAsync); + this.MapGet("api/v1/bizConfig/{identity}", GetAsync); + this.MapPost("api/v1/bizConfig/latestReleaseConfig", GetLatestReleaseConfigByProjectAsync); } public async Task> GetLatestReleaseConfigByProjectAsync(IEventBus eventBus, diff --git a/src/Services/Masa.Dcc.Service/Services/ClusterService.cs b/src/Services/Masa.Dcc.Service/Services/ClusterService.cs index b5e6085..ab07f18 100644 --- a/src/Services/Masa.Dcc.Service/Services/ClusterService.cs +++ b/src/Services/Masa.Dcc.Service/Services/ClusterService.cs @@ -3,16 +3,14 @@ namespace Masa.Dcc.Service.Admin.Services; -[Authorize] public class ClusterService : ServiceBase { public ClusterService(IPmClient pmClient) { - RouteOptions.DisableAutoMapRoute = true; - App.MapGet("api/v1/cluster", GetListAsync); - App.MapGet("api/v1/cluster/{Id}", GetAsync); - App.MapGet("api/v1/envClusters", GetEnvironmentClustersAsync); - App.MapGet("api/v1/{envId}/cluster", GetListByEnvIdAsync); + this.MapGet("api/v1/cluster", GetListAsync); + this.MapGet("api/v1/cluster/{Id}", GetAsync); + this.MapGet("api/v1/envClusters", GetEnvironmentClustersAsync); + this.MapGet("api/v1/{envId}/cluster", GetListByEnvIdAsync); } public async Task> GetListAsync(IPmClient pmClient) diff --git a/src/Services/Masa.Dcc.Service/Services/ConfigObjectService.cs b/src/Services/Masa.Dcc.Service/Services/ConfigObjectService.cs index 846e5b6..a8ccfff 100644 --- a/src/Services/Masa.Dcc.Service/Services/ConfigObjectService.cs +++ b/src/Services/Masa.Dcc.Service/Services/ConfigObjectService.cs @@ -3,24 +3,24 @@ namespace Masa.Dcc.Service.Admin.Services; -[Authorize] + public class ConfigObjectService : ServiceBase { public ConfigObjectService() { - RouteOptions.DisableAutoMapRoute = true; - App.MapPost("api/v1/configObject", AddAsync).AddFluentValidationAutoValidation(); - App.MapDelete("api/v1/configObject", RemoveAsync); - App.MapGet("api/v1/configObjects/{envClusterId}/{objectId}/{type}/{getLatestRelease}", GetListAsync); - App.MapGet("api/v1/configObjects/{envClusterId}/{objectId}/{type}/{getLatestRelease}/{configObjectName}", GetListAsync); - App.MapPost("api/v1/configObjects/getListByIds", GetListByIdsAsync); - App.MapPut("api/v1/configObject", UpdateConfigObjectContentAsync).AddFluentValidationAutoValidation(); - App.MapPost("api/v1/configObject/release", AddConfigObjectReleaseAsync).AddFluentValidationAutoValidation(); - App.MapPut("api/v1/configObject/revoke/{id}", RevokeConfigObjectAsync); - App.MapPut("api/v1/configObject/rollback", RollbackAsync); - App.MapPost("api/v1/configObject/clone", CloneConfigObjectAsync); - App.MapGet("api/v1/configObject/release/history/{configObjectId}", GetConfigObjectReleaseHistoryAsync); - App.MapGet("api/v1/configObject/refresh", RefreshConfigObjectToRedisAsync); + + this.MapPost("api/v1/configObject", AddAsync); + this.MapDelete("api/v1/configObject", RemoveAsync); + this.MapGet("api/v1/configObjects/{envClusterId}/{objectId}/{type}/{getLatestRelease}", GetListAsync); + this.MapGet("api/v1/configObjects/{envClusterId}/{objectId}/{type}/{getLatestRelease}/{configObjectName}", GetListAsync); + this.MapPost("api/v1/configObjects/getListByIds", GetListByIdsAsync); + this.MapPut("api/v1/configObject", UpdateConfigObjectContentAsync); + this.MapPost("api/v1/configObject/release", AddConfigObjectReleaseAsync); + this.MapPut("api/v1/configObject/revoke/{id}", RevokeConfigObjectAsync); + this.MapPut("api/v1/configObject/rollback", RollbackAsync); + this.MapPost("api/v1/configObject/clone", CloneConfigObjectAsync); + this.MapGet("api/v1/configObject/release/history/{configObjectId}", GetConfigObjectReleaseHistoryAsync); + this.MapGet("api/v1/configObject/refresh", RefreshConfigObjectToRedisAsync); } public async Task AddAsync(IEventBus eventBus, List dtos) diff --git a/src/Services/Masa.Dcc.Service/Services/EnvironmentService.cs b/src/Services/Masa.Dcc.Service/Services/EnvironmentService.cs index b7a5912..fc1ad02 100644 --- a/src/Services/Masa.Dcc.Service/Services/EnvironmentService.cs +++ b/src/Services/Masa.Dcc.Service/Services/EnvironmentService.cs @@ -3,14 +3,13 @@ namespace Masa.Dcc.Service.Admin.Services; -[Authorize] + public class EnvironmentService : ServiceBase { public EnvironmentService(IPmClient pmClient) { - RouteOptions.DisableAutoMapRoute = true; - App.MapGet("api/v1/env", GetListAsync); - App.MapGet("api/v1/env/{Id}", GetAsync); + this.MapGet("api/v1/env", GetListAsync); + this.MapGet("api/v1/env/{Id}", GetAsync); } public async Task> GetListAsync(IPmClient pmClient) @@ -20,7 +19,7 @@ public async Task> GetListAsync(IPmClient pmClient) return result; } - public async Task GetAsync(IPmClient pmClient,int Id) + public async Task GetAsync(IPmClient pmClient, int Id) { var result = await pmClient.EnvironmentService.GetAsync(Id); diff --git a/src/Services/Masa.Dcc.Service/Services/LabelService.cs b/src/Services/Masa.Dcc.Service/Services/LabelService.cs index 06ee641..0245dc0 100644 --- a/src/Services/Masa.Dcc.Service/Services/LabelService.cs +++ b/src/Services/Masa.Dcc.Service/Services/LabelService.cs @@ -3,17 +3,17 @@ namespace Masa.Dcc.Service.Admin.Services; -[Authorize] + public class LabelService : ServiceBase { public LabelService() { - RouteOptions.DisableAutoMapRoute = true; - App.MapGet("api/v1/labels", GetListAsync); - App.MapGet("api/v1/{typeCode}/labels", GetLabelsByTypeCodeAsync); - App.MapPost("api/v1/labels", AddAsync).AddFluentValidationAutoValidation(); - App.MapPut("api/v1/labels", UpdateAsync).AddFluentValidationAutoValidation(); - App.MapDelete("api/v1/labels/{typeCode}", RemoveAsync); + + this.MapGet("api/v1/labels", GetListAsync); + this.MapGet("api/v1/{typeCode}/labels", GetLabelsByTypeCodeAsync); + this.MapPost("api/v1/labels", AddAsync); + this.MapPut("api/v1/labels", UpdateAsync); + this.MapDelete("api/v1/labels/{typeCode}", RemoveAsync); } public async Task> GetLabelsByTypeCodeAsync(IEventBus eventBus, string typeCode) diff --git a/src/Services/Masa.Dcc.Service/Services/OpenApiService.cs b/src/Services/Masa.Dcc.Service/Services/OpenApiService.cs index aeb76e9..c175c45 100644 --- a/src/Services/Masa.Dcc.Service/Services/OpenApiService.cs +++ b/src/Services/Masa.Dcc.Service/Services/OpenApiService.cs @@ -7,13 +7,13 @@ public class OpenApiService : ServiceBase { public OpenApiService() { - RouteOptions.DisableAutoMapRoute = true; - App.MapPut("open-api/releasing/{environment}/{cluster}/{appId}/{configObject}", UpdateConfigObjectAsync); - App.MapPost("open-api/releasing/{environment}/{cluster}/{appId}/{isEncryption}", AddConfigObjectAsync); - App.MapPost("open-api/releasing/get/{environment}/{cluster}/{appId}", GetConfigObjectsAsync); - App.MapGet("open-api/releasing/{environment}/{cluster}/stack-config", GetStackConfigAsync); - App.MapGet("open-api/releasing/{environment}/{cluster}/i18n/{culture}", GetI18NConfigAsync); - App.MapGet("open-api/oss-token", GetOssSecurityTokenAsync); + + this.MapPut("open-api/releasing/{environment}/{cluster}/{appId}/{configObject}", UpdateConfigObjectAsync); + this.MapPost("open-api/releasing/{environment}/{cluster}/{appId}/{isEncryption}", AddConfigObjectAsync); + this.MapPost("open-api/releasing/get/{environment}/{cluster}/{appId}", GetConfigObjectsAsync); + this.MapGet("open-api/releasing/{environment}/{cluster}/stack-config", GetStackConfigAsync).AllowAnonymous(); + this.MapGet("open-api/releasing/{environment}/{cluster}/i18n/{culture}", GetI18NConfigAsync).AllowAnonymous(); + this.MapGet("open-api/oss-token", GetOssSecurityTokenAsync); } public async Task UpdateConfigObjectAsync(IEventBus eventBus, string environment, string cluster, string appId, string configObject, diff --git a/src/Services/Masa.Dcc.Service/Services/ProjectService.cs b/src/Services/Masa.Dcc.Service/Services/ProjectService.cs index 62cd983..19b98c4 100644 --- a/src/Services/Masa.Dcc.Service/Services/ProjectService.cs +++ b/src/Services/Masa.Dcc.Service/Services/ProjectService.cs @@ -3,18 +3,18 @@ namespace Masa.Dcc.Service.Admin.Services; -[Authorize] + public class ProjectService : ServiceBase { public ProjectService(IPmClient pmClient) { - RouteOptions.DisableAutoMapRoute = true; - App.MapGet("api/v1/projectwithapps/{envName}", GetProjectListAsync); - App.MapGet("api/v1/project/{id}", GetAsync); - App.MapGet("api/v1/{envClusterId}/project", GetListByEnvironmentClusterIdAsync); - App.MapGet("api/v1/project/projectType", GetProjectTypes); - App.MapGet("api/v1/project", GetListAsync); - App.MapPost("api/v1/project/teamsProject", GetListByTeamIdsAsync); + + this.MapGet("api/v1/projectwithapps/{envName}", GetProjectListAsync); + this.MapGet("api/v1/project/{id}", GetAsync); + this.MapGet("api/v1/{envClusterId}/project", GetListByEnvironmentClusterIdAsync); + this.MapGet("api/v1/project/projectType", GetProjectTypes); + this.MapGet("api/v1/project", GetListAsync); + this.MapPost("api/v1/project/teamsProject", GetListByTeamIdsAsync); } public async Task> GetProjectListAsync(IPmClient pmClient, string envName) diff --git a/src/Services/Masa.Dcc.Service/Services/PublicConfigService.cs b/src/Services/Masa.Dcc.Service/Services/PublicConfigService.cs index 3ba5348..f61fa4c 100644 --- a/src/Services/Masa.Dcc.Service/Services/PublicConfigService.cs +++ b/src/Services/Masa.Dcc.Service/Services/PublicConfigService.cs @@ -7,12 +7,12 @@ public class PublicConfigService : ServiceBase { public PublicConfigService() { - RouteOptions.DisableAutoMapRoute = true; - App.MapPost("api/v1/publicConfig", AddAsync); - App.MapPut("api/v1/publicConfig", UpdateAsync); - App.MapDelete("api/v1/publicConfig/{Id}", RemoveAsync); - App.MapGet("api/v1/publicConfig", GetListAsync); - App.MapGet("api/v1/pubConfigObjects", GetByConfigObjectIdAsync); + + this.MapPost("api/v1/publicConfig", AddAsync); + this.MapPut("api/v1/publicConfig", UpdateAsync); + this.MapDelete("api/v1/publicConfig/{Id}", RemoveAsync); + this.MapGet("api/v1/publicConfig", GetListAsync); + this.MapGet("api/v1/pubConfigObjects", GetByConfigObjectIdAsync); } public async Task AddAsync(IEventBus eventBus, AddObjectConfigDto dto)