/
main.go
83 lines (67 loc) · 2.25 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
package main
import (
"fmt"
"github.com/nats-io/stan.go"
log "github.com/sirupsen/logrus"
"go.virtualstaticvoid.com/eventinator/config"
"go.virtualstaticvoid.com/eventinator/metrics"
"go.virtualstaticvoid.com/eventinator/protobuf"
"go.virtualstaticvoid.com/eventinator/service"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"net"
"os"
"os/signal"
"syscall"
)
//go:generate protoc --proto_path protobuf --go_out=protobuf --go_opt=paths=source_relative --go-grpc_out=protobuf --go-grpc_opt=paths=source_relative api.proto
//go:generate protoc --proto_path protobuf --go_out=protobuf --go_opt=paths=source_relative eventinator.proto
//go:generate protoc --proto_path protobuf --go_out=protobuf --go_opt=paths=source_relative internal.proto
func main() {
cfg := config.ParseCommandLine()
log.SetFormatter(&log.TextFormatter{})
log.SetOutput(os.Stdout)
if cfg.VerboseLogging {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
}
log.Infof("Starting server on port %d", cfg.Port)
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", cfg.Port))
if err != nil {
log.Panicf("Failed %v", err)
}
metric := metrics.NewInstrumentation(cfg.MetricsEnabled)
metric.Serve(cfg.MetricsPort)
// connect to STAN
log.Infof("Connecting to NATS on %s", cfg.ServerURL)
conn, err := stan.Connect(cfg.ClusterId, cfg.ClientId, stan.NatsURL(cfg.ServerURL))
if err != nil {
log.Panicf("Failed to connect to NATS streaming server: %v", err)
}
defer conn.Close()
svc := service.NewService(conn, metric)
var opts []grpc.ServerOption
if cfg.Secure {
transCred, err := credentials.NewServerTLSFromFile(cfg.CertificateFile, cfg.CertificateKeyFile)
if err != nil {
log.Fatalf("Failed to generate credentials %v", err)
}
opts = []grpc.ServerOption{grpc.Creds(transCred)}
}
server := grpc.NewServer(opts...)
protobuf.RegisterAPIServer(server, svc)
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigChan
log.Info("Gracefully shutting down server...")
server.GracefulStop() // unblocks
}()
log.Info("Waiting for requests...")
err = server.Serve(listener) // blocking
if err != nil {
log.Panicf("Failed to start server: %v", err)
}
log.Info("Stopped server.")
}