add prefix
This commit is contained in:
parent
4704a0a27f
commit
0ec05352e9
7 changed files with 94 additions and 29 deletions
10
README.md
10
README.md
|
|
@ -18,4 +18,12 @@ The `plugis.go` file defines the `PlugisIntf` interface, which specifies the fun
|
|||
|
||||
The `example/echoService/echoService.go` file is a minimal service implementation sample.
|
||||
|
||||
The `example/echoService/cmd/main.go` file is a sample of ServiceRunner usage.
|
||||
The `example/echoService/cmd/main.go` file is a sample of ServiceRunner usage.
|
||||
|
||||
# How to declare a service
|
||||
|
||||
```go
|
||||
|
||||
|
||||
|
||||
```
|
||||
|
|
@ -24,7 +24,7 @@ func main() {
|
|||
}
|
||||
defer nc.Close()
|
||||
|
||||
runner := plugisservice.NewServiceRunner(nc, logger)
|
||||
runner := plugisservice.NewServiceRunner(nc, logger, "integrator.customer.instance")
|
||||
|
||||
runner.Start(ctx, echoservice.NewEchoService())
|
||||
|
||||
|
|
|
|||
|
|
@ -72,23 +72,25 @@ func (svc *EchoService) Run(ctx context.Context) error {
|
|||
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",
|
||||
},
|
||||
}
|
||||
|
||||
echoEndpoint := 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",
|
||||
},
|
||||
}
|
||||
*/
|
||||
|
||||
err = service.AddEndpoint(ctx, echoEndpoint)
|
||||
err = service.AddEndpoint(ctx, pingEndpoint)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -117,9 +119,9 @@ func (svc *EchoService) Version() string {
|
|||
|
||||
// Metadata returns the metadata of the service.
|
||||
func (svc *EchoService) Metadata() plugisservice.Metadata {
|
||||
meta, err := plugisservice.NewServiceMetadata(svc.Prefix(), time.Now())
|
||||
serviceMetadata, err := plugisservice.NewServiceMetadata(svc.Prefix(), time.Now())
|
||||
if err != nil {
|
||||
svc.Logger().Error("NewServiceMetadata", "error", err)
|
||||
}
|
||||
return meta
|
||||
return serviceMetadata.Meta()
|
||||
}
|
||||
|
|
|
|||
23
example/echoService/pingEndpoint.go
Normal file
23
example/echoService/pingEndpoint.go
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
package echoservice
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/nats-io/nats.go/micro"
|
||||
nats_service "github.com/telemac/plugisservice/pkg/nats-service"
|
||||
"time"
|
||||
)
|
||||
|
||||
var pingEndpoint = nats_service.EndpointConfig{
|
||||
Name: "ping",
|
||||
Handler: func(ctx context.Context, request micro.Request) (any, error) {
|
||||
data := request.Data()
|
||||
_ = data
|
||||
return "ping: " + string(data), nil
|
||||
},
|
||||
MaxConcurrency: 10,
|
||||
RequestTimeout: 2 * time.Second,
|
||||
Metadata: map[string]string{
|
||||
"description": "ping",
|
||||
"version": "0.0.1",
|
||||
},
|
||||
}
|
||||
32
plugis.go
32
plugis.go
|
|
@ -13,7 +13,6 @@ import (
|
|||
"github.com/telemac/goutils/net"
|
||||
"github.com/telemac/goutils/task"
|
||||
|
||||
//"github.com/go-viper/mapstructure/v2"
|
||||
"github.com/nats-io/nats.go"
|
||||
"github.com/synadia-io/orbit.go/natsext"
|
||||
)
|
||||
|
|
@ -26,6 +25,7 @@ var _ PlugisIntf = (*Plugis)(nil)
|
|||
type Plugis struct {
|
||||
logger *slog.Logger
|
||||
nc *nats.Conn
|
||||
prefix string
|
||||
}
|
||||
|
||||
var (
|
||||
|
|
@ -78,7 +78,12 @@ func (plugis *Plugis) Publish(topic string, payload []byte) error {
|
|||
|
||||
// Prefix returns the prefix for the Plugis instance.
|
||||
func (plugis *Plugis) Prefix() string {
|
||||
return "integrator.customer.location"
|
||||
return plugis.prefix
|
||||
}
|
||||
|
||||
// SetPrefix sets the prefix for the Plugis instance.
|
||||
func (plugis *Plugis) SetPrefix(prefix string) {
|
||||
plugis.prefix = prefix
|
||||
}
|
||||
|
||||
// Request sends a request to the nats connection.
|
||||
|
|
@ -191,7 +196,7 @@ func NewServiceMetadata(prefix string, startedAt time.Time) (*ServiceMetadata, e
|
|||
meta.Platform += " (docker)"
|
||||
}
|
||||
|
||||
meta.StartedAt = startedAt.String()
|
||||
meta.StartedAt = startedAt.Format(time.RFC3339)
|
||||
meta.Hostname, err = os.Hostname()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
@ -203,3 +208,24 @@ func NewServiceMetadata(prefix string, startedAt time.Time) (*ServiceMetadata, e
|
|||
|
||||
return &meta, nil
|
||||
}
|
||||
|
||||
//func (smd *ServiceMetadata) Meta() Metadata {
|
||||
// var meta Metadata
|
||||
// err := mapstructure.Decode(smd, &meta)
|
||||
// if err != nil {
|
||||
// return Metadata{}
|
||||
// }
|
||||
// return meta
|
||||
//}
|
||||
|
||||
func (smd *ServiceMetadata) Meta() Metadata {
|
||||
data, err := json.Marshal(smd)
|
||||
if err != nil {
|
||||
return Metadata{}
|
||||
}
|
||||
var meta Metadata
|
||||
if err := json.Unmarshal(data, &meta); err != nil {
|
||||
return Metadata{}
|
||||
}
|
||||
return meta
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,6 +29,7 @@ type PlugisIntf interface {
|
|||
Logger() *slog.Logger
|
||||
SetNats(*nats.Conn)
|
||||
Nats() *nats.Conn
|
||||
SetPrefix(prefix string)
|
||||
Prefix() string
|
||||
Publish(topic string, payload []byte) error
|
||||
Request(subj string, data []byte, timeout time.Duration) (*nats.Msg, error)
|
||||
|
|
@ -43,7 +44,7 @@ type ServiceInfo struct {
|
|||
Description string `json:"description"`
|
||||
Version string `json:"version"`
|
||||
Type string `json:"type"`
|
||||
Metadata Metadata `json:"metadata"`
|
||||
Metadata Metadata `json:"metadata"` // contains at least ServiceMetadata fields
|
||||
Endpoints []Endpoint `json:"endpoints"`
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -11,12 +11,13 @@ import (
|
|||
|
||||
// ServiceRunner is a struct that runs one or more services.
|
||||
type ServiceRunner struct {
|
||||
wg sync.WaitGroup
|
||||
log *slog.Logger
|
||||
nc *nats.Conn
|
||||
wg sync.WaitGroup
|
||||
log *slog.Logger
|
||||
nc *nats.Conn
|
||||
prefix string
|
||||
}
|
||||
|
||||
func NewServiceRunner(nc *nats.Conn, log *slog.Logger) *ServiceRunner {
|
||||
func NewServiceRunner(nc *nats.Conn, log *slog.Logger, prefix string) *ServiceRunner {
|
||||
if log == nil {
|
||||
log = slog.Default()
|
||||
}
|
||||
|
|
@ -24,9 +25,11 @@ func NewServiceRunner(nc *nats.Conn, log *slog.Logger) *ServiceRunner {
|
|||
log.Error("failed to connect to nats")
|
||||
return nil
|
||||
}
|
||||
// TODO : handle empty prefix
|
||||
return &ServiceRunner{
|
||||
nc: nc,
|
||||
log: log,
|
||||
nc: nc,
|
||||
log: log,
|
||||
prefix: prefix,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -37,6 +40,8 @@ func (sr *ServiceRunner) Start(ctx context.Context, svc PlugisServiceIntf) {
|
|||
defer sr.wg.Done()
|
||||
svc.SetLogger(sr.log)
|
||||
svc.SetNats(sr.nc)
|
||||
// TODO : handle empty prefix
|
||||
svc.SetPrefix(sr.prefix)
|
||||
serviceType := fmt.Sprintf("%T", svc)
|
||||
err := svc.Run(ctx)
|
||||
if err != nil {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue