mirror of
https://github.com/lorsanstand/HomeOps-Hub.git
synced 2026-06-19 16:45:15 +03:00
feat: update conn in hub
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
|
// Code generated by protoc-gen-go-rpc. DO NOT EDIT.
|
||||||
// versions:
|
// versions:
|
||||||
// - protoc-gen-go-grpc v1.6.1
|
// - protoc-gen-go-rpc 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 grpc package it is being compiled against.
|
// is compatible with the rpc 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 grpc.RegisterService,
|
// It's only intended for direct use with rpc.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",
|
||||||
|
|||||||
+3
-27
@@ -1,32 +1,8 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import "github.com/lorsanstand/HomeOps-Hub/internal/hub/app"
|
||||||
"context"
|
|
||||||
"log"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/lorsanstand/HomeOps-Hub/api/gen/homeops"
|
|
||||||
"google.golang.org/grpc"
|
|
||||||
"google.golang.org/grpc/credentials/insecure"
|
|
||||||
"google.golang.org/protobuf/types/known/emptypb"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
conn, err := grpc.Dial("127.0.0.1:6756", grpc.WithTransportCredentials(insecure.NewCredentials()))
|
start := app.NewApp()
|
||||||
if err != nil {
|
start.Run()
|
||||||
log.Fatalf("dial: %v", err)
|
|
||||||
}
|
|
||||||
defer conn.Close()
|
|
||||||
|
|
||||||
client := homeops.NewHubClient(conn)
|
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
||||||
resp, err := client.Ping(ctx, &emptypb.Empty{})
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("dial: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
log.Printf("pong: %+v", resp.Pong)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,38 @@
|
|||||||
|
package app
|
||||||
|
|
||||||
|
import (
|
||||||
|
standartlog "log"
|
||||||
|
|
||||||
|
"github.com/lorsanstand/HomeOps-Hub/internal/agent/rpc"
|
||||||
|
"github.com/lorsanstand/HomeOps-Hub/internal/agent/utils/config_yaml"
|
||||||
|
log2 "github.com/lorsanstand/HomeOps-Hub/internal/shared/log"
|
||||||
|
"github.com/rs/zerolog"
|
||||||
|
)
|
||||||
|
|
||||||
|
type App struct {
|
||||||
|
log zerolog.Logger
|
||||||
|
cfg *config_yaml.AgentConfig
|
||||||
|
hubConn *rpc.Connection
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewApp() *App {
|
||||||
|
cfg, err := config_yaml.NewConfig()
|
||||||
|
if err != nil {
|
||||||
|
standartlog.Fatalf("failed get config: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
log := log2.NewLogger(cfg)
|
||||||
|
|
||||||
|
return &App{cfg: cfg, log: log}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *App) Run() {
|
||||||
|
conn, err := rpc.NewConnectAgent(a.cfg.GetGRPCAddress())
|
||||||
|
if err != nil {
|
||||||
|
a.log.Error().Err(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
a.hubConn = conn
|
||||||
|
r
|
||||||
|
}
|
||||||
@@ -1,38 +0,0 @@
|
|||||||
package grpc
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
pb "github.com/lorsanstand/HomeOps-Hub/api/gen/homeops"
|
|
||||||
"google.golang.org/grpc"
|
|
||||||
"google.golang.org/protobuf/types/known/emptypb"
|
|
||||||
)
|
|
||||||
|
|
||||||
type HomeOpsAgent struct {
|
|
||||||
conn pb.HubClient
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewConnectAgent(address string) (*HomeOpsAgent, error) {
|
|
||||||
conn, err := grpc.NewClient(address)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed connection hub: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
client := pb.NewHubClient(conn)
|
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
resp, err := client.Ping(ctx, &emptypb.Empty{})
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed connection hub: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if resp.Pong != "Pong" {
|
|
||||||
return nil, fmt.Errorf("failed connection hub: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return &HomeOpsAgent{conn: client}, nil
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package rpc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
pb "github.com/lorsanstand/HomeOps-Hub/api/gen/homeops"
|
||||||
|
"google.golang.org/grpc"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Connection struct {
|
||||||
|
hub pb.HubClient
|
||||||
|
conn *grpc.ClientConn
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewConnectAgent(address string) (*Connection, error) {
|
||||||
|
conn, err := grpc.NewClient(address)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed connection hub: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
client := pb.NewHubClient(conn)
|
||||||
|
|
||||||
|
return &Connection{hub: client, conn: conn}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Connection) Close() error {
|
||||||
|
return c.conn.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Connection) Hub() pb.HubClient {
|
||||||
|
return c.hub
|
||||||
|
}
|
||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/rs/zerolog"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -13,6 +14,7 @@ type AgentConfig struct {
|
|||||||
Host string `yaml:"host"`
|
Host string `yaml:"host"`
|
||||||
Port int `yaml:"port"`
|
Port int `yaml:"port"`
|
||||||
} `yaml:"hub"`
|
} `yaml:"hub"`
|
||||||
|
LogLevel string `yaml:"log_level"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConfig() (*AgentConfig, error) {
|
func NewConfig() (*AgentConfig, error) {
|
||||||
@@ -29,3 +31,19 @@ func NewConfig() (*AgentConfig, error) {
|
|||||||
|
|
||||||
return &cfg, nil
|
return &cfg, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *AgentConfig) GetLogLevel() zerolog.Level {
|
||||||
|
level, err := zerolog.ParseLevel(c.LogLevel)
|
||||||
|
if err != nil {
|
||||||
|
return zerolog.InfoLevel
|
||||||
|
}
|
||||||
|
return level
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *AgentConfig) GetMode() string {
|
||||||
|
return "PROD"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *AgentConfig) GetGRPCAddress() string {
|
||||||
|
return fmt.Sprintf("%v:%v", c.HubConnect.Host, c.HubConnect.Port)
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,10 +2,11 @@ package app
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
standartlog "log"
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
pb "github.com/lorsanstand/HomeOps-Hub/api/gen/homeops"
|
pb "github.com/lorsanstand/HomeOps-Hub/api/gen/homeops"
|
||||||
grpcserv "github.com/lorsanstand/HomeOps-Hub/internal/hub/grpc"
|
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"
|
||||||
@@ -20,7 +21,7 @@ type App struct {
|
|||||||
func NewApp() *App {
|
func NewApp() *App {
|
||||||
cfg, err := config.NewConfig()
|
cfg, err := config.NewConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Errorf("failed get config: %v", err)
|
standartlog.Fatalf("failed get config: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
logger := log.NewLogger(cfg)
|
logger := log.NewLogger(cfg)
|
||||||
@@ -44,7 +45,7 @@ func (a *App) Run() {
|
|||||||
|
|
||||||
err = grpcServer.Serve(lis)
|
err = grpcServer.Serve(lis)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.log.Error().Err(err).Msg("failed started grpc server")
|
a.log.Error().Err(err).Msg("failed started rpc server")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package grpc
|
package rpc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
Reference in New Issue
Block a user