/
main.go
79 lines (61 loc) · 1.25 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"context"
"fmt"
"github.com/golang/protobuf/ptypes/empty"
"log"
"sync"
"github.com/go-chi/chi"
"github.com/micro/go-micro"
"github.com/micro/go-micro/web"
"github.com/webhippie/protoc-gen-microweb/examples/greeter/proto"
)
func main() {
var wg sync.WaitGroup
wg.Add(2)
grpc := micro.NewService(
micro.Name("go.micro.api.hello"),
)
proto.RegisterGreeterHandler(
grpc.Server(),
&Greeter{},
)
go func(svc micro.Service) {
defer wg.Done()
svc.Init()
if err := svc.Run(); err != nil {
log.Fatal(err)
}
}(grpc)
http := web.NewService(
web.Name("go.micro.web.hello"),
)
mux := chi.NewMux()
mux.Route("/", func(r chi.Router) {
proto.RegisterGreeterWeb(
r,
&Greeter{},
)
})
go func(svc web.Service) {
defer wg.Done()
svc.Init()
if err := svc.Run(); err != nil {
log.Fatal(err)
}
}(http)
wg.Wait()
}
type Greeter struct{}
func (g *Greeter) Say(ctx context.Context, in *proto.SayRequest, out *proto.SayResponse) error {
name := "World"
if in.Name != "" {
name = in.Name
}
out.Message = fmt.Sprintf("Hello %s!", name)
return nil
}
func (g *Greeter) SayAnything(ctx context.Context, in *empty.Empty, out *proto.SayResponse) error {
out.Message = "Saying Anything!"
return nil
}