using Fleet.Core;
using Fleet.Rpc;
using Fleet.Server;
using Microsoft.Extensions.DependencyInjection;
using MoShiro.Backend.Application;
using MoShiro.Protocol;
namespace MoShiro.Backend.Gateway;
public static class GatewayServiceCollectionExtensions
{
///
/// 在 Fleet.Server 之上接入 MoShiro:期望状态(TaskSet、DialTargets)、RawChunk 收件箱、moshiro.* 方法、注册挂钩和节点事件。
/// 需要事先注册 和 ,并在 AddMoShiroApplication 之后调用。
///
public static IServiceCollection AddMoShiroGateway(this IServiceCollection services)
{
services.AddSingleton();
services.AddSingleton();
services.AddHostedService();
services.AddFleetEnrollment();
services.AddFleetServer(options => options.NodeIdPrefix = AgentIds.Prefix)
.AddState()
.AddState()
.AddInbox()
.WithMethods(MoShiroMethodTable.Table)
.MapMethods(MapMoShiroMethods);
return services;
}
private static void MapMoShiroMethods(RpcRouter router, IServiceProvider services)
{
router
.OnNotify(
MoShiroMethods.Event,
MoShiroJsonContext.Default.AgentEventParams,
(context, parameters, ct) => services.GetRequiredService().OnEventAsync(context.Node.Id, parameters, ct))
.OnNotify(
MoShiroMethods.Host,
MoShiroJsonContext.Default.HostSnapshot,
(context, snapshot, ct) => services.GetRequiredService().OnHostAsync(context.Node.Id, snapshot, ct))
.OnNotify(
MoShiroMethods.TestStatus,
MoShiroJsonContext.Default.TestStatusParams,
(context, parameters, ct) => services.GetRequiredService().OnStatusAsync(context.Node.Id, parameters, ct));
}
}
/// 握手时的版本要求:告诉 Agent 面板的版本,并要求 Agent 不低于配置的最低版本。
internal sealed class BackendVersionPolicy(BackendOptions options) : IFleetVersionPolicy
{
public string? ServerVersion => BuildInfo.Version;
public string? MinNodeVersion => string.IsNullOrWhiteSpace(options.MinAgentVersion) ? null : options.MinAgentVersion;
}