Skip to content

Commit

Permalink
[ADDED] Support for multi-endpoint services
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrpio committed Jan 4, 2023
1 parent d50c137 commit a8d695d
Show file tree
Hide file tree
Showing 5 changed files with 417 additions and 206 deletions.
13 changes: 8 additions & 5 deletions micro/example_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@ func ExampleHandler() {
rec := rectangle{10, 5}

config := micro.Config{
Name: "RectangleAreaService",
Version: "0.1.0",
Endpoint: micro.Endpoint{
Handler: rec,
Subject: "rectangle.area",
Name: "RectangleAreaService",
Version: "0.1.0",
RootSubject: "area",
Endpoints: map[string]micro.Endpoint{
"Rectangle": {
Subject: "rec",
Handler: rec,
},
},
}
svc, err := micro.AddService(nc, config)
Expand Down
31 changes: 25 additions & 6 deletions micro/example_package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func Example() {
}
defer nc.Close()

// service handler - in this case, HandlerFunc is used,
// endpoint handler - in this case, HandlerFunc is used,
// which is a built-in implementation of Handler interface
incrementHandler := func(req micro.Request) {
val, err := strconv.Atoi(string(req.Data()))
Expand All @@ -43,15 +43,34 @@ func Example() {
req.Respond([]byte(strconv.Itoa(responseData)))
}

// second endpoint
multiply := func(req micro.Request) {
val, err := strconv.Atoi(string(req.Data()))
if err != nil {
req.Error("400", "request data should be a number", nil)
return
}

responseData := val * 2
req.Respond([]byte(strconv.Itoa(responseData)))
}

config := micro.Config{
Name: "IncrementService",
Version: "0.1.0",
Description: "Increment numbers",
Endpoint: micro.Endpoint{
// service handler
Handler: micro.HandlerFunc(incrementHandler),
// a unique subject serving as a service endpoint
Subject: "numbers.increment",
RootSubject: "numbers",
Endpoints: map[string]micro.Endpoint{
"Increment": {
// service handler
Handler: micro.HandlerFunc(incrementHandler),
// a unique subject serving as a service endpoint
Subject: "increment",
},
"Multiply": {
Handler: micro.HandlerFunc(multiply),
Subject: "multiply",
},
},
}
// Multiple instances of the servcice with the same name can be created.
Expand Down
78 changes: 48 additions & 30 deletions micro/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@ func ExampleAddService() {
Name: "EchoService",
Version: "1.0.0",
Description: "Send back what you receive",
Endpoint: micro.Endpoint{
Subject: "echo",
Handler: micro.HandlerFunc(echoHandler),
RootSubject: "svc",
Endpoints: map[string]micro.Endpoint{
"Echo": {
Subject: "echo",
Handler: micro.HandlerFunc(echoHandler),
},
},

// DoneHandler can be set to customize behavior on stopping a service.
Expand Down Expand Up @@ -70,10 +73,13 @@ func ExampleService_Info() {
defer nc.Close()

config := micro.Config{
Name: "EchoService",
Endpoint: micro.Endpoint{
Subject: "echo",
Handler: micro.HandlerFunc(func(micro.Request) {}),
Name: "EchoService",
RootSubject: "svc",
Endpoints: map[string]micro.Endpoint{
"Echo": {
Subject: "echo",
Handler: micro.HandlerFunc(func(r micro.Request) {}),
},
},
}

Expand All @@ -86,7 +92,7 @@ func ExampleService_Info() {
fmt.Println(info.Name)
fmt.Println(info.Description)
fmt.Println(info.Version)
fmt.Println(info.Subject)
fmt.Println(info.RootSubject)
}

func ExampleService_Stats() {
Expand All @@ -97,11 +103,14 @@ func ExampleService_Stats() {
defer nc.Close()

config := micro.Config{
Name: "EchoService",
Version: "0.1.0",
Endpoint: micro.Endpoint{
Subject: "echo",
Handler: micro.HandlerFunc(func(micro.Request) {}),
Name: "EchoService",
Version: "0.1.0",
RootSubject: "svc",
Endpoints: map[string]micro.Endpoint{
"Echo": {
Subject: "echo",
Handler: micro.HandlerFunc(func(r micro.Request) {}),
},
},
}

Expand All @@ -110,8 +119,8 @@ func ExampleService_Stats() {
// stats of a service instance
stats := srv.Stats()

fmt.Println(stats.AverageProcessingTime)
fmt.Println(stats.ProcessingTime)
fmt.Println(stats.Endpoints["Echo"].AverageProcessingTime)
fmt.Println(stats.Endpoints["Echo"].ProcessingTime)

}

Expand All @@ -123,11 +132,14 @@ func ExampleService_Stop() {
defer nc.Close()

config := micro.Config{
Name: "EchoService",
Version: "0.1.0",
Endpoint: micro.Endpoint{
Subject: "echo",
Handler: micro.HandlerFunc(func(micro.Request) {}),
Name: "EchoService",
Version: "0.1.0",
RootSubject: "svc",
Endpoints: map[string]micro.Endpoint{
"Echo": {
Subject: "echo",
Handler: micro.HandlerFunc(func(r micro.Request) {}),
},
},
}

Expand All @@ -154,11 +166,14 @@ func ExampleService_Stopped() {
defer nc.Close()

config := micro.Config{
Name: "EchoService",
Version: "0.1.0",
Endpoint: micro.Endpoint{
Subject: "echo",
Handler: micro.HandlerFunc(func(micro.Request) {}),
Name: "EchoService",
Version: "0.1.0",
RootSubject: "svc",
Endpoints: map[string]micro.Endpoint{
"Echo": {
Subject: "echo",
Handler: micro.HandlerFunc(func(r micro.Request) {}),
},
},
}

Expand All @@ -183,11 +198,14 @@ func ExampleService_Reset() {
defer nc.Close()

config := micro.Config{
Name: "EchoService",
Version: "0.1.0",
Endpoint: micro.Endpoint{
Subject: "echo",
Handler: micro.HandlerFunc(func(micro.Request) {}),
Name: "EchoService",
Version: "0.1.0",
RootSubject: "svc",
Endpoints: map[string]micro.Endpoint{
"Echo": {
Subject: "echo",
Handler: micro.HandlerFunc(func(r micro.Request) {}),
},
},
}

Expand Down
Loading

0 comments on commit a8d695d

Please sign in to comment.