1
0
mirror of https://github.com/go-micro/go-micro.git synced 2025-06-12 22:07:47 +02:00
Files
go-micro/services/helloworld/helloworld.go
2021-12-21 13:18:29 +00:00

68 lines
1.3 KiB
Go
Executable File

package helloworld
import (
"go.m3o.com/client"
)
func NewHelloworldService(token string) *HelloworldService {
return &HelloworldService{
client: client.NewClient(&client.Options{
Token: token,
}),
}
}
type HelloworldService struct {
client *client.Client
}
// Call returns a personalised "Hello $name" response
func (t *HelloworldService) Call(request *CallRequest) (*CallResponse, error) {
rsp := &CallResponse{}
return rsp, t.client.Call("helloworld", "Call", request, rsp)
}
// Stream returns a stream of "Hello $name" responses
func (t *HelloworldService) Stream(request *StreamRequest) (*StreamResponseStream, error) {
stream, err := t.client.Stream("helloworld", "Stream", request)
if err != nil {
return nil, err
}
return &StreamResponseStream{
stream: stream,
}, nil
}
type StreamResponseStream struct {
stream *client.Stream
}
func (t *StreamResponseStream) Recv() (*StreamResponse, error) {
var rsp StreamResponse
if err := t.stream.Recv(&rsp); err != nil {
return nil, err
}
return &rsp, nil
}
type CallRequest struct {
Name string `json:"name"`
}
type CallResponse struct {
Message string `json:"message"`
}
type StreamRequest struct {
// the number of messages to send back
Messages int64 `json:"messages,string"`
Name string `json:"name"`
}
type StreamResponse struct {
Message string `json:"message"`
}