-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
68 lines (56 loc) · 1.52 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
package main
import (
"os"
"fmt"
"log"
"net"
"context"
"google.golang.org/grpc"
pb "github.com/fireacademy/golden-gate/grpc"
. "github.com/fireacademy/golden-gate/redis"
telemetry "github.com/fireacademy/telemetry"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
)
type server struct {
pb.UnimplementedGoldenGateServer
}
func (s *server) RefreshAPIKeyData(ctx context.Context, r *pb.RefreshAPIKeyRequest) (*pb.RefreshAPIKeyReply, error) {
canBeUsed, origin, err := RefreshAPIKey(ctx, r.APIKey)
return &pb.RefreshAPIKeyReply{
CanBeUsed: canBeUsed,
Origin: origin,
}, err
}
func (s *server) BillCredits(ctx context.Context, r *pb.BillCreditsRequest) (*pb.EmptyReply, error) {
err := BillCreditsQuickly(ctx, r.APIKey, r.Credits)
return &pb.EmptyReply{}, err
}
func gRPCServer(port string) {
lis, err := net.Listen("tcp", fmt.Sprintf(":%s", port))
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer(
grpc.UnaryInterceptor(otelgrpc.UnaryServerInterceptor()),
grpc.StreamInterceptor(otelgrpc.StreamServerInterceptor()),
)
pb.RegisterGoldenGateServer(s, &server{})
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}
func getPort() string {
port := os.Getenv("GOLDEN_GATE_LISTEN_PORT")
if port == "" {
panic("GOLDEN_GATE_LISTEN_PORT not set.")
}
return port
}
func main() {
cleanup := telemetry.Initialize()
defer cleanup(context.Background())
SetupRedis()
SetupCheck()
port := getPort()
gRPCServer(port)
}