Skip to content

Commit

Permalink
added subject field to ServiceInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
aricart committed Oct 20, 2022
1 parent bba377a commit 43050b2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
27 changes: 18 additions & 9 deletions service.go
Expand Up @@ -61,13 +61,14 @@ type ServiceInfo struct {
Id string `json:"id"`
Description string `json:"description"`
Version string `json:"version"`
Subject string `json:"subject"`
}

func (s *ServiceInfo) Valid() error {
func (s *ServiceConfig) Valid() error {
if s.Name == "" {
return errors.New("name is required")
}
return nil
return s.Endpoint.Valid()
}

type ServiceSchema struct {
Expand Down Expand Up @@ -97,7 +98,10 @@ func (e *Endpoint) Valid() error {
}

type ServiceConfig struct {
ServiceInfo
Name string `json:"name"`
Id string `json:"id"`
Description string `json:"description"`
Version string `json:"version"`
Schema ServiceSchema `json:"schema"`
Endpoint Endpoint `json:"endpoint"`
StatusHandler func(Endpoint) interface{}
Expand Down Expand Up @@ -191,10 +195,7 @@ func (svc *ServiceImpl) _addInternalHandler(nc *Conn, verb ServiceVerb, kind str
// NOTE we can do an OpenAPI version as well, but looking at it it was very involved. So I think keep simple version and
// also have a version that talkes full blown OpenAPI spec and we can pull these things out.
func (nc *Conn) AddService(config ServiceConfig) (Service, error) {
if err := config.ServiceInfo.Valid(); err != nil {
return nil, err
}
if err := config.Endpoint.Valid(); err != nil {
if err := config.Valid(); err != nil {
return nil, err
}

Expand Down Expand Up @@ -222,8 +223,16 @@ func (nc *Conn) AddService(config ServiceConfig) (Service, error) {
return nil, err
}

info := &ServiceInfo{
Name: config.Name,
Id: config.Id,
Description: config.Description,
Version: config.Version,
Subject: config.Endpoint.Subject,
}

infoHandler := func(m *Msg) {
response, _ := json.MarshalIndent(config.ServiceInfo, "", " ")
response, _ := json.MarshalIndent(info, "", " ")
m.Respond(response)
}

Expand Down Expand Up @@ -257,7 +266,7 @@ func (nc *Conn) AddService(config ServiceConfig) (Service, error) {
}
}

svc.stats.ID = svc.ServiceInfo.Id
svc.stats.ID = svc.Id
svc.stats.Started = time.Now()
return svc, nil
}
Expand Down
13 changes: 8 additions & 5 deletions service_test.go
Expand Up @@ -53,11 +53,9 @@ func TestServiceBasics(t *testing.T) {

// Create 5 ServiceImpl responders.
config := ServiceConfig{
ServiceInfo: ServiceInfo{
Name: "CoolAddService",
Version: "v0.1",
Description: "Add things together",
},
Name: "CoolAddService",
Version: "v0.1",
Description: "Add things together",
Endpoint: Endpoint{
Subject: "svc.add",
Handler: doAdd,
Expand Down Expand Up @@ -102,6 +100,11 @@ func TestServiceBasics(t *testing.T) {
if err != nil {
t.Fatalf("Expected a response, got %v", err)
}
inf := ServiceInfo{}
json.Unmarshal(info.Data, &inf)
if inf.Subject != "svc.add" {
t.Fatalf("expected service subject to be srv.add: %s", inf.Subject)
}
fmt.Printf("\ninfo response:\n%s\n\n", info.Data)

// Get stats for all the nodes. Multiple responses.
Expand Down

0 comments on commit 43050b2

Please sign in to comment.