-
Notifications
You must be signed in to change notification settings - Fork 52
/
main.go
132 lines (113 loc) · 4.23 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
// An example pubsub grpc server. This uses the aws message broker as its
// underlying messaging system.
import (
"context"
"fmt"
"net"
"strings"
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"google.golang.org/grpc"
"github.com/infobloxopen/atlas-app-toolkit/health"
"github.com/infobloxopen/atlas-app-toolkit/logging"
"github.com/infobloxopen/atlas-app-toolkit/server"
pubsub "github.com/infobloxopen/atlas-pubsub"
pubsubaws "github.com/infobloxopen/atlas-pubsub/aws"
pubsubgrpc "github.com/infobloxopen/atlas-pubsub/grpc"
)
var registeredPubSubServer = false
const pubSubNotReadyError = "PubSub Server is not ready!"
func main() {
logger := logging.New(viper.GetString("logging.level"))
if viper.GetBool("internal.enable") {
go func() {
if err := ServeInternal(logger); err != nil {
logger.Fatal(err)
}
}()
}
logger.Printf("starting aws pubsub server on port %s", viper.GetString("server.port"))
lis, err := net.Listen("tcp", fmt.Sprintf("%s:%s", viper.GetString("server.address"), viper.GetString("server.port")))
if err != nil {
logger.Fatalf("failed to listen: %v", err)
}
grpcServer := grpc.NewServer()
pubsubServer, err := newAWSPubSubServer(logger)
if err != nil {
logger.Fatalf("failed to create aws pubsub server: %v", err)
}
pubsubgrpc.RegisterPubSubServer(grpcServer, pubsubServer)
// We just setting global variable, because no race condition consequence is unacceptable
registeredPubSubServer = true
if err := grpcServer.Serve(lis); err != nil {
logger.Fatalf("failed to serve: %v", err)
}
}
// newAWSPubSubServer creates a new grpc PubSub server using the broker
// implementation for AWS
func newAWSPubSubServer(logger *logrus.Logger) (pubsubgrpc.PubSubServer, error) {
sess := session.Must(session.NewSessionWithOptions(session.Options{
AssumeRoleTokenProvider: stscreds.StdinTokenProvider,
SharedConfigState: session.SharedConfigEnable,
}))
// Checks to see if aws config credentials are valid
logger.Print("checking server for AWS permissions")
if err := pubsubaws.VerifyPermissions(sess, logger); err != nil {
logger.Fatalf("AWS permissions check failed: %v", err)
}
logger.Print("server has proper AWS permissions")
pubFactory := func(ctx context.Context, topic string) (pubsub.Publisher, error) {
return pubsubaws.NewPublisher(sess, topic, pubsubaws.PublishWithLogger(logger))
}
subFactory := func(ctx context.Context, topic, subscriptionID string) (pubsub.Subscriber, error) {
return pubsubaws.NewSubscriber(sess, topic, subscriptionID, pubsubaws.SubscribeWithLogger(logger))
}
return pubsubgrpc.NewPubSubServer(pubFactory, subFactory, pubsubgrpc.WithLogger(logger)), nil
}
// ServeInternal builds and runs the server that listens on InternalAddress
func ServeInternal(logger *logrus.Logger) error {
healthChecker := health.NewChecksHandler(
viper.GetString("internal.health"),
viper.GetString("internal.readiness"),
)
healthChecker.AddReadiness("PubSub Server ready check", pubSubServerReady)
s, err := server.NewServer(
// register our health checks
server.WithHealthChecks(healthChecker),
)
if err != nil {
return err
}
l, err := net.Listen("tcp", fmt.Sprintf("%s:%s", viper.GetString("internal.address"), viper.GetString("internal.port")))
if err != nil {
return err
}
logger.Debugf("serving internal http at %q", fmt.Sprintf("%s:%s", viper.GetString("internal.address"), viper.GetString("internal.port")))
return s.Serve(nil, l)
}
func init() {
pflag.Parse()
viper.BindPFlags(pflag.CommandLine)
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AddConfigPath(viper.GetString("config.source"))
if viper.GetString("config.file") != "" {
logrus.Infof("Serving from configuration file: %s", viper.GetString("config.file"))
viper.SetConfigName(viper.GetString("config.file"))
if err := viper.ReadInConfig(); err != nil {
logrus.Fatalf("cannot load configuration: %v", err)
}
} else {
logrus.Infof("Serving from default values, environment variables, and/or flags")
}
}
func pubSubServerReady() error {
if !registeredPubSubServer {
return fmt.Errorf(pubSubNotReadyError)
}
return nil
}