From 1236ca6f81e776e09846dbebaa02ef52f15fc875 Mon Sep 17 00:00:00 2001 From: "agile.zhou" Date: Sat, 28 Sep 2024 18:14:58 +0800 Subject: [PATCH 1/3] Refactor Config API --- .../Controllers/api/ConfigController.cs | 150 +++++------------- .../Controllers/api/Models/ApiConfigVM.cs | 21 +++ .../Models/Mapping/ModelMappingExtension.cs | 25 +++ 3 files changed, 82 insertions(+), 114 deletions(-) diff --git a/src/AgileConfig.Server.Apisite/Controllers/api/ConfigController.cs b/src/AgileConfig.Server.Apisite/Controllers/api/ConfigController.cs index 1c4aea3d..3f24a566 100644 --- a/src/AgileConfig.Server.Apisite/Controllers/api/ConfigController.cs +++ b/src/AgileConfig.Server.Apisite/Controllers/api/ConfigController.cs @@ -6,6 +6,7 @@ using AgileConfig.Server.Apisite.Filters; using AgileConfig.Server.Apisite.Metrics; using AgileConfig.Server.Apisite.Models; +using AgileConfig.Server.Apisite.Models.Mapping; using AgileConfig.Server.Common.EventBus; using AgileConfig.Server.Data.Entity; using AgileConfig.Server.IService; @@ -21,26 +22,23 @@ public class ConfigController : Controller { private readonly IConfigService _configService; private readonly IAppService _appService; - private readonly IUserService _userService; private readonly IMemoryCache _cacheMemory; - private readonly ITinyEventBus _tinyEventBus; private readonly IMeterService _meterService; + private readonly Controllers.ConfigController _configController; public ConfigController( IConfigService configService, IAppService appService, - IUserService userService, IMemoryCache cacheMemory, - ITinyEventBus tinyEventBus, - IMeterService meterService + IMeterService meterService, + Controllers.ConfigController configController ) { _configService = configService; _appService = appService; - _userService = userService; _cacheMemory = cacheMemory; - _tinyEventBus = tinyEventBus; _meterService = meterService; + _configController = configController; } /// @@ -52,11 +50,9 @@ IMeterService meterService /// [TypeFilter(typeof(AppBasicAuthenticationAttribute))] [HttpGet("app/{appId}")] - public async Task>> GetAppConfig(string appId, [FromQuery] string env) + public async Task>> GetAppConfig(string appId, [FromQuery] EnvString env) { - ArgumentNullException.ThrowIfNullOrEmpty(appId); - - ISettingService.IfEnvEmptySetDefault(ref env); + ArgumentException.ThrowIfNullOrEmpty(appId); var app = await _appService.GetAsync(appId); if (!app.Enabled) @@ -64,7 +60,7 @@ public async Task>> GetAppConfig(string appId, [F return NotFound(); } - var cacheKey = $"ConfigController_APPCONFIG_{appId}_{env}"; + var cacheKey = $"ConfigController_AppConfig_{appId}_{env.Value}"; List configs = null; _cacheMemory?.TryGetValue(cacheKey, out configs); if (configs != null) @@ -72,21 +68,8 @@ public async Task>> GetAppConfig(string appId, [F return configs; } - var appConfigs = await _configService.GetPublishedConfigsByAppIdWithInheritanced(appId, env); - var vms = appConfigs.Select(c => - { - return new ApiConfigVM() - { - Id = c.Id, - AppId = c.AppId, - Group = c.Group, - Key = c.Key, - Value = c.Value, - Status = c.Status, - OnlineStatus = c.OnlineStatus, - EditStatus = c.EditStatus - }; - }).ToList(); + var appConfigs = await _configService.GetPublishedConfigsByAppIdWithInheritanced(appId, env.Value); + var vms = appConfigs.Select(x => x.ToApiConfigVM()).ToList(); //增加5s的缓存,防止同一个app同时启动造成db的压力过大 var cacheOp = new MemoryCacheEntryOptions() @@ -106,26 +89,13 @@ public async Task>> GetAppConfig(string appId, [F /// [TypeFilter(typeof(AdmBasicAuthenticationAttribute))] [HttpGet()] - public async Task>> GetConfigs(string appId, string env) + public async Task>> GetConfigs(string appId, [FromQuery] EnvString env) { - ArgumentNullException.ThrowIfNullOrEmpty(appId); - - ISettingService.IfEnvEmptySetDefault(ref env); + ArgumentException.ThrowIfNullOrEmpty(appId); - var configs = await _configService.GetByAppIdAsync(appId, env); + var configs = await _configService.GetByAppIdAsync(appId, env.Value); - return configs.Select(config => new ApiConfigVM() - { - Id = config.Id, - AppId = config.AppId, - Group = config.Group, - Key = config.Key, - Value = config.Value, - Status = config.Status, - Description = config.Description, - OnlineStatus = config.OnlineStatus, - EditStatus = config.EditStatus - }).ToList(); + return configs.Select(x => x.ToApiConfigVM()).ToList(); } /// @@ -136,30 +106,17 @@ public async Task>> GetConfigs(string appId, stri /// [TypeFilter(typeof(AdmBasicAuthenticationAttribute))] [HttpGet("{id}")] - public async Task> GetConfig(string id, string env) + public async Task> GetConfig(string id, [FromQuery] EnvString env) { - ArgumentNullException.ThrowIfNullOrEmpty(id); - - ISettingService.IfEnvEmptySetDefault(ref env); + ArgumentException.ThrowIfNullOrEmpty(id); - var config = await _configService.GetAsync(id, env); + var config = await _configService.GetAsync(id, env.Value); if (config == null || config.Status == ConfigStatus.Deleted) { return NotFound(); } - return new ApiConfigVM() - { - Id = config.Id, - AppId = config.AppId, - Group = config.Group, - Key = config.Key, - Value = config.Value, - Status = config.Status, - Description = config.Description, - OnlineStatus = config.OnlineStatus, - EditStatus = config.EditStatus - }; + return config.ToApiConfigVM(); } /// @@ -185,27 +142,13 @@ public async Task Add([FromBody] ApiConfigVM model, EnvString env }); } - var ctrl = new Controllers.ConfigController( - _configService, - _appService, - _userService, - _tinyEventBus - ); - ctrl.ControllerContext.HttpContext = HttpContext; + _configController.ControllerContext.HttpContext = HttpContext; - var result = (await ctrl.Add(new ConfigVM() - { - Id = model.Id, - AppId = model.AppId, - Group = model.Group, - Key = model.Key, - Value = model.Value, - Description = model.Description - }, env)) as JsonResult; + var result = (await _configController.Add(model.ToConfigVM(), env)) as JsonResult; - dynamic obj = result.Value; + dynamic obj = result?.Value; - if (obj.success == true) + if (obj?.success == true) { return Created("/api/config/" + obj.data.Id, ""); } @@ -213,7 +156,7 @@ public async Task Add([FromBody] ApiConfigVM model, EnvString env Response.StatusCode = 400; return Json(new { - obj.message + obj?.message }); } @@ -240,27 +183,12 @@ public async Task Edit(string id, [FromBody] ApiConfigVM model, E }); } - var ctrl = new Controllers.ConfigController( - _configService, - _appService, - _userService, - _tinyEventBus - ); - ctrl.ControllerContext.HttpContext = HttpContext; - + _configController.ControllerContext.HttpContext = HttpContext; model.Id = id; - var result = (await ctrl.Edit(new ConfigVM() - { - Id = model.Id, - AppId = model.AppId, - Group = model.Group, - Key = model.Key, - Value = model.Value, - Description = model.Description - }, env)) as JsonResult; - - dynamic obj = result.Value; - if (obj.success == true) + var result = (await _configController.Edit(model.ToConfigVM(), env)) as JsonResult; + + dynamic obj = result?.Value; + if (obj?.success == true) { return Ok(); } @@ -268,7 +196,7 @@ public async Task Edit(string id, [FromBody] ApiConfigVM model, E Response.StatusCode = 400; return Json(new { - obj.message + obj?.message }); } @@ -284,18 +212,12 @@ public async Task Edit(string id, [FromBody] ApiConfigVM model, E [HttpDelete("{id}")] public async Task Delete(string id, EnvString env) { - var ctrl = new Controllers.ConfigController( - _configService, - _appService, - _userService, - _tinyEventBus - ); - ctrl.ControllerContext.HttpContext = HttpContext; - - var result = (await ctrl.Delete(id, env)) as JsonResult; - - dynamic obj = result.Value; - if (obj.success == true) + _configController.ControllerContext.HttpContext = HttpContext; + + var result = (await _configController.Delete(id, env)) as JsonResult; + + dynamic obj = result?.Value; + if (obj?.success == true) { return NoContent(); } @@ -303,7 +225,7 @@ public async Task Delete(string id, EnvString env) Response.StatusCode = 400; return Json(new { - obj.message + obj?.message }); } diff --git a/src/AgileConfig.Server.Apisite/Controllers/api/Models/ApiConfigVM.cs b/src/AgileConfig.Server.Apisite/Controllers/api/Models/ApiConfigVM.cs index 586f92c8..ee21cc94 100644 --- a/src/AgileConfig.Server.Apisite/Controllers/api/Models/ApiConfigVM.cs +++ b/src/AgileConfig.Server.Apisite/Controllers/api/Models/ApiConfigVM.cs @@ -52,4 +52,25 @@ public class ApiConfigVM : IAppIdModel /// public string Description { get; set; } } + + public static class ApiConfigVMExtension + { + public static ConfigVM ToConfigVM(this ApiConfigVM model) + { + if (model is null) + { + return null; + } + + return new ConfigVM() + { + Id = model.Id, + AppId = model.AppId, + Group = model.Group, + Key = model.Key, + Value = model.Value, + Description = model.Description + }; + } + } } diff --git a/src/AgileConfig.Server.Apisite/Models/Mapping/ModelMappingExtension.cs b/src/AgileConfig.Server.Apisite/Models/Mapping/ModelMappingExtension.cs index c312e868..506f4c5a 100644 --- a/src/AgileConfig.Server.Apisite/Models/Mapping/ModelMappingExtension.cs +++ b/src/AgileConfig.Server.Apisite/Models/Mapping/ModelMappingExtension.cs @@ -149,4 +149,29 @@ public static ApiServiceInfoVM ToApiServiceInfoVM(this ServiceInfo serviceInfo) } } + public static class ConfigExtension + { + public static ApiConfigVM ToApiConfigVM(this Config config) + { + if (config == null) + { + return null; + } + + var vm = new ApiConfigVM() + { + Id = config.Id, + AppId = config.AppId, + Group = config.Group, + Key = config.Key, + Value = config.Value, + Status = config.Status, + OnlineStatus = config.OnlineStatus, + EditStatus = config.EditStatus + }; + + return vm; + } + } + } From 54e00f7143dfba5eaff0aa664bb33c36e630548b Mon Sep 17 00:00:00 2001 From: "agile.zhou" Date: Sat, 28 Sep 2024 22:37:38 +0800 Subject: [PATCH 2/3] Fix build error --- .../Controllers/api/ConfigController.cs | 1 - test/ApiSiteTests/TestApiConfigController.cs | 21 ++++++++----------- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/src/AgileConfig.Server.Apisite/Controllers/api/ConfigController.cs b/src/AgileConfig.Server.Apisite/Controllers/api/ConfigController.cs index 3f24a566..67efada1 100644 --- a/src/AgileConfig.Server.Apisite/Controllers/api/ConfigController.cs +++ b/src/AgileConfig.Server.Apisite/Controllers/api/ConfigController.cs @@ -7,7 +7,6 @@ using AgileConfig.Server.Apisite.Metrics; using AgileConfig.Server.Apisite.Models; using AgileConfig.Server.Apisite.Models.Mapping; -using AgileConfig.Server.Common.EventBus; using AgileConfig.Server.Data.Entity; using AgileConfig.Server.IService; using Microsoft.AspNetCore.Mvc; diff --git a/test/ApiSiteTests/TestApiConfigController.cs b/test/ApiSiteTests/TestApiConfigController.cs index 22593b05..23ffc691 100644 --- a/test/ApiSiteTests/TestApiConfigController.cs +++ b/test/ApiSiteTests/TestApiConfigController.cs @@ -10,6 +10,7 @@ using AgileConfig.Server.Apisite.Controllers.api.Models; using AgileConfig.Server.Common.EventBus; using AgileConfig.Server.Apisite.Metrics; +using AgileConfig.Server.Apisite.Models; namespace ApiSiteTests; @@ -59,10 +60,6 @@ List newConfigs() .ReturnsAsync(newConfigs); IMemoryCache memoryCache = null; - var remoteNodeProxy = new Mock(); - var serverNodeService = new Mock(); - var sysLogService = new Mock(); - var appBasicAuthService = new Mock(); var userSErvice = new Mock(); var eventBus = new Mock(); var meterService = new Mock(); @@ -70,11 +67,11 @@ List newConfigs() var ctrl = new ConfigController( configService.Object, appService.Object, - userSErvice.Object, - memoryCache, eventBus.Object, - meterService.Object + memoryCache, + meterService.Object, + new AgileConfig.Server.Apisite.Controllers.ConfigController(configService.Object, appService.Object, userSErvice.Object, eventBus.Object) ); - var act = await ctrl.GetAppConfig("001", "DEV"); + var act = await ctrl.GetAppConfig("001", new EnvString() { Value = "DEV" }); Assert.IsNotNull(act); Assert.IsNotNull(act.Value); @@ -94,11 +91,11 @@ App newApp1() ctrl = new ConfigController( configService.Object, appService.Object, - userSErvice.Object, - memoryCache, eventBus.Object, - meterService.Object + memoryCache, + meterService.Object, + new AgileConfig.Server.Apisite.Controllers.ConfigController(configService.Object, appService.Object, userSErvice.Object, eventBus.Object) ); - act = await ctrl.GetAppConfig("001", "DEV"); + act = await ctrl.GetAppConfig("001", new EnvString() { Value = "DEV" }); Assert.IsNotNull(act); Assert.IsNull(act.Value); From 755229f2476ad03e4184e744731ab22f27a55799 Mon Sep 17 00:00:00 2001 From: "agile.zhou" Date: Sun, 29 Sep 2024 01:15:03 +0800 Subject: [PATCH 3/3] Fix issue --- .../Controllers/AppController.cs | 25 ++++--- .../Controllers/api/AppController.cs | 70 +++++++++++-------- .../Controllers/api/Models/ApiAppVM.cs | 3 +- .../AppBasicAuthenticationAttribute.cs | 7 +- src/AgileConfig.Server.Apisite/Startup.cs | 2 +- 5 files changed, 61 insertions(+), 46 deletions(-) diff --git a/src/AgileConfig.Server.Apisite/Controllers/AppController.cs b/src/AgileConfig.Server.Apisite/Controllers/AppController.cs index a4a1a881..3c297a34 100644 --- a/src/AgileConfig.Server.Apisite/Controllers/AppController.cs +++ b/src/AgileConfig.Server.Apisite/Controllers/AppController.cs @@ -333,7 +333,7 @@ public async Task All() [HttpGet] public async Task Get(string id) { - ArgumentNullException.ThrowIfNullOrEmpty(id); + ArgumentException.ThrowIfNullOrEmpty(id); var app = await _appService.GetAsync(id); var vm = app.ToAppVM(); @@ -342,12 +342,19 @@ public async Task Get(string id) { vm.inheritancedApps = (await _appService.GetInheritancedAppsAsync(id)).Select(x => x.Id).ToList(); } + else + { + return NotFound(new + { + success = false, + message = "未找到对应的应用程序。" + }); + } return Json(new { - success = app != null, - data = vm, - message = app == null ? "未找到对应的应用程序。" : "" + success = true, + data = vm }); } @@ -360,12 +367,12 @@ public async Task Get(string id) [HttpPost] public async Task DisableOrEanble(string id) { - ArgumentNullException.ThrowIfNullOrEmpty(id); + ArgumentException.ThrowIfNullOrEmpty(id); var app = await _appService.GetAsync(id); if (app == null) { - return Json(new + return NotFound(new { success = false, message = "未找到对应的应用程序。" @@ -392,12 +399,12 @@ public async Task DisableOrEanble(string id) [HttpPost] public async Task Delete(string id) { - ArgumentNullException.ThrowIfNullOrEmpty(id); + ArgumentException.ThrowIfNullOrEmpty(id); var app = await _appService.GetAsync(id); if (app == null) { - return Json(new + return NotFound(new { success = false, message = "未找到对应的应用程序。" @@ -472,7 +479,7 @@ public async Task SaveAppAuth([FromBody] AppAuthVM model) [HttpGet] public async Task GetUserAppAuth(string appId) { - ArgumentNullException.ThrowIfNullOrEmpty(appId); + ArgumentException.ThrowIfNullOrEmpty(appId); var result = new AppAuthVM { diff --git a/src/AgileConfig.Server.Apisite/Controllers/api/AppController.cs b/src/AgileConfig.Server.Apisite/Controllers/api/AppController.cs index 7f4de83f..f86023a0 100644 --- a/src/AgileConfig.Server.Apisite/Controllers/api/AppController.cs +++ b/src/AgileConfig.Server.Apisite/Controllers/api/AppController.cs @@ -8,6 +8,7 @@ using System.Linq; using System.Threading.Tasks; using AgileConfig.Server.Apisite.Models.Mapping; +using Microsoft.AspNetCore.Mvc.Infrastructure; namespace AgileConfig.Server.Apisite.Controllers.api { @@ -57,19 +58,22 @@ public async Task>> GetAll() [HttpGet("{id}")] public async Task> GetById(string id) { - var result = (await _appController.Get(id)) as JsonResult; - dynamic obj = result.Value; + var actionResult = await _appController.Get(id); + var status = actionResult as IStatusCodeActionResult; - if (obj.success) + var result = actionResult as JsonResult; + dynamic obj = result?.Value; + + if (obj?.success ?? false) { AppVM appVM = obj.data; return Json(appVM.ToApiAppVM()); } - Response.StatusCode = 400; + Response.StatusCode = status.StatusCode.Value; return Json(new { - obj.message + obj?.message }); } @@ -98,9 +102,9 @@ public async Task Add([FromBody] ApiAppVM model) var result = (await _appController.Add(model.ToAppVM())) as JsonResult; - dynamic obj = result.Value; + dynamic obj = result?.Value; - if (obj.success == true) + if (obj?.success == true) { return Created("/api/app/" + obj.data.Id, ""); } @@ -108,7 +112,7 @@ public async Task Add([FromBody] ApiAppVM model) Response.StatusCode = 400; return Json(new { - obj.message + obj?.message }); } @@ -137,18 +141,20 @@ public async Task Edit(string id, [FromBody] ApiAppVM model) _appController.ControllerContext.HttpContext = HttpContext; model.Id = id; - var result = (await _appController.Edit(model.ToAppVM())) as JsonResult; + var actionResult = await _appController.Edit(model.ToAppVM()); + var status = actionResult as IStatusCodeActionResult; + var result = actionResult as JsonResult; - dynamic obj = result.Value; - if (obj.success == true) + dynamic obj = result?.Value; + if (obj?.success ?? false) { return Ok(); } - Response.StatusCode = 400; + Response.StatusCode = status.StatusCode.Value; return Json(new { - obj.message + obj?.message }); } @@ -164,18 +170,20 @@ public async Task Delete(string id) { _appController.ControllerContext.HttpContext = HttpContext; - var result = (await _appController.Delete(id)) as JsonResult; + var actionResult = await _appController.Delete(id); + var status = actionResult as IStatusCodeActionResult; + var result = actionResult as JsonResult; - dynamic obj = result.Value; - if (obj.success == true) + dynamic obj = result?.Value; + if (obj?.success ?? false) { return NoContent(); } - Response.StatusCode = 400; + Response.StatusCode = status.StatusCode.Value; return Json(new { - obj.message + obj?.message }); } @@ -192,21 +200,23 @@ public async Task Publish(string appId, EnvString env) { _configController.ControllerContext.HttpContext = HttpContext; - var result = (await _configController.Publish(new PublishLogVM() + var actionResult = await _configController.Publish(new PublishLogVM() { AppId = appId - }, env)) as JsonResult; + }, env); + var status = actionResult as IStatusCodeActionResult; + var result = actionResult as JsonResult; - dynamic obj = result.Value; - if (obj.success == true) + dynamic obj = result?.Value; + if (obj?.success ?? false) { return Ok(); } - Response.StatusCode = 400; + Response.StatusCode = status.StatusCode.Value; return Json(new { - obj.message + obj?.message }); } @@ -244,18 +254,20 @@ public async Task Rollback(string historyId, EnvString env) { _configController.ControllerContext.HttpContext = HttpContext; - var result = (await _configController.Rollback(historyId, env)) as JsonResult; + var actionResult = await _configController.Rollback(historyId, env); + var status = actionResult as IStatusCodeActionResult; + var result = actionResult as JsonResult; - dynamic obj = result.Value; - if (obj.success == true) + dynamic obj = result?.Value; + if (obj?.success ?? false) { return Ok(); } - Response.StatusCode = 400; + Response.StatusCode = status.StatusCode.Value; return Json(new { - obj.message + obj?.message }); } diff --git a/src/AgileConfig.Server.Apisite/Controllers/api/Models/ApiAppVM.cs b/src/AgileConfig.Server.Apisite/Controllers/api/Models/ApiAppVM.cs index 9ddb5c61..0226bf01 100644 --- a/src/AgileConfig.Server.Apisite/Controllers/api/Models/ApiAppVM.cs +++ b/src/AgileConfig.Server.Apisite/Controllers/api/Models/ApiAppVM.cs @@ -60,7 +60,8 @@ public static AppVM ToAppVM(this ApiAppVM vm) Secret = vm.Secret, AppAdmin = vm.AppAdmin, Inheritanced = vm.Inheritanced, - Group = vm.Group + Group = vm.Group, + Enabled = vm.Enabled.GetValueOrDefault() }; } } diff --git a/src/AgileConfig.Server.Apisite/Filters/AppBasicAuthenticationAttribute.cs b/src/AgileConfig.Server.Apisite/Filters/AppBasicAuthenticationAttribute.cs index e1e8e8e6..9639d0d7 100644 --- a/src/AgileConfig.Server.Apisite/Filters/AppBasicAuthenticationAttribute.cs +++ b/src/AgileConfig.Server.Apisite/Filters/AppBasicAuthenticationAttribute.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net; -using System.Text; -using System.Threading.Tasks; +using System.Threading.Tasks; using AgileConfig.Server.IService; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; diff --git a/src/AgileConfig.Server.Apisite/Startup.cs b/src/AgileConfig.Server.Apisite/Startup.cs index 72691d0d..1b504f05 100644 --- a/src/AgileConfig.Server.Apisite/Startup.cs +++ b/src/AgileConfig.Server.Apisite/Startup.cs @@ -67,7 +67,7 @@ public void ConfigureServices(IServiceCollection services) services.AddMemoryCache(); services.AddCors(); - services.AddMvc().AddRazorRuntimeCompilation(); + services.AddMvc().AddRazorRuntimeCompilation().AddControllersAsServices(); if (Appsettings.IsPreviewMode) {