Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<IOptions<ServiceGlobalRouteOptions>>().Value;
options.RouteHandlerBuilder?.Invoke(builder);
return builder;
}
}
60 changes: 29 additions & 31 deletions src/Services/Masa.Dcc.Service/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@

builder.Services
.AddValidatorsFromAssemblyContaining<AddConfigObjectDto>()
.AddFluentValidationAutoValidation(configuration => {
.AddFluentValidationAutoValidation(configuration =>
{
configuration.OverrideDefaultResultFactoryWith<CustomResultFactory>();
});

Expand Down Expand Up @@ -167,46 +168,43 @@
.UseRepository<DccDbContext>();
});


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
Expand Down
18 changes: 8 additions & 10 deletions src/Services/Masa.Dcc.Service/Services/AppService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AppDetailModel> GetAsync(IPmClient pmClient, int id)
Expand Down
10 changes: 5 additions & 5 deletions src/Services/Masa.Dcc.Service/Services/BizConfigService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<LatestReleaseConfigModel>> GetLatestReleaseConfigByProjectAsync(IEventBus eventBus,
Expand Down
10 changes: 4 additions & 6 deletions src/Services/Masa.Dcc.Service/Services/ClusterService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<ClusterModel>> GetListAsync(IPmClient pmClient)
Expand Down
28 changes: 14 additions & 14 deletions src/Services/Masa.Dcc.Service/Services/ConfigObjectService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AddConfigObjectDto> dtos)
Expand Down
9 changes: 4 additions & 5 deletions src/Services/Masa.Dcc.Service/Services/EnvironmentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<EnvironmentModel>> GetListAsync(IPmClient pmClient)
Expand All @@ -20,7 +19,7 @@ public async Task<List<EnvironmentModel>> GetListAsync(IPmClient pmClient)
return result;
}

public async Task<EnvironmentDetailModel> GetAsync(IPmClient pmClient,int Id)
public async Task<EnvironmentDetailModel> GetAsync(IPmClient pmClient, int Id)
{
var result = await pmClient.EnvironmentService.GetAsync(Id);

Expand Down
14 changes: 7 additions & 7 deletions src/Services/Masa.Dcc.Service/Services/LabelService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<LabelDto>> GetLabelsByTypeCodeAsync(IEventBus eventBus, string typeCode)
Expand Down
14 changes: 7 additions & 7 deletions src/Services/Masa.Dcc.Service/Services/OpenApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
16 changes: 8 additions & 8 deletions src/Services/Masa.Dcc.Service/Services/ProjectService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<ProjectAppsModel>> GetProjectListAsync(IPmClient pmClient, string envName)
Expand Down
12 changes: 6 additions & 6 deletions src/Services/Masa.Dcc.Service/Services/PublicConfigService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PublicConfigDto> AddAsync(IEventBus eventBus, AddObjectConfigDto dto)
Expand Down