refactor: hub grpc system

This commit is contained in:
lorsan
2026-04-06 11:39:19 +03:00
committed by lorsan
parent 306dcff31f
commit 0bd52aebea
11 changed files with 167 additions and 50 deletions
+10
View File
@@ -0,0 +1,10 @@
# Default ignored files
/shelf/
/workspace.xml
# Ignored default folder with query files
/queries/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# Editor-based HTTP Client requests
/httpRequests/
+4
View File
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="Go" enabled="true" />
</module>
+11
View File
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GoImports">
<option name="excludedPackages">
<array>
<option value="github.com/pkg/errors" />
<option value="golang.org/x/net/context" />
</array>
</option>
</component>
</project>
Generated
+6
View File
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>
+84 -18
View File
@@ -73,6 +73,7 @@ type RegisterAgentRequest struct {
Hostname string `protobuf:"bytes,3,opt,name=hostname,proto3" json:"hostname,omitempty"` Hostname string `protobuf:"bytes,3,opt,name=hostname,proto3" json:"hostname,omitempty"`
Version string `protobuf:"bytes,4,opt,name=version,proto3" json:"version,omitempty"` Version string `protobuf:"bytes,4,opt,name=version,proto3" json:"version,omitempty"`
Arch string `protobuf:"bytes,5,opt,name=arch,proto3" json:"arch,omitempty"` Arch string `protobuf:"bytes,5,opt,name=arch,proto3" json:"arch,omitempty"`
Config *AgentConfig `protobuf:"bytes,6,opt,name=config,proto3" json:"config,omitempty"`
unknownFields protoimpl.UnknownFields unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache sizeCache protoimpl.SizeCache
} }
@@ -142,6 +143,65 @@ func (x *RegisterAgentRequest) GetArch() string {
return "" return ""
} }
func (x *RegisterAgentRequest) GetConfig() *AgentConfig {
if x != nil {
return x.Config
}
return nil
}
type AgentConfig struct {
state protoimpl.MessageState `protogen:"open.v1"`
System string `protobuf:"bytes,1,opt,name=system,proto3" json:"system,omitempty"`
Docker bool `protobuf:"varint,2,opt,name=docker,proto3" json:"docker,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
func (x *AgentConfig) Reset() {
*x = AgentConfig{}
mi := &file_homeops_hub_proto_msgTypes[2]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
func (x *AgentConfig) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*AgentConfig) ProtoMessage() {}
func (x *AgentConfig) ProtoReflect() protoreflect.Message {
mi := &file_homeops_hub_proto_msgTypes[2]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use AgentConfig.ProtoReflect.Descriptor instead.
func (*AgentConfig) Descriptor() ([]byte, []int) {
return file_homeops_hub_proto_rawDescGZIP(), []int{2}
}
func (x *AgentConfig) GetSystem() string {
if x != nil {
return x.System
}
return ""
}
func (x *AgentConfig) GetDocker() bool {
if x != nil {
return x.Docker
}
return false
}
type RegisterAgentResponse struct { type RegisterAgentResponse struct {
state protoimpl.MessageState `protogen:"open.v1"` state protoimpl.MessageState `protogen:"open.v1"`
HeartbeatIntervalSecond int64 `protobuf:"varint,1,opt,name=heartbeat_interval_second,json=heartbeatIntervalSecond,proto3" json:"heartbeat_interval_second,omitempty"` HeartbeatIntervalSecond int64 `protobuf:"varint,1,opt,name=heartbeat_interval_second,json=heartbeatIntervalSecond,proto3" json:"heartbeat_interval_second,omitempty"`
@@ -151,7 +211,7 @@ type RegisterAgentResponse struct {
func (x *RegisterAgentResponse) Reset() { func (x *RegisterAgentResponse) Reset() {
*x = RegisterAgentResponse{} *x = RegisterAgentResponse{}
mi := &file_homeops_hub_proto_msgTypes[2] mi := &file_homeops_hub_proto_msgTypes[3]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi) ms.StoreMessageInfo(mi)
} }
@@ -163,7 +223,7 @@ func (x *RegisterAgentResponse) String() string {
func (*RegisterAgentResponse) ProtoMessage() {} func (*RegisterAgentResponse) ProtoMessage() {}
func (x *RegisterAgentResponse) ProtoReflect() protoreflect.Message { func (x *RegisterAgentResponse) ProtoReflect() protoreflect.Message {
mi := &file_homeops_hub_proto_msgTypes[2] mi := &file_homeops_hub_proto_msgTypes[3]
if x != nil { if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil { if ms.LoadMessageInfo() == nil {
@@ -176,7 +236,7 @@ func (x *RegisterAgentResponse) ProtoReflect() protoreflect.Message {
// Deprecated: Use RegisterAgentResponse.ProtoReflect.Descriptor instead. // Deprecated: Use RegisterAgentResponse.ProtoReflect.Descriptor instead.
func (*RegisterAgentResponse) Descriptor() ([]byte, []int) { func (*RegisterAgentResponse) Descriptor() ([]byte, []int) {
return file_homeops_hub_proto_rawDescGZIP(), []int{2} return file_homeops_hub_proto_rawDescGZIP(), []int{3}
} }
func (x *RegisterAgentResponse) GetHeartbeatIntervalSecond() int64 { func (x *RegisterAgentResponse) GetHeartbeatIntervalSecond() int64 {
@@ -192,14 +252,18 @@ const file_homeops_hub_proto_rawDesc = "" +
"\n" + "\n" +
"\x11homeops/hub.proto\x1a\x1bgoogle/protobuf/empty.proto\"\"\n" + "\x11homeops/hub.proto\x1a\x1bgoogle/protobuf/empty.proto\"\"\n" +
"\fPongResponse\x12\x12\n" + "\fPongResponse\x12\x12\n" +
"\x04pong\x18\x01 \x01(\tR\x04pong\"\x9a\x01\n" + "\x04pong\x18\x01 \x01(\tR\x04pong\"\xc0\x01\n" +
"\x14RegisterAgentRequest\x12\x19\n" + "\x14RegisterAgentRequest\x12\x19\n" +
"\bagent_id\x18\x01 \x01(\tR\aagentId\x12\x1d\n" + "\bagent_id\x18\x01 \x01(\tR\aagentId\x12\x1d\n" +
"\n" + "\n" +
"agent_name\x18\x02 \x01(\tR\tagentName\x12\x1a\n" + "agent_name\x18\x02 \x01(\tR\tagentName\x12\x1a\n" +
"\bhostname\x18\x03 \x01(\tR\bhostname\x12\x18\n" + "\bhostname\x18\x03 \x01(\tR\bhostname\x12\x18\n" +
"\aversion\x18\x04 \x01(\tR\aversion\x12\x12\n" + "\aversion\x18\x04 \x01(\tR\aversion\x12\x12\n" +
"\x04arch\x18\x05 \x01(\tR\x04arch\"S\n" + "\x04arch\x18\x05 \x01(\tR\x04arch\x12$\n" +
"\x06config\x18\x06 \x01(\v2\f.AgentConfigR\x06config\"=\n" +
"\vAgentConfig\x12\x16\n" +
"\x06system\x18\x01 \x01(\tR\x06system\x12\x16\n" +
"\x06docker\x18\x02 \x01(\bR\x06docker\"S\n" +
"\x15RegisterAgentResponse\x12:\n" + "\x15RegisterAgentResponse\x12:\n" +
"\x19heartbeat_interval_second\x18\x01 \x01(\x03R\x17heartbeatIntervalSecond2x\n" + "\x19heartbeat_interval_second\x18\x01 \x01(\x03R\x17heartbeatIntervalSecond2x\n" +
"\x03Hub\x12/\n" + "\x03Hub\x12/\n" +
@@ -218,23 +282,25 @@ func file_homeops_hub_proto_rawDescGZIP() []byte {
return file_homeops_hub_proto_rawDescData return file_homeops_hub_proto_rawDescData
} }
var file_homeops_hub_proto_msgTypes = make([]protoimpl.MessageInfo, 3) var file_homeops_hub_proto_msgTypes = make([]protoimpl.MessageInfo, 4)
var file_homeops_hub_proto_goTypes = []any{ var file_homeops_hub_proto_goTypes = []any{
(*PongResponse)(nil), // 0: PongResponse (*PongResponse)(nil), // 0: PongResponse
(*RegisterAgentRequest)(nil), // 1: RegisterAgentRequest (*RegisterAgentRequest)(nil), // 1: RegisterAgentRequest
(*RegisterAgentResponse)(nil), // 2: RegisterAgentResponse (*AgentConfig)(nil), // 2: AgentConfig
(*emptypb.Empty)(nil), // 3: google.protobuf.Empty (*RegisterAgentResponse)(nil), // 3: RegisterAgentResponse
(*emptypb.Empty)(nil), // 4: google.protobuf.Empty
} }
var file_homeops_hub_proto_depIdxs = []int32{ var file_homeops_hub_proto_depIdxs = []int32{
3, // 0: Hub.Ping:input_type -> google.protobuf.Empty 2, // 0: RegisterAgentRequest.config:type_name -> AgentConfig
1, // 1: Hub.RegisterAgent:input_type -> RegisterAgentRequest 4, // 1: Hub.Ping:input_type -> google.protobuf.Empty
0, // 2: Hub.Ping:output_type -> PongResponse 1, // 2: Hub.RegisterAgent:input_type -> RegisterAgentRequest
2, // 3: Hub.RegisterAgent:output_type -> RegisterAgentResponse 0, // 3: Hub.Ping:output_type -> PongResponse
2, // [2:4] is the sub-list for method output_type 3, // 4: Hub.RegisterAgent:output_type -> RegisterAgentResponse
0, // [0:2] is the sub-list for method input_type 3, // [3:5] is the sub-list for method output_type
0, // [0:0] is the sub-list for extension type_name 1, // [1:3] is the sub-list for method input_type
0, // [0:0] is the sub-list for extension extendee 1, // [1:1] is the sub-list for extension type_name
0, // [0:0] is the sub-list for field type_name 1, // [1:1] is the sub-list for extension extendee
0, // [0:1] is the sub-list for field type_name
} }
func init() { file_homeops_hub_proto_init() } func init() { file_homeops_hub_proto_init() }
@@ -248,7 +314,7 @@ func file_homeops_hub_proto_init() {
GoPackagePath: reflect.TypeOf(x{}).PkgPath(), GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: unsafe.Slice(unsafe.StringData(file_homeops_hub_proto_rawDesc), len(file_homeops_hub_proto_rawDesc)), RawDescriptor: unsafe.Slice(unsafe.StringData(file_homeops_hub_proto_rawDesc), len(file_homeops_hub_proto_rawDesc)),
NumEnums: 0, NumEnums: 0,
NumMessages: 3, NumMessages: 4,
NumExtensions: 0, NumExtensions: 0,
NumServices: 1, NumServices: 1,
}, },
+4 -4
View File
@@ -1,6 +1,6 @@
// Code generated by protoc-gen-go-rpc. DO NOT EDIT. // Code generated by protoc-gen-go-grpc. DO NOT EDIT.
// versions: // versions:
// - protoc-gen-go-rpc v1.6.1 // - protoc-gen-go-grpc v1.6.1
// - protoc v7.34.1 // - protoc v7.34.1
// source: homeops/hub.proto // source: homeops/hub.proto
@@ -15,7 +15,7 @@ import (
) )
// This is a compile-time assertion to ensure that this generated file // This is a compile-time assertion to ensure that this generated file
// is compatible with the rpc package it is being compiled against. // is compatible with the grpc package it is being compiled against.
// Requires gRPC-Go v1.64.0 or later. // Requires gRPC-Go v1.64.0 or later.
const _ = grpc.SupportPackageIsVersion9 const _ = grpc.SupportPackageIsVersion9
@@ -140,7 +140,7 @@ func _Hub_RegisterAgent_Handler(srv interface{}, ctx context.Context, dec func(i
} }
// Hub_ServiceDesc is the grpc.ServiceDesc for Hub service. // Hub_ServiceDesc is the grpc.ServiceDesc for Hub service.
// It's only intended for direct use with rpc.RegisterService, // It's only intended for direct use with grpc.RegisterService,
// and not to be introspected or modified (even as a copy) // and not to be introspected or modified (even as a copy)
var Hub_ServiceDesc = grpc.ServiceDesc{ var Hub_ServiceDesc = grpc.ServiceDesc{
ServiceName: "Hub", ServiceName: "Hub",
+6
View File
@@ -19,6 +19,12 @@ message RegisterAgentRequest {
string hostname = 3; string hostname = 3;
string version = 4; string version = 4;
string arch = 5; string arch = 5;
AgentConfig config = 6;
}
message AgentConfig {
string system = 1;
bool docker = 2;
} }
message RegisterAgentResponse { message RegisterAgentResponse {
-1
View File
@@ -34,5 +34,4 @@ func (a *App) Run() {
} }
a.hubConn = conn a.hubConn = conn
r
} }
@@ -0,0 +1 @@
package hub_service
+23 -20
View File
@@ -5,17 +5,16 @@ import (
standartlog "log" standartlog "log"
"net" "net"
pb "github.com/lorsanstand/HomeOps-Hub/api/gen/homeops"
grpcserv "github.com/lorsanstand/HomeOps-Hub/internal/hub/rpc" grpcserv "github.com/lorsanstand/HomeOps-Hub/internal/hub/rpc"
"github.com/lorsanstand/HomeOps-Hub/internal/shared/config" "github.com/lorsanstand/HomeOps-Hub/internal/shared/config"
"github.com/lorsanstand/HomeOps-Hub/internal/shared/log" "github.com/lorsanstand/HomeOps-Hub/internal/shared/log"
"github.com/rs/zerolog" "github.com/rs/zerolog"
"google.golang.org/grpc"
) )
type App struct { type App struct {
cfg *config.Config cfg *config.Config
log zerolog.Logger log zerolog.Logger
server *grpcserv.HubHandler
} }
func NewApp() *App { func NewApp() *App {
@@ -26,27 +25,31 @@ func NewApp() *App {
logger := log.NewLogger(cfg) logger := log.NewLogger(cfg)
return &App{cfg: cfg, log: logger} server := grpcserv.NewHubHandler(logger)
return &App{cfg: cfg, log: logger, server: server}
} }
func (a *App) Run() { func (a *App) Run() {
address := fmt.Sprintf("http://0.0.0.0:%v", a.cfg.Port) err := a.hubServe()
lis, err := net.Listen("tcp", fmt.Sprintf(":%v", a.cfg.Port))
if err != nil { if err != nil {
a.log.Error().Err(err).Msg("failed started listen") a.log.Error().Err(err).Msg("failed start server")
return
} }
}
grpcServer := grpc.NewServer()
pb.RegisterHubServer(grpcServer, grpcserv.NewServer()) func (a *App) hubServe() error {
address := fmt.Sprintf("0.0.0.0:%v", a.cfg.Port)
a.log.Info().Str("address", address).Msg("server started") a.log.Info().Str("address", "http://"+address).Msg("start GRPC server")
err = grpcServer.Serve(lis) lis, err := net.Listen("tcp", address)
if err != nil { if err != nil {
a.log.Error().Err(err).Msg("failed started rpc server") return err
return }
}
err = a.server.GrpcServer.Serve(lis)
if err != nil {
return err
}
return nil
} }
+16 -5
View File
@@ -4,18 +4,29 @@ import (
"context" "context"
pb "github.com/lorsanstand/HomeOps-Hub/api/gen/homeops" pb "github.com/lorsanstand/HomeOps-Hub/api/gen/homeops"
"github.com/rs/zerolog"
"google.golang.org/grpc"
"google.golang.org/protobuf/types/known/emptypb" "google.golang.org/protobuf/types/known/emptypb"
) )
type Server struct { type HubHandler struct {
pb.UnimplementedHubServer pb.UnimplementedHubServer
log zerolog.Logger
GrpcServer *grpc.Server
} }
func NewServer() *Server { func NewHubHandler(logger zerolog.Logger) *HubHandler {
return &Server{} hub := &HubHandler{log: logger}
grpcServer := grpc.NewServer()
pb.RegisterHubServer(grpcServer, hub)
hub.GrpcServer = grpcServer
return hub
} }
func (s *Server) Ping(ctx context.Context, _ *emptypb.Empty) (*pb.PongResponse, error) { func (h *HubHandler) Ping(ctx context.Context, _ *emptypb.Empty) (*pb.PongResponse, error) {
h.log.Info().Msg("pong request")
return &pb.PongResponse{Pong: "Pong"}, nil return &pb.PongResponse{Pong: "Pong"}, nil
} }