From 0bd52aebeaa5066c45ee469f67a1ed04b1a36404 Mon Sep 17 00:00:00 2001 From: lorsan Date: Mon, 6 Apr 2026 11:39:19 +0300 Subject: [PATCH] refactor: hub grpc system --- .idea/.gitignore | 10 +++ .idea/HomeOps-Hub.iml | 4 + .idea/go.imports.xml | 11 +++ .idea/vcs.xml | 6 ++ api/gen/homeops/hub.pb.go | 102 ++++++++++++++++++---- api/gen/homeops/hub_grpc.pb.go | 8 +- api/proto/homeops/hub.proto | 6 ++ internal/agent/app/app.go | 1 - internal/agent/service/hub_service/hub.go | 1 + internal/hub/app/app.go | 47 +++++----- internal/hub/rpc/server.go | 21 +++-- 11 files changed, 167 insertions(+), 50 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/HomeOps-Hub.iml create mode 100644 .idea/go.imports.xml create mode 100644 .idea/vcs.xml create mode 100644 internal/agent/service/hub_service/hub.go diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..ab1f416 --- /dev/null +++ b/.idea/.gitignore @@ -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/ diff --git a/.idea/HomeOps-Hub.iml b/.idea/HomeOps-Hub.iml new file mode 100644 index 0000000..7ee078d --- /dev/null +++ b/.idea/HomeOps-Hub.iml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/go.imports.xml b/.idea/go.imports.xml new file mode 100644 index 0000000..d7202f0 --- /dev/null +++ b/.idea/go.imports.xml @@ -0,0 +1,11 @@ + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/api/gen/homeops/hub.pb.go b/api/gen/homeops/hub.pb.go index 0e317ab..5f6836c 100644 --- a/api/gen/homeops/hub.pb.go +++ b/api/gen/homeops/hub.pb.go @@ -73,6 +73,7 @@ type RegisterAgentRequest struct { Hostname string `protobuf:"bytes,3,opt,name=hostname,proto3" json:"hostname,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"` + Config *AgentConfig `protobuf:"bytes,6,opt,name=config,proto3" json:"config,omitempty"` unknownFields protoimpl.UnknownFields sizeCache protoimpl.SizeCache } @@ -142,6 +143,65 @@ func (x *RegisterAgentRequest) GetArch() string { 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 { state protoimpl.MessageState `protogen:"open.v1"` 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() { *x = RegisterAgentResponse{} - mi := &file_homeops_hub_proto_msgTypes[2] + mi := &file_homeops_hub_proto_msgTypes[3] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -163,7 +223,7 @@ func (x *RegisterAgentResponse) String() string { func (*RegisterAgentResponse) ProtoMessage() {} func (x *RegisterAgentResponse) ProtoReflect() protoreflect.Message { - mi := &file_homeops_hub_proto_msgTypes[2] + mi := &file_homeops_hub_proto_msgTypes[3] if x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -176,7 +236,7 @@ func (x *RegisterAgentResponse) ProtoReflect() protoreflect.Message { // Deprecated: Use RegisterAgentResponse.ProtoReflect.Descriptor instead. 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 { @@ -192,14 +252,18 @@ const file_homeops_hub_proto_rawDesc = "" + "\n" + "\x11homeops/hub.proto\x1a\x1bgoogle/protobuf/empty.proto\"\"\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" + "\bagent_id\x18\x01 \x01(\tR\aagentId\x12\x1d\n" + "\n" + "agent_name\x18\x02 \x01(\tR\tagentName\x12\x1a\n" + "\bhostname\x18\x03 \x01(\tR\bhostname\x12\x18\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" + "\x19heartbeat_interval_second\x18\x01 \x01(\x03R\x17heartbeatIntervalSecond2x\n" + "\x03Hub\x12/\n" + @@ -218,23 +282,25 @@ func file_homeops_hub_proto_rawDescGZIP() []byte { 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{ (*PongResponse)(nil), // 0: PongResponse (*RegisterAgentRequest)(nil), // 1: RegisterAgentRequest - (*RegisterAgentResponse)(nil), // 2: RegisterAgentResponse - (*emptypb.Empty)(nil), // 3: google.protobuf.Empty + (*AgentConfig)(nil), // 2: AgentConfig + (*RegisterAgentResponse)(nil), // 3: RegisterAgentResponse + (*emptypb.Empty)(nil), // 4: google.protobuf.Empty } var file_homeops_hub_proto_depIdxs = []int32{ - 3, // 0: Hub.Ping:input_type -> google.protobuf.Empty - 1, // 1: Hub.RegisterAgent:input_type -> RegisterAgentRequest - 0, // 2: Hub.Ping:output_type -> PongResponse - 2, // 3: Hub.RegisterAgent:output_type -> RegisterAgentResponse - 2, // [2:4] is the sub-list for method output_type - 0, // [0:2] is the sub-list for method input_type - 0, // [0:0] is the sub-list for extension type_name - 0, // [0:0] is the sub-list for extension extendee - 0, // [0:0] is the sub-list for field type_name + 2, // 0: RegisterAgentRequest.config:type_name -> AgentConfig + 4, // 1: Hub.Ping:input_type -> google.protobuf.Empty + 1, // 2: Hub.RegisterAgent:input_type -> RegisterAgentRequest + 0, // 3: Hub.Ping:output_type -> PongResponse + 3, // 4: Hub.RegisterAgent:output_type -> RegisterAgentResponse + 3, // [3:5] is the sub-list for method output_type + 1, // [1:3] is the sub-list for method input_type + 1, // [1:1] is the sub-list for extension 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() } @@ -248,7 +314,7 @@ func file_homeops_hub_proto_init() { GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: unsafe.Slice(unsafe.StringData(file_homeops_hub_proto_rawDesc), len(file_homeops_hub_proto_rawDesc)), NumEnums: 0, - NumMessages: 3, + NumMessages: 4, NumExtensions: 0, NumServices: 1, }, diff --git a/api/gen/homeops/hub_grpc.pb.go b/api/gen/homeops/hub_grpc.pb.go index f18e257..e032d6c 100644 --- a/api/gen/homeops/hub_grpc.pb.go +++ b/api/gen/homeops/hub_grpc.pb.go @@ -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: -// - protoc-gen-go-rpc v1.6.1 +// - protoc-gen-go-grpc v1.6.1 // - protoc v7.34.1 // source: homeops/hub.proto @@ -15,7 +15,7 @@ import ( ) // 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. 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. -// 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) var Hub_ServiceDesc = grpc.ServiceDesc{ ServiceName: "Hub", diff --git a/api/proto/homeops/hub.proto b/api/proto/homeops/hub.proto index 4c987f2..01c9698 100644 --- a/api/proto/homeops/hub.proto +++ b/api/proto/homeops/hub.proto @@ -19,6 +19,12 @@ message RegisterAgentRequest { string hostname = 3; string version = 4; string arch = 5; + AgentConfig config = 6; +} + +message AgentConfig { + string system = 1; + bool docker = 2; } message RegisterAgentResponse { diff --git a/internal/agent/app/app.go b/internal/agent/app/app.go index 5d68171..18f4f60 100644 --- a/internal/agent/app/app.go +++ b/internal/agent/app/app.go @@ -34,5 +34,4 @@ func (a *App) Run() { } a.hubConn = conn - r } diff --git a/internal/agent/service/hub_service/hub.go b/internal/agent/service/hub_service/hub.go new file mode 100644 index 0000000..e80229d --- /dev/null +++ b/internal/agent/service/hub_service/hub.go @@ -0,0 +1 @@ +package hub_service diff --git a/internal/hub/app/app.go b/internal/hub/app/app.go index 002eacc..0db6be1 100644 --- a/internal/hub/app/app.go +++ b/internal/hub/app/app.go @@ -5,17 +5,16 @@ import ( standartlog "log" "net" - pb "github.com/lorsanstand/HomeOps-Hub/api/gen/homeops" grpcserv "github.com/lorsanstand/HomeOps-Hub/internal/hub/rpc" "github.com/lorsanstand/HomeOps-Hub/internal/shared/config" "github.com/lorsanstand/HomeOps-Hub/internal/shared/log" "github.com/rs/zerolog" - "google.golang.org/grpc" ) type App struct { - cfg *config.Config - log zerolog.Logger + cfg *config.Config + log zerolog.Logger + server *grpcserv.HubHandler } func NewApp() *App { @@ -26,27 +25,31 @@ func NewApp() *App { 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() { - address := fmt.Sprintf("http://0.0.0.0:%v", a.cfg.Port) - - lis, err := net.Listen("tcp", fmt.Sprintf(":%v", a.cfg.Port)) + err := a.hubServe() if err != nil { - a.log.Error().Err(err).Msg("failed started listen") - return + a.log.Error().Err(err).Msg("failed start server") } - - grpcServer := grpc.NewServer() - pb.RegisterHubServer(grpcServer, grpcserv.NewServer()) - - a.log.Info().Str("address", address).Msg("server started") - - err = grpcServer.Serve(lis) - if err != nil { - a.log.Error().Err(err).Msg("failed started rpc server") - return - } - +} + +func (a *App) hubServe() error { + address := fmt.Sprintf("0.0.0.0:%v", a.cfg.Port) + a.log.Info().Str("address", "http://"+address).Msg("start GRPC server") + + lis, err := net.Listen("tcp", address) + if err != nil { + return err + } + + err = a.server.GrpcServer.Serve(lis) + if err != nil { + return err + } + + return nil } diff --git a/internal/hub/rpc/server.go b/internal/hub/rpc/server.go index 9a4b83f..0692d17 100644 --- a/internal/hub/rpc/server.go +++ b/internal/hub/rpc/server.go @@ -4,18 +4,29 @@ import ( "context" pb "github.com/lorsanstand/HomeOps-Hub/api/gen/homeops" + "github.com/rs/zerolog" + "google.golang.org/grpc" "google.golang.org/protobuf/types/known/emptypb" ) -type Server struct { +type HubHandler struct { pb.UnimplementedHubServer + log zerolog.Logger + GrpcServer *grpc.Server } -func NewServer() *Server { - return &Server{} +func NewHubHandler(logger zerolog.Logger) *HubHandler { + 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 - }