| 1 |
// Copyright 2022-2023 The NATS Authors |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package micro_test |
| 15 |
|
| 16 |
import ( |
| 17 |
"fmt" |
| 18 |
"log" |
| 19 |
"strconv" |
| 20 |
"time" |
| 21 |
|
| 22 |
"github.com/nats-io/nats.go" |
| 23 |
"github.com/nats-io/nats.go/micro" |
| 24 |
) |
| 25 |
|
| 26 |
func Example() { |
| 27 |
nc, err := nats.Connect("127.0.0.1:4222") |
| 28 |
if err != nil { |
| 29 |
log.Fatal(err) |
| 30 |
} |
| 31 |
defer nc.Close() |
| 32 |
|
| 33 |
// endpoint handler - in this case, HandlerFunc is used, |
| 34 |
// which is a built-in implementation of Handler interface |
| 35 |
echoHandler := func(req micro.Request) { |
| 36 |
req.Respond(req.Data()) |
| 37 |
} |
| 38 |
|
| 39 |
// second endpoint |
| 40 |
incrementHandler := func(req micro.Request) { |
| 41 |
val, err := strconv.Atoi(string(req.Data())) |
| 42 |
if err != nil { |
| 43 |
req.Error("400", "request data should be a number", nil) |
| 44 |
return |
| 45 |
} |
| 46 |
|
| 47 |
responseData := val + 1 |
| 48 |
req.Respond([]byte(strconv.Itoa(responseData))) |
| 49 |
} |
| 50 |
|
| 51 |
// third endpoint |
| 52 |
multiplyHandler := func(req micro.Request) { |
| 53 |
val, err := strconv.Atoi(string(req.Data())) |
| 54 |
if err != nil { |
| 55 |
req.Error("400", "request data should be a number", nil) |
| 56 |
return |
| 57 |
} |
| 58 |
|
| 59 |
responseData := val * 2 |
| 60 |
req.Respond([]byte(strconv.Itoa(responseData))) |
| 61 |
} |
| 62 |
|
| 63 |
config := micro.Config{ |
| 64 |
Name: "IncrementService", |
| 65 |
Version: "0.1.0", |
| 66 |
Description: "Increment numbers", |
| 67 |
|
| 68 |
// base handler - for simple services with single endpoints this is sufficient |
| 69 |
Endpoint: µ.EndpointConfig{ |
| 70 |
Subject: "echo", |
| 71 |
Handler: micro.HandlerFunc(echoHandler), |
| 72 |
}, |
| 73 |
} |
| 74 |
svc, err := micro.AddService(nc, config) |
| 75 |
if err != nil { |
| 76 |
log.Fatal(err) |
| 77 |
} |
| 78 |
defer svc.Stop() |
| 79 |
|
| 80 |
// add a group to aggregate endpoints under common prefix |
| 81 |
numbers := svc.AddGroup("numbers") |
| 82 |
|
| 83 |
// register endpoints in a group |
| 84 |
err = numbers.AddEndpoint("Increment", micro.HandlerFunc(incrementHandler)) |
| 85 |
if err != nil { |
| 86 |
log.Fatal(err) |
| 87 |
} |
| 88 |
err = numbers.AddEndpoint("Multiply", micro.HandlerFunc(multiplyHandler)) |
| 89 |
if err != nil { |
| 90 |
log.Fatal(err) |
| 91 |
} |
| 92 |
|
| 93 |
// send a request to a service |
| 94 |
resp, err := nc.Request("numbers.Increment", []byte("3"), 1*time.Second) |
| 95 |
if err != nil { |
| 96 |
log.Fatal(err) |
| 97 |
} |
| 98 |
responseVal, err := strconv.Atoi(string(resp.Data)) |
| 99 |
if err != nil { |
| 100 |
log.Fatal(err) |
| 101 |
} |
| 102 |
fmt.Println(responseVal) |
| 103 |
} |