/
main.go
162 lines (145 loc) · 4.13 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"context"
"fmt"
"net"
"net/http"
_ "net/http/pprof"
"net/url"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"github.com/PacktPublishing/Hands-On-Software-Engineering-with-Golang/Chapter06/textindexer/index"
"github.com/PacktPublishing/Hands-On-Software-Engineering-with-Golang/Chapter06/textindexer/store/es"
memindex "github.com/PacktPublishing/Hands-On-Software-Engineering-with-Golang/Chapter06/textindexer/store/memory"
"github.com/PacktPublishing/Hands-On-Software-Engineering-with-Golang/Chapter09/linksrus/textindexerapi"
"github.com/PacktPublishing/Hands-On-Software-Engineering-with-Golang/Chapter09/linksrus/textindexerapi/proto"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"golang.org/x/xerrors"
"google.golang.org/grpc"
)
var (
appName = "linksrus-textindexer"
appSha = "populated-at-link-time"
logger *logrus.Entry
)
func main() {
host, _ := os.Hostname()
rootLogger := logrus.New()
rootLogger.SetFormatter(new(logrus.JSONFormatter))
logger = rootLogger.WithFields(logrus.Fields{
"app": appName,
"sha": appSha,
"host": host,
})
if err := makeApp().Run(os.Args); err != nil {
logger.WithField("err", err).Error("shutting down due to error")
_ = os.Stderr.Sync()
os.Exit(1)
}
}
func makeApp() *cli.App {
app := cli.NewApp()
app.Name = appName
app.Version = appSha
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "text-indexer-uri",
Value: "in-memory://",
EnvVar: "TEXT_INDEXER_URI",
Usage: "The URI for connecting to the text indexer (supported URIs: in-memory://, es://node1:9200,...,nodeN:9200)",
},
cli.IntFlag{
Name: "grpc-port",
Value: 8080,
EnvVar: "GRPC_PORT",
Usage: "The port for exposing the gRPC endpoints for accessing the text indexer",
},
cli.IntFlag{
Name: "pprof-port",
Value: 6060,
EnvVar: "PPROF_PORT",
Usage: "The port for exposing pprof endpoints",
},
}
app.Action = runMain
return app
}
func runMain(appCtx *cli.Context) error {
var wg sync.WaitGroup
ctx, cancelFn := context.WithCancel(context.Background())
defer cancelFn()
indexer, err := getTextIndexer(appCtx.String("text-indexer-uri"))
if err != nil {
return err
}
// Start gRPC server
grpcListener, err := net.Listen("tcp", fmt.Sprintf(":%d", appCtx.Int("grpc-port")))
if err != nil {
return err
}
defer func() { _ = grpcListener.Close() }()
wg.Add(1)
go func() {
defer wg.Done()
srv := grpc.NewServer()
proto.RegisterTextIndexerServer(srv, textindexerapi.NewTextIndexerServer(indexer))
logger.WithField("port", appCtx.Int("grpc-port")).Info("listening for gRPC connections")
_ = srv.Serve(grpcListener)
}()
// Start pprof server
pprofListener, err := net.Listen("tcp", fmt.Sprintf(":%d", appCtx.Int("pprof-port")))
if err != nil {
return err
}
defer func() { _ = pprofListener.Close() }()
wg.Add(1)
go func() {
defer wg.Done()
logger.WithField("port", appCtx.Int("pprof-port")).Info("listening for pprof requests")
srv := new(http.Server)
_ = srv.Serve(pprofListener)
}()
// Start signal watcher
go func() {
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGHUP)
select {
case s := <-sigCh:
logger.WithField("signal", s.String()).Infof("shutting down due to signal")
_ = grpcListener.Close()
_ = pprofListener.Close()
cancelFn()
case <-ctx.Done():
}
}()
// Keep running until we receive a signal
wg.Wait()
return nil
}
func getTextIndexer(textIndexerURI string) (index.Indexer, error) {
if textIndexerURI == "" {
return nil, xerrors.Errorf("text indexer URI must be specified with --text-indexer-uri")
}
uri, err := url.Parse(textIndexerURI)
if err != nil {
return nil, xerrors.Errorf("could not parse text indexer URI: %w", err)
}
switch uri.Scheme {
case "in-memory":
logger.Info("using in-memory indexer")
return memindex.NewInMemoryBleveIndexer()
case "es":
nodes := strings.Split(uri.Host, ",")
for i := 0; i < len(nodes); i++ {
nodes[i] = "http://" + nodes[i]
}
logger.Info("using ES indexer")
return es.NewElasticSearchIndexer(nodes, false)
default:
return nil, xerrors.Errorf("unsupported link graph URI scheme: %q", uri.Scheme)
}
}