-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
119 lines (101 loc) · 2.54 KB
/
Copy pathmain.go
File metadata and controls
119 lines (101 loc) · 2.54 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main
import (
"errors"
"log"
"net"
"net/http"
"os"
"sync"
"time"
"cloud.google.com/go/bigtable"
"github.com/golang/protobuf/ptypes/empty"
hello "github.com/weathersource/test-hello/proto"
context "golang.org/x/net/context"
"golang.org/x/oauth2/google"
"golang.org/x/oauth2/jwt"
"google.golang.org/api/option"
grpc "google.golang.org/grpc"
reflection "google.golang.org/grpc/reflection"
)
// server is used to implement hello.HelloService.
type server struct{}
type Clients struct {
cbtClient *bigtable.Client
}
var clients *Clients
// SetOnpoints implements gRPC hello.HelloService
func (s *server) SayHello(ctx context.Context, in *empty.Empty) (*hello.SayHelloResponse, error) {
// TODO: use clients to access BigTable
return &hello.SayHelloResponse{Msg: "hello"}, nil
}
// sayHealthy demonstrates health of the server by returning "Healthy" to http requests
func sayHealthy(w http.ResponseWriter, r *http.Request) {
message := "Healthy"
w.Write([]byte(message))
}
func main() {
time.Sleep(30 * time.Second)
// get service account credentials for BigTable
var cbtConfig *jwt.Config
if 0 != len(os.Getenv("password")) {
var err error
jsonStr := os.Getenv("password")
jsonKey := []byte(jsonStr)
cbtConfig, err = google.JWTConfigFromJSON(jsonKey, bigtable.Scope)
if err != nil {
log.Println(err)
os.Exit(1)
}
} else {
log.Println(errors.New("Failed to retrieve service account for BigTable client."))
os.Exit(1)
}
// create BigTable client
log.Println("Configuring cbtClient.")
cbtClient, err := bigtable.NewClient(
context.Background(),
"ws-microservices-production",
"legolas-production",
option.WithTokenSource(cbtConfig.TokenSource(context.Background())),
)
log.Println("SUCCESS configuring cbtClient.")
if err != nil {
log.Println(err)
os.Exit(1)
}
// set global clients
clients = &Clients{
cbtClient: cbtClient,
}
var wg sync.WaitGroup
wg.Add(1)
// start GRPC server
go func() {
defer wg.Done()
log.Println("Serve gRPC.")
lis, err := net.Listen("tcp", ":50051")
if err != nil {
log.Println(err)
return
}
s := grpc.NewServer(grpc.MaxRecvMsgSize(100 * 1024 * 1024))
hello.RegisterHelloServiceServer(s, &server{})
reflection.Register(s)
if err := s.Serve(lis); err != nil {
log.Println(err)
return
}
}()
// start health check HTTP server
go func() {
defer wg.Done()
log.Println("Serve heath check")
http.HandleFunc("/", sayHealthy)
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Println(err)
return
}
}()
wg.Wait()
os.Exit(1)
}