Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor RegisterCenter API #192

Merged
merged 1 commit into from
Sep 28, 2024
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
Expand Up @@ -3,7 +3,7 @@

namespace AgileConfig.Server.Apisite.Controllers.api.Models
{
public class ServiceInfoVM
public class ApiServiceInfoVM
{
public string ServiceId { get; set; } = "";

Expand All @@ -20,6 +20,6 @@ public class ServiceInfoVM

public class QueryServiceInfoResultVM
{
public List<ServiceInfoVM> Data { get; set; }
public List<ApiServiceInfoVM> Data { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Generic;
using AgileConfig.Server.Data.Entity;
using Newtonsoft.Json;

namespace AgileConfig.Server.Apisite.Controllers.api.Models
{
Expand All @@ -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
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -23,32 +22,23 @@ public class RegisterCenterController : Controller
private readonly IRegisterCenterService _registerCenterService;
private readonly IServiceInfoService _serviceInfoService;
private readonly ITinyEventBus _tinyEventBus;
private readonly ILogger<RegisterCenterController> _logger;

public RegisterCenterController(IRegisterCenterService registerCenterService
, IServiceInfoService serviceInfoService,
ILoggerFactory loggerFactory,
ITinyEventBus tinyEventBus
)
{
_registerCenterService = registerCenterService;
_serviceInfoService = serviceInfoService;
_tinyEventBus = tinyEventBus;
_logger = loggerFactory.CreateLogger<RegisterCenterController>();
}

[HttpPost]
public async Task<RegisterResultVM> 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);
Expand Down Expand Up @@ -94,10 +84,7 @@ public async Task<ActionResult<RegisterResultVM>> UnRegister(string id, [FromBod
[HttpPost("heartbeat")]
public async Task<ActionResult<HeartbeatResultVM>> Heartbeat([FromBody] HeartbeatParam param)
{
if (param == null)
{
throw new ArgumentNullException(nameof(param));
}
ArgumentNullException.ThrowIfNull(param);

bool serviceHeartbeatResult = false;
if (!string.IsNullOrEmpty(param.UniqueId))
Expand All @@ -120,89 +107,44 @@ public async Task<ActionResult<HeartbeatResultVM>> Heartbeat([FromBody] Heartbea
}

[HttpGet("services")]
public async Task<List<ServiceInfoVM>> AllServices()
public async Task<List<ApiServiceInfoVM>> AllServices()
{
var services = await _serviceInfoService.GetAllServiceInfoAsync();
var vms = new List<ServiceInfoVM>();
var vms = new List<ApiServiceInfoVM>();
foreach (var serviceInfo in services)
{
var vm = new ServiceInfoVM
{
ServiceId = serviceInfo.ServiceId,
ServiceName = serviceInfo.ServiceName,
Ip = serviceInfo.Ip,
Port = serviceInfo.Port,
MetaData = new List<string>(),
Status = serviceInfo.Status
};
try
{
vm.MetaData = JsonConvert.DeserializeObject<List<string>>(serviceInfo.MetaData);
}
catch (Exception e)
{
_logger.LogError(e, $"deserialize meta data error, serviceId:{serviceInfo.ServiceId}");
}
var vm = serviceInfo.ToApiServiceInfoVM();

vms.Add(vm);
}

return vms;
}

[HttpGet("services/online")]
public async Task<List<ServiceInfoVM>> OnlineServices()
public async Task<List<ApiServiceInfoVM>> OnlineServices()
{
var services = await _serviceInfoService.GetOnlineServiceInfoAsync();
var vms = new List<ServiceInfoVM>();
var vms = new List<ApiServiceInfoVM>();
foreach (var serviceInfo in services)
{
var vm = new ServiceInfoVM
{
ServiceId = serviceInfo.ServiceId,
ServiceName = serviceInfo.ServiceName,
Ip = serviceInfo.Ip,
Port = serviceInfo.Port,
MetaData = new List<string>(),
Status = serviceInfo.Status
};
try
{
vm.MetaData = JsonConvert.DeserializeObject<List<string>>(serviceInfo.MetaData);
}
catch (Exception e)
{
_logger.LogError(e, $"deserialize meta data error, serviceId:{serviceInfo.ServiceId}");
}
var vm = serviceInfo.ToApiServiceInfoVM();

vms.Add(vm);
}

return vms;
}

[HttpGet("services/offline")]
public async Task<List<ServiceInfoVM>> OfflineServices()
public async Task<List<ApiServiceInfoVM>> OfflineServices()
{
var services = await _serviceInfoService.GetOfflineServiceInfoAsync();
var vms = new List<ServiceInfoVM>();
var vms = new List<ApiServiceInfoVM>();
foreach (var serviceInfo in services)
{
var vm = new ServiceInfoVM
{
ServiceId = serviceInfo.ServiceId,
ServiceName = serviceInfo.ServiceName,
Ip = serviceInfo.Ip,
Port = serviceInfo.Port,
MetaData = new List<string>(),
Status = serviceInfo.Status
};
try
{
vm.MetaData = JsonConvert.DeserializeObject<List<string>>(serviceInfo.MetaData);
}
catch (Exception e)
{
_logger.LogError(e, $"deserialize meta data error, serviceId:{serviceInfo.ServiceId}");
}
var vm = serviceInfo.ToApiServiceInfoVM();

vms.Add(vm);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -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<string>(),
Status = serviceInfo.Status
};

try
{
vm.MetaData = JsonConvert.DeserializeObject<List<string>>(serviceInfo.MetaData);
}
catch
{
}

return vm;
}
}

}
Loading