-
Notifications
You must be signed in to change notification settings - Fork 40
/
http_wrapper.go
51 lines (41 loc) · 1.28 KB
/
http_wrapper.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
package main
import (
"context" // Use "golang.org/x/net/context" for Golang version <= 1.6
"flag"
"fmt"
"net/http"
"os"
"github.com/golang/glog"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"google.golang.org/grpc"
gw "github.com/Yelp/nrtsearch/grpc-gateway"
)
var (
// command-line options:
// gRPC server endpoint
grpc_server_hostport = fmt.Sprintf("localhost:%s", os.Args[1])
rest_server_hostport = fmt.Sprintf(":%s", os.Args[2])
grpcServerEndpoint = flag.String("grpc-server-endpoint", grpc_server_hostport, "gRPC server endpoint")
)
func run() error {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
// Register gRPC server endpoint
// Note: Make sure the gRPC server is running properly and accessible
mux := runtime.NewServeMux()
opts := []grpc.DialOption{grpc.WithInsecure(), grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(104_857_600))}
err := gw.RegisterLuceneServerHandlerFromEndpoint(ctx, mux, *grpcServerEndpoint, opts)
if err != nil {
return err
}
// Start HTTP server (and proxy calls to gRPC server endpoint)
return http.ListenAndServe(rest_server_hostport, mux)
}
func main() {
flag.Parse()
defer glog.Flush()
if err := run(); err != nil {
glog.Fatal(err)
}
}