-
Notifications
You must be signed in to change notification settings - Fork 111
/
rest.go
53 lines (43 loc) · 1.39 KB
/
rest.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
package rest
import (
"fmt"
"net/http"
"context"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
log "github.com/sirupsen/logrus"
gw "github.com/chef/automate/api/interservice/ingest"
"github.com/chef/automate/components/ingest-service/serveropts"
)
// Spawn starts a gRPC REST Client listening on the provided port and host,
// it will forward the requests to the provided (gRPC Server) endpoint
//
// You can start a client in a goroutine to work independently like:
// ```
// go rest.Spawn("localhost", "2194", "my-grpc-server.example.com:2191")
// ```
//
// Maybe even spawn multiple clients
func Spawn(endpoint string, conf *serveropts.Opts) error {
uri := fmt.Sprintf("%s:%d", conf.Host, conf.RestPort)
log.WithFields(log.Fields{
"uri": uri,
"endpoint": endpoint,
}).Info("Starting gRPC REST Client")
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
mux := runtime.NewServeMux(
runtime.WithMarshalerOption(runtime.MIMEWildcard, &runtime.JSONPb{OrigName: true, EmitDefaults: true}))
opts := conf.ConnFactory.DialOptions("ingest-service")
err := gw.RegisterChefIngesterServiceHandlerFromEndpoint(ctx, mux, endpoint, opts)
if err != nil {
log.Error(err)
return err
}
err = gw.RegisterJobSchedulerServiceHandlerFromEndpoint(ctx, mux, endpoint, opts)
if err != nil {
log.Error(err)
return err
}
return http.ListenAndServe(uri, mux)
}