From 50b4fce8a3725d8854db8d5d93eca352193d9873 Mon Sep 17 00:00:00 2001 From: Alexandre HEIM Date: Thu, 26 Jun 2025 18:34:53 +0200 Subject: [PATCH] add StartService to plugis --- example/echoService/echoService.go | 35 ++++------------------- example/echoService/pingEndpoint.go | 10 ++++++- pkg/nats-service/nats-service.go | 5 ++-- pkg/systeminfo/systeminfo.go | 44 ----------------------------- plugis.go | 16 +++++++++++ plugisservice.go | 2 ++ 6 files changed, 35 insertions(+), 77 deletions(-) delete mode 100644 pkg/systeminfo/systeminfo.go diff --git a/example/echoService/echoService.go b/example/echoService/echoService.go index 6404d54..049c6f4 100644 --- a/example/echoService/echoService.go +++ b/example/echoService/echoService.go @@ -2,9 +2,7 @@ package echoservice import ( "context" - "github.com/nats-io/nats.go/micro" "github.com/telemac/plugisservice" - nats_service "github.com/telemac/plugisservice/pkg/nats-service" "time" ) @@ -21,7 +19,7 @@ func NewEchoService() *EchoService { } // ExecuteCommand sends a command -func (svc *EchoService) ExecuteCommand(ctx context.Context, command string) error { +func (svc *EchoService) ExecuteCommand(ctx context.Context, command string) ([]byte, error) { subject := "ism.homelab.service.plugis.command" svc.Logger().Info("sending command", @@ -35,7 +33,7 @@ func (svc *EchoService) ExecuteCommand(ctx context.Context, command string) erro "error", err, "command", command, ) - return err + return nil, err } else { svc.Logger().Info("command executed successfully", "command", command, @@ -43,7 +41,7 @@ func (svc *EchoService) ExecuteCommand(ctx context.Context, command string) erro ) } - return nil + return msg.Data, nil } // Run is the main function of the service. @@ -59,37 +57,14 @@ func (svc *EchoService) Run(ctx context.Context) error { svc.ExecuteCommand(ctx, "sleep 3") - service, err := nats_service.NewNatsService(svc.Nats(), svc.Prefix(), micro.Config{ - Name: svc.Name(), - Endpoint: nil, - Version: svc.Version(), - Description: svc.Description(), - Metadata: svc.Metadata(), - }) + service, err := svc.StartService(svc) if err != nil { return err } defer func() { service.Stop() }() - /* - pingEndpoint := nats_service.EndpointConfig{ - Name: "ping", - Handler: func(ctx context.Context, request micro.Request) (any, error) { - data := request.Data() - _ = data - return "ping: " + string(data), err - }, - MaxConcurrency: 10, - RequestTimeout: 2 * time.Second, - Metadata: map[string]string{ - "description": "ping", - "version": "0.0.1", - }, - } - - */ - + pingEndpoint.UserData = svc err = service.AddEndpoint(ctx, pingEndpoint) if err != nil { return err diff --git a/example/echoService/pingEndpoint.go b/example/echoService/pingEndpoint.go index 10307e6..9e35e10 100644 --- a/example/echoService/pingEndpoint.go +++ b/example/echoService/pingEndpoint.go @@ -9,9 +9,17 @@ import ( var pingEndpoint = nats_service.EndpointConfig{ Name: "ping", - Handler: func(ctx context.Context, request micro.Request) (any, error) { + Handler: func(ctx context.Context, request micro.Request, ec nats_service.EndpointConfig) (any, error) { data := request.Data() _ = data + // get plugis from EndpointConfigt + echoService, ok := ec.UserData.(*EchoService) + if ok { + echoService.Logger().Info("plugis ping received") + res, err := echoService.ExecuteCommand(ctx, "hostnamectl") + return string(res), err + } + return ec, nil return "ping: " + string(data), nil }, MaxConcurrency: 10, diff --git a/pkg/nats-service/nats-service.go b/pkg/nats-service/nats-service.go index 87ecb65..ea40357 100644 --- a/pkg/nats-service/nats-service.go +++ b/pkg/nats-service/nats-service.go @@ -18,6 +18,7 @@ type EndpointConfig struct { Metadata map[string]string `json:"metadata,omitempty"` QueueGroup string `json:"queue_group,omitempty"` Subject string `json:"subject,omitempty"` + UserData any `json:"-"` } // setDefaults applies default values to endpoint configuration @@ -90,7 +91,7 @@ type PlugisServiceReply struct { } // PlugisServiceHandler is the function signature for endpoint handlers -type PlugisServiceHandler func(ctx context.Context, request micro.Request) (any, error) +type PlugisServiceHandler func(ctx context.Context, request micro.Request, endpoint EndpointConfig) (any, error) // PlugisHandler manages concurrency and timeouts for a single endpoint type PlugisHandler struct { @@ -154,7 +155,7 @@ func (ph PlugisHandler) handleWithTimeout(req micro.Request) { } }() - data, err := ph.plugisServiceHandler(ctx, req) + data, err := ph.plugisServiceHandler(ctx, req, ph.config) resultChan <- result{data: data, err: err} }() diff --git a/pkg/systeminfo/systeminfo.go b/pkg/systeminfo/systeminfo.go deleted file mode 100644 index e843977..0000000 --- a/pkg/systeminfo/systeminfo.go +++ /dev/null @@ -1,44 +0,0 @@ -package systeminfo - -import ( - "fmt" - "github.com/telemac/goutils/net" - "os" - "runtime" -) - -type SystemInfo struct { - Platform string `json:"platform"` // ex: linux/amd64 - Docker bool `json:"docker"` // true if runnint in docker container - Hostname string `json:"hostname"` - MacAddress string `json:"mac_address"` -} - -func NewSystemInfo() (*SystemInfo, error) { - var systemInfo SystemInfo - err := systemInfo.Fill() - return &systemInfo, err -} - -func (si *SystemInfo) Fill() error { - var err error - si.Platform = runtime.GOOS + "/" + runtime.GOARCH - si.Docker = isRunningInDockerContainer() - si.Hostname, err = os.Hostname() - if err != nil { - return fmt.Errorf("unable to get hostname: %v", err) - } - si.MacAddress, err = net.GetMACAddress() - if err != nil { - return fmt.Errorf("unable to get MAC address: %v", err) - } - return nil -} - -// isRunningInDockerContainer -func isRunningInDockerContainer() bool { - if _, err := os.Stat("/.dockerenv"); err == nil { - return true - } - return false -} diff --git a/plugis.go b/plugis.go index ac55bf3..96df961 100644 --- a/plugis.go +++ b/plugis.go @@ -4,6 +4,8 @@ import ( "context" "encoding/json" "errors" + "github.com/nats-io/nats.go/micro" + nats_service "github.com/telemac/plugisservice/pkg/nats-service" "iter" "log/slog" "os" @@ -218,6 +220,7 @@ func NewServiceMetadata(prefix string, startedAt time.Time) (*ServiceMetadata, e // return meta //} +// Meta returns ServiceMetaData as map[string]string func (smd *ServiceMetadata) Meta() Metadata { data, err := json.Marshal(smd) if err != nil { @@ -229,3 +232,16 @@ func (smd *ServiceMetadata) Meta() Metadata { } return meta } + +// StartService initializes and starts a NATS service for the given PlugisServiceIntf implementation. +// It returns the created NatsService and any error encountered during the creation process. +func (plugis *Plugis) StartService(svc PlugisServiceIntf) (*nats_service.NatsService, error) { + service, err := nats_service.NewNatsService(plugis.Nats(), plugis.Prefix(), micro.Config{ + Name: svc.Name(), + Endpoint: nil, + Version: svc.Version(), + Description: svc.Description(), + Metadata: svc.Metadata(), + }) + return service, err +} diff --git a/plugisservice.go b/plugisservice.go index 8ad1b1a..b1fe329 100644 --- a/plugisservice.go +++ b/plugisservice.go @@ -2,6 +2,7 @@ package plugisservice import ( "context" + nats_service "github.com/telemac/plugisservice/pkg/nats-service" "iter" "log/slog" "time" @@ -35,6 +36,7 @@ type PlugisIntf interface { Request(subj string, data []byte, timeout time.Duration) (*nats.Msg, error) RequestMany(ctx context.Context, subject string, data []byte, opts ...natsext.RequestManyOpt) (iter.Seq2[*nats.Msg, error], error) GetServices(ctx context.Context) ([]ServiceInfo, error) + StartService(svc PlugisServiceIntf) (*nats_service.NatsService, error) } // ServiceInfo is the information about a service.