-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
52 lines (41 loc) · 1.24 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
package main
import (
"context"
"fmt"
"net/http"
"os"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/grpclog"
bothan "github.com/bandprotocol/bothan/bothan-api/client/go-client/proto/bothan/v1"
)
func main() {
grpcEndpoint := os.Getenv("GRPC_ENDPOINT")
if grpcEndpoint == "" {
grpcEndpoint = "localhost:50051"
}
proxyEndpoint := os.Getenv("PROXY_ENDPOINT")
if proxyEndpoint == "" {
proxyEndpoint = "localhost:8080"
}
if err := run(grpcEndpoint, proxyEndpoint); err != nil {
grpclog.Fatal(err)
}
}
func run(grpcEndpoint, proxyEndpoint string) 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 accessibly
mux := runtime.NewServeMux()
opts := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}
err := bothan.RegisterBothanServiceHandlerFromEndpoint(ctx, mux, grpcEndpoint, opts)
if err != nil {
return err
}
fmt.Println("Server running on", proxyEndpoint)
// Start an HTTP server (and proxy calls to gRPC server endpoint)
return http.ListenAndServe(proxyEndpoint, mux)
}