From 24dd5843e00405ee327edaec5996d7916ba1a895 Mon Sep 17 00:00:00 2001 From: "agile.zhou" Date: Sat, 28 Sep 2024 17:09:30 +0800 Subject: [PATCH] Refactor RegisterCenter API --- .../{ServiceInfoVM.cs => ApiServiceInfoVM.cs} | 4 +- .../api/Models/RegisterServiceInfoVM.cs | 25 +++++ .../api/RegisterCenterController.cs | 92 ++++--------------- .../Models/Mapping/ModelMappingExtension.cs | 34 +++++++ 4 files changed, 78 insertions(+), 77 deletions(-) rename src/AgileConfig.Server.Apisite/Controllers/api/Models/{ServiceInfoVM.cs => ApiServiceInfoVM.cs} (85%) diff --git a/src/AgileConfig.Server.Apisite/Controllers/api/Models/ServiceInfoVM.cs b/src/AgileConfig.Server.Apisite/Controllers/api/Models/ApiServiceInfoVM.cs similarity index 85% rename from src/AgileConfig.Server.Apisite/Controllers/api/Models/ServiceInfoVM.cs rename to src/AgileConfig.Server.Apisite/Controllers/api/Models/ApiServiceInfoVM.cs index f6293028..1dffe4f7 100644 --- a/src/AgileConfig.Server.Apisite/Controllers/api/Models/ServiceInfoVM.cs +++ b/src/AgileConfig.Server.Apisite/Controllers/api/Models/ApiServiceInfoVM.cs @@ -3,7 +3,7 @@ namespace AgileConfig.Server.Apisite.Controllers.api.Models { - public class ServiceInfoVM + public class ApiServiceInfoVM { public string ServiceId { get; set; } = ""; @@ -20,6 +20,6 @@ public class ServiceInfoVM public class QueryServiceInfoResultVM { - public List Data { get; set; } + public List Data { get; set; } } } diff --git a/src/AgileConfig.Server.Apisite/Controllers/api/Models/RegisterServiceInfoVM.cs b/src/AgileConfig.Server.Apisite/Controllers/api/Models/RegisterServiceInfoVM.cs index fc730e7e..3877dc1e 100644 --- a/src/AgileConfig.Server.Apisite/Controllers/api/Models/RegisterServiceInfoVM.cs +++ b/src/AgileConfig.Server.Apisite/Controllers/api/Models/RegisterServiceInfoVM.cs @@ -1,4 +1,6 @@ using System.Collections.Generic; +using AgileConfig.Server.Data.Entity; +using Newtonsoft.Json; namespace AgileConfig.Server.Apisite.Controllers.api.Models { @@ -20,4 +22,27 @@ public class RegisterServiceInfoVM public string HeartBeatMode { get; set; } } + + public static class RegisterServiceInfoVMExtension + { + public static ServiceInfo ToServiceInfo(this RegisterServiceInfoVM model) + { + if (model == null) + { + return null; + } + + return new ServiceInfo + { + ServiceId = model.ServiceId, + ServiceName = model.ServiceName, + Ip = model.Ip, + Port = model.Port, + MetaData = model.MetaData is null ? "[]" : JsonConvert.SerializeObject(model.MetaData), + CheckUrl = model.CheckUrl, + AlarmUrl = model.AlarmUrl, + HeartBeatMode = model.HeartBeatMode + }; + } + } } \ No newline at end of file diff --git a/src/AgileConfig.Server.Apisite/Controllers/api/RegisterCenterController.cs b/src/AgileConfig.Server.Apisite/Controllers/api/RegisterCenterController.cs index faa424a9..a4385b6b 100644 --- a/src/AgileConfig.Server.Apisite/Controllers/api/RegisterCenterController.cs +++ b/src/AgileConfig.Server.Apisite/Controllers/api/RegisterCenterController.cs @@ -6,8 +6,7 @@ using System.Collections.Generic; using System.Threading.Tasks; using Agile.Config.Protocol; -using Microsoft.Extensions.Logging; -using Newtonsoft.Json; +using AgileConfig.Server.Apisite.Models.Mapping; using AgileConfig.Server.Common.EventBus; using AgileConfig.Server.Event; @@ -23,32 +22,23 @@ public class RegisterCenterController : Controller private readonly IRegisterCenterService _registerCenterService; private readonly IServiceInfoService _serviceInfoService; private readonly ITinyEventBus _tinyEventBus; - private readonly ILogger _logger; public RegisterCenterController(IRegisterCenterService registerCenterService , IServiceInfoService serviceInfoService, - ILoggerFactory loggerFactory, ITinyEventBus tinyEventBus ) { _registerCenterService = registerCenterService; _serviceInfoService = serviceInfoService; _tinyEventBus = tinyEventBus; - _logger = loggerFactory.CreateLogger(); } [HttpPost] public async Task Register([FromBody] RegisterServiceInfoVM model) { - var entity = new ServiceInfo(); - entity.ServiceId = model.ServiceId; - entity.ServiceName = model.ServiceName; - entity.Ip = model.Ip; - entity.Port = model.Port; - entity.CheckUrl = model.CheckUrl; - entity.AlarmUrl = model.AlarmUrl; - entity.HeartBeatMode = model.HeartBeatMode; - entity.MetaData = model.MetaData is null ? "[]" : JsonConvert.SerializeObject(model.MetaData); + ArgumentNullException.ThrowIfNull(model); + + var entity = model.ToServiceInfo(); entity.RegisterWay = RegisterWay.Auto; var id = await _registerCenterService.RegisterAsync(entity); @@ -94,10 +84,7 @@ public async Task> UnRegister(string id, [FromBod [HttpPost("heartbeat")] public async Task> Heartbeat([FromBody] HeartbeatParam param) { - if (param == null) - { - throw new ArgumentNullException(nameof(param)); - } + ArgumentNullException.ThrowIfNull(param); bool serviceHeartbeatResult = false; if (!string.IsNullOrEmpty(param.UniqueId)) @@ -120,29 +107,14 @@ public async Task> Heartbeat([FromBody] Heartbea } [HttpGet("services")] - public async Task> AllServices() + public async Task> AllServices() { var services = await _serviceInfoService.GetAllServiceInfoAsync(); - var vms = new List(); + var vms = new List(); foreach (var serviceInfo in services) { - var vm = new ServiceInfoVM - { - ServiceId = serviceInfo.ServiceId, - ServiceName = serviceInfo.ServiceName, - Ip = serviceInfo.Ip, - Port = serviceInfo.Port, - MetaData = new List(), - Status = serviceInfo.Status - }; - try - { - vm.MetaData = JsonConvert.DeserializeObject>(serviceInfo.MetaData); - } - catch (Exception e) - { - _logger.LogError(e, $"deserialize meta data error, serviceId:{serviceInfo.ServiceId}"); - } + var vm = serviceInfo.ToApiServiceInfoVM(); + vms.Add(vm); } @@ -150,29 +122,14 @@ public async Task> AllServices() } [HttpGet("services/online")] - public async Task> OnlineServices() + public async Task> OnlineServices() { var services = await _serviceInfoService.GetOnlineServiceInfoAsync(); - var vms = new List(); + var vms = new List(); foreach (var serviceInfo in services) { - var vm = new ServiceInfoVM - { - ServiceId = serviceInfo.ServiceId, - ServiceName = serviceInfo.ServiceName, - Ip = serviceInfo.Ip, - Port = serviceInfo.Port, - MetaData = new List(), - Status = serviceInfo.Status - }; - try - { - vm.MetaData = JsonConvert.DeserializeObject>(serviceInfo.MetaData); - } - catch (Exception e) - { - _logger.LogError(e, $"deserialize meta data error, serviceId:{serviceInfo.ServiceId}"); - } + var vm = serviceInfo.ToApiServiceInfoVM(); + vms.Add(vm); } @@ -180,29 +137,14 @@ public async Task> OnlineServices() } [HttpGet("services/offline")] - public async Task> OfflineServices() + public async Task> OfflineServices() { var services = await _serviceInfoService.GetOfflineServiceInfoAsync(); - var vms = new List(); + var vms = new List(); foreach (var serviceInfo in services) { - var vm = new ServiceInfoVM - { - ServiceId = serviceInfo.ServiceId, - ServiceName = serviceInfo.ServiceName, - Ip = serviceInfo.Ip, - Port = serviceInfo.Port, - MetaData = new List(), - Status = serviceInfo.Status - }; - try - { - vm.MetaData = JsonConvert.DeserializeObject>(serviceInfo.MetaData); - } - catch (Exception e) - { - _logger.LogError(e, $"deserialize meta data error, serviceId:{serviceInfo.ServiceId}"); - } + var vm = serviceInfo.ToApiServiceInfoVM(); + vms.Add(vm); } diff --git a/src/AgileConfig.Server.Apisite/Models/Mapping/ModelMappingExtension.cs b/src/AgileConfig.Server.Apisite/Models/Mapping/ModelMappingExtension.cs index 3018ae9b..c312e868 100644 --- a/src/AgileConfig.Server.Apisite/Models/Mapping/ModelMappingExtension.cs +++ b/src/AgileConfig.Server.Apisite/Models/Mapping/ModelMappingExtension.cs @@ -1,5 +1,8 @@ using AgileConfig.Server.Data.Entity; using AgileConfig.Server.Apisite.Controllers.api.Models; +using System.Collections.Generic; +using System; +using Newtonsoft.Json; namespace AgileConfig.Server.Apisite.Models.Mapping { @@ -115,4 +118,35 @@ public static ApiNodeVM ToApiNodeVM(this ServerNode node) } } + public static class ServiceInfoExtension + { + public static ApiServiceInfoVM ToApiServiceInfoVM(this ServiceInfo serviceInfo) + { + if (serviceInfo == null) + { + return null; + } + + var vm = new ApiServiceInfoVM + { + ServiceId = serviceInfo.ServiceId, + ServiceName = serviceInfo.ServiceName, + Ip = serviceInfo.Ip, + Port = serviceInfo.Port, + MetaData = new List(), + Status = serviceInfo.Status + }; + + try + { + vm.MetaData = JsonConvert.DeserializeObject>(serviceInfo.MetaData); + } + catch + { + } + + return vm; + } + } + }