mirror of
https://github.com/lorsanstand/HomeOps-Hub.git
synced 2026-06-19 16:45:15 +03:00
refactor: move domain structure and mapper
This commit is contained in:
@@ -4,7 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
|
|
||||||
pb "github.com/lorsanstand/HomeOps-Hub/api/gen/homeops"
|
pb "github.com/lorsanstand/HomeOps-Hub/api/gen/homeops"
|
||||||
"github.com/lorsanstand/HomeOps-Hub/internal/agent/domain"
|
"github.com/lorsanstand/HomeOps-Hub/internal/domain"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
)
|
)
|
||||||
@@ -32,8 +32,8 @@ func (c *Connection) Hub() pb.HubClient {
|
|||||||
return c.hub
|
return c.hub
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Connection) RegisterAgent(ctx context.Context, RegisterData domain.RegisterAgentData) (domain.RegisterAgentDataResponse, error) {
|
func (c *Connection) RegisterAgent(ctx context.Context, RegisterData domain.RegisterAgentRequest) (domain.RegisterAgentResponse, error) {
|
||||||
ResponseData, err := c.Hub().RegisterAgent(ctx, new(toAgentRegisterRequest(RegisterData)))
|
ResponseData, err := c.Hub().RegisterAgent(ctx, new(domain.ToGRPCAgentRequest(RegisterData)))
|
||||||
c.log.Info().Msg("register agent")
|
c.log.Info().Msg("register agent")
|
||||||
return toAgentRegisterDataResponse(ResponseData), err
|
return domain.ToDomainAgentResponse(ResponseData), err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,44 +0,0 @@
|
|||||||
package rpc
|
|
||||||
|
|
||||||
import (
|
|
||||||
pb "github.com/lorsanstand/HomeOps-Hub/api/gen/homeops"
|
|
||||||
"github.com/lorsanstand/HomeOps-Hub/internal/agent/domain"
|
|
||||||
)
|
|
||||||
|
|
||||||
func toAgentRegisterRequest(request domain.RegisterAgentData) pb.RegisterAgentRequest {
|
|
||||||
return pb.RegisterAgentRequest{
|
|
||||||
AgentId: request.AgentId,
|
|
||||||
AgentName: request.AgentName,
|
|
||||||
Host: &pb.HostInfo{
|
|
||||||
Hostname: request.Host.Hostname,
|
|
||||||
Arch: request.Host.Arch,
|
|
||||||
System: request.Host.System,
|
|
||||||
},
|
|
||||||
Version: request.AgentVersion,
|
|
||||||
Capability: toGRPCCapability(request.Capabilities),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func toGRPCCapability(caps []domain.Capability) []*pb.Capability {
|
|
||||||
var capability []*pb.Capability
|
|
||||||
for _, capi := range caps {
|
|
||||||
capability = append(capability, &pb.Capability{
|
|
||||||
Name: capi.Name,
|
|
||||||
Available: capi.Available,
|
|
||||||
Version: capi.Version,
|
|
||||||
Reason: capi.Reason,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
return capability
|
|
||||||
}
|
|
||||||
|
|
||||||
func toAgentRegisterDataResponse(response *pb.RegisterAgentResponse) domain.RegisterAgentDataResponse {
|
|
||||||
if response == nil {
|
|
||||||
return domain.RegisterAgentDataResponse{}
|
|
||||||
}
|
|
||||||
|
|
||||||
return domain.RegisterAgentDataResponse{
|
|
||||||
AgentID: response.AgentId,
|
|
||||||
Heartbeat: int(response.HeartbeatIntervalSecond),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -4,9 +4,9 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/lorsanstand/HomeOps-Hub/internal/agent/domain"
|
|
||||||
"github.com/lorsanstand/HomeOps-Hub/internal/agent/utils/config_yaml"
|
"github.com/lorsanstand/HomeOps-Hub/internal/agent/utils/config_yaml"
|
||||||
"github.com/lorsanstand/HomeOps-Hub/internal/agent/utils/settings"
|
"github.com/lorsanstand/HomeOps-Hub/internal/agent/utils/settings"
|
||||||
|
"github.com/lorsanstand/HomeOps-Hub/internal/domain"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -15,7 +15,7 @@ type Collector interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type HubConnection interface {
|
type HubConnection interface {
|
||||||
RegisterAgent(ctx context.Context, RegisterData domain.RegisterAgentData) (domain.RegisterAgentDataResponse, error)
|
RegisterAgent(ctx context.Context, RegisterData domain.RegisterAgentRequest) (domain.RegisterAgentResponse, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type AgentService struct {
|
type AgentService struct {
|
||||||
@@ -43,7 +43,7 @@ func (a *AgentService) RegisterAgentConn(ctx context.Context) {
|
|||||||
info, caps := a.collect.GatherInfoSystem()
|
info, caps := a.collect.GatherInfoSystem()
|
||||||
AgentID := a.settings.AgentID
|
AgentID := a.settings.AgentID
|
||||||
AgentName := a.cfg.AppName
|
AgentName := a.cfg.AppName
|
||||||
AgentData := domain.RegisterAgentData{AgentId: AgentID, AgentName: AgentName, Host: info, Capabilities: caps}
|
AgentData := domain.RegisterAgentRequest{AgentId: AgentID, AgentName: AgentName, Host: info, Capabilities: caps}
|
||||||
|
|
||||||
data, err := a.conn.RegisterAgent(ctx, AgentData)
|
data, err := a.conn.RegisterAgent(ctx, AgentData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
|
||||||
"github.com/lorsanstand/HomeOps-Hub/internal/agent/domain"
|
"github.com/lorsanstand/HomeOps-Hub/internal/domain"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package docker_service
|
package docker_service
|
||||||
|
|
||||||
import "github.com/lorsanstand/HomeOps-Hub/internal/agent/domain"
|
import (
|
||||||
|
"github.com/lorsanstand/HomeOps-Hub/internal/domain"
|
||||||
|
)
|
||||||
|
|
||||||
type BadDocker struct {
|
type BadDocker struct {
|
||||||
reason string
|
reason string
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import (
|
|||||||
|
|
||||||
"github.com/docker/docker/api/types"
|
"github.com/docker/docker/api/types"
|
||||||
"github.com/docker/docker/api/types/container"
|
"github.com/docker/docker/api/types/container"
|
||||||
"github.com/lorsanstand/HomeOps-Hub/internal/agent/domain"
|
"github.com/lorsanstand/HomeOps-Hub/internal/domain"
|
||||||
|
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -0,0 +1,79 @@
|
|||||||
|
package domain
|
||||||
|
|
||||||
|
import (
|
||||||
|
pb "github.com/lorsanstand/HomeOps-Hub/api/gen/homeops"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ToDomainAgentRequest(request *pb.RegisterAgentRequest) RegisterAgentData {
|
||||||
|
if request == nil {
|
||||||
|
return RegisterAgentData{}
|
||||||
|
}
|
||||||
|
|
||||||
|
return RegisterAgentData{
|
||||||
|
AgentId: request.AgentId,
|
||||||
|
AgentName: request.AgentName,
|
||||||
|
Host: HostInfo{
|
||||||
|
System: request.Host.System,
|
||||||
|
Hostname: request.Host.Hostname,
|
||||||
|
Arch: request.Host.Arch,
|
||||||
|
},
|
||||||
|
Capabilities: ToDomainCapabilities(request.Capability),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ToDomainAgentResponse(response *pb.RegisterAgentResponse) RegisterAgentDataResponse {
|
||||||
|
if response == nil {
|
||||||
|
return RegisterAgentDataResponse{}
|
||||||
|
}
|
||||||
|
|
||||||
|
return RegisterAgentDataResponse{
|
||||||
|
AgentID: response.AgentId,
|
||||||
|
Heartbeat: int(response.HeartbeatIntervalSecond),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ToDomainCapabilities(capability []*pb.Capability) []Capability {
|
||||||
|
var caps []Capability
|
||||||
|
|
||||||
|
for _, capa := range capability {
|
||||||
|
if capa == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
caps = append(caps, Capability{
|
||||||
|
Name: capa.Name,
|
||||||
|
Version: capa.Version,
|
||||||
|
Reason: capa.Reason,
|
||||||
|
Available: capa.Available,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return caps
|
||||||
|
}
|
||||||
|
|
||||||
|
func ToGRPCAgentRequest(request RegisterAgentData) pb.RegisterAgentRequest {
|
||||||
|
return pb.RegisterAgentRequest{
|
||||||
|
AgentId: request.AgentId,
|
||||||
|
AgentName: request.AgentName,
|
||||||
|
Host: &pb.HostInfo{
|
||||||
|
Hostname: request.Host.Hostname,
|
||||||
|
Arch: request.Host.Arch,
|
||||||
|
System: request.Host.System,
|
||||||
|
},
|
||||||
|
Version: request.AgentVersion,
|
||||||
|
Capability: ToGRPCCapability(request.Capabilities),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ToGRPCCapability(caps []Capability) []*pb.Capability {
|
||||||
|
var capability []*pb.Capability
|
||||||
|
for _, capi := range caps {
|
||||||
|
capability = append(capability, &pb.Capability{
|
||||||
|
Name: capi.Name,
|
||||||
|
Available: capi.Available,
|
||||||
|
Version: capi.Version,
|
||||||
|
Reason: capi.Reason,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return capability
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
package domain
|
package domain
|
||||||
|
|
||||||
type RegisterAgentData struct {
|
type RegisterAgentRequest struct {
|
||||||
AgentId string
|
AgentId string
|
||||||
AgentName string
|
AgentName string
|
||||||
AgentVersion string
|
AgentVersion string
|
||||||
@@ -21,7 +21,7 @@ type Capability struct {
|
|||||||
Reason string
|
Reason string
|
||||||
}
|
}
|
||||||
|
|
||||||
type RegisterAgentDataResponse struct {
|
type RegisterAgentResponse struct {
|
||||||
Heartbeat int
|
Heartbeat int
|
||||||
AgentID string
|
AgentID string
|
||||||
}
|
}
|
||||||
@@ -33,7 +33,7 @@ func NewApp() *App {
|
|||||||
func (a *App) Run() {
|
func (a *App) Run() {
|
||||||
err := a.hubServe()
|
err := a.hubServe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.log.Error().Err(err).Msg("failed start server")
|
a.log.Error().Err(err).Msg("failed to start the server")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,8 +2,10 @@ package rpc
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
pb "github.com/lorsanstand/HomeOps-Hub/api/gen/homeops"
|
pb "github.com/lorsanstand/HomeOps-Hub/api/gen/homeops"
|
||||||
|
"github.com/lorsanstand/HomeOps-Hub/internal/domain"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
"google.golang.org/protobuf/types/known/emptypb"
|
"google.golang.org/protobuf/types/known/emptypb"
|
||||||
@@ -32,5 +34,7 @@ func (h *HubHandler) Ping(ctx context.Context, _ *emptypb.Empty) (*pb.PongRespon
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *HubHandler) RegisterAgent(ctx context.Context, request *pb.RegisterAgentRequest) (*pb.RegisterAgentResponse, error) {
|
func (h *HubHandler) RegisterAgent(ctx context.Context, request *pb.RegisterAgentRequest) (*pb.RegisterAgentResponse, error) {
|
||||||
return &pb.RegisterAgentResponse{AgentId: "12234", HeartbeatIntervalSecond: 2}, nil
|
data := domain.ToDomainAgentRequest(request)
|
||||||
|
fmt.Println(data)
|
||||||
|
return &pb.RegisterAgentResponse{}, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package hub_service
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/lorsanstand/HomeOps-Hub/internal/domain"
|
||||||
|
"github.com/rs/zerolog"
|
||||||
|
)
|
||||||
|
|
||||||
|
type HubService struct {
|
||||||
|
log zerolog.Logger
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewHubService(request domain.RegisterAgentRequest)
|
||||||
Reference in New Issue
Block a user