-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
196 lines (167 loc) · 4.87 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package main
import (
"context"
"flag"
"fmt"
"mime"
"net"
"net/http"
"strconv"
"strings"
"time"
assetfs "github.com/elazarl/go-bindata-assetfs"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"
"google.golang.org/protobuf/encoding/protojson"
"github.com/uw-labs/go-mono/cmd/user-api/internal/repo"
"github.com/uw-labs/go-mono/cmd/user-api/internal/server"
"github.com/uw-labs/go-mono/cmd/user-api/third_party/swagger"
pkgctx "github.com/uw-labs/go-mono/pkg/context"
usersservicepb "github.com/uw-labs/go-mono/proto/gen/go/uwlabs/users/service/v1"
)
//go:generate cp ../../proto/gen/openapiv2/uwlabs/users/service/v1/service.swagger.json ./third_party/swagger/swagger.json
//go:generate go-bindata -pkg swagger -prefix third_party/swagger -nometadata -ignore bindata -o ./third_party/swagger/bindata.go ./third_party/swagger
var (
postgresURL = flag.String("postgres-url", "", "The URL of the postgres database to connect to.")
grpcPort = flag.Uint("grpc-port", 8080, "The port to serve the gRPC server on.")
gatewayPort = flag.Uint("grpc-gateway-port", 8081, "The port to serve the gRPC-Gateway on.")
adminUser = flag.String("admin-user", "admin", "The username of the admin user.")
adminPassword = flag.String("admin-password", "", "The password of the admin user.")
)
func main() {
flag.Parse()
logger := logrus.New()
logger.Formatter = &logrus.TextFormatter{
FullTimestamp: true,
TimestampFormat: time.StampMilli,
}
if *postgresURL == "" {
logger.Fatal("postgres-url must be specified")
}
if *adminPassword == "" {
logger.Fatal("admin-password must be specified")
}
err := run(logger, *postgresURL, *adminUser, *adminPassword, *grpcPort, *gatewayPort)
if err != nil {
logger.WithError(err).Fatal()
}
}
func run(logger *logrus.Logger, postgresURL, adminUser, adminPassword string, grpcPort, gatewayPort uint) (err error) {
ctx := pkgctx.WithSignalHandler(context.Background())
rp, err := repo.NewRepository(postgresURL, logger)
if err != nil {
return fmt.Errorf("create repository: %w", err)
}
defer func() {
cErr := rp.Close()
if err == nil {
err = cErr
}
}()
admin := &server.User{
Username: adminUser,
Password: adminPassword,
}
grpcAddr := ":" + strconv.Itoa(int(grpcPort))
lis, err := net.Listen("tcp", grpcAddr)
if err != nil {
return fmt.Errorf("starting TCP listener: %w", err)
}
srv := grpc.NewServer()
backend := &server.Server{
Logger: logger,
Repo: rp,
Admin: admin,
}
usersservicepb.RegisterUserReaderServiceServer(srv, backend)
usersservicepb.RegisterUserWriterServiceServer(srv, backend)
eg, ctx := errgroup.WithContext(ctx)
eg.Go(func() error {
return srv.Serve(lis)
})
eg.Go(func() error {
<-ctx.Done()
tCtx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
ok := make(chan struct{})
go func() {
srv.GracefulStop()
close(ok)
}()
select {
case <-tCtx.Done():
srv.Stop()
case <-ok:
}
return nil
})
cc, err := grpc.Dial(grpcAddr, grpc.WithInsecure(), grpc.WithBlock())
if err != nil {
return fmt.Errorf("dialling gRPC server: %w", err)
}
defer func() {
cErr := cc.Close()
if err == nil {
err = cErr
}
}()
jsonpb := &runtime.JSONPb{
MarshalOptions: protojson.MarshalOptions{
Indent: " ",
},
}
gwmux := runtime.NewServeMux(
runtime.WithMarshalerOption(runtime.MIMEWildcard, jsonpb),
)
err = usersservicepb.RegisterUserReaderServiceHandler(ctx, gwmux, cc)
if err != nil {
return fmt.Errorf("register gateway: %w", err)
}
err = usersservicepb.RegisterUserWriterServiceHandler(ctx, gwmux, cc)
if err != nil {
return fmt.Errorf("register gateway: %w", err)
}
err = mime.AddExtensionType(".svg", "image/svg+xml")
if err != nil {
return fmt.Errorf("register svg mime type: %w", err)
}
swaggerHandler := http.FileServer(&assetfs.AssetFS{
Asset: swagger.Asset,
AssetDir: swagger.AssetDir,
AssetInfo: swagger.AssetInfo,
})
gatewayAddr := ":" + strconv.Itoa(int(gatewayPort))
gwServer := &http.Server{
Addr: gatewayAddr,
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.URL.Path, "/v1") {
gwmux.ServeHTTP(w, r)
return
}
swaggerHandler.ServeHTTP(w, r)
}),
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
eg.Go(func() error {
<-ctx.Done()
tCtx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
err := gwServer.Shutdown(tCtx)
if err != nil {
return fmt.Errorf("shutdown gRPC gateway: %w", err)
}
return nil
})
eg.Go(func() error {
logger.Info("Serving gRPC-Gateway and Swagger Documentation on http://localhost:", gatewayPort)
err = gwServer.ListenAndServe()
if err != http.ErrServerClosed {
return fmt.Errorf("serve gRPC gateway: %w", err)
}
return nil
})
return eg.Wait()
}