/
serve.go
209 lines (178 loc) · 6.23 KB
/
serve.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
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
Copyright © 2023 Dhiraj Bokde dhirajsb@gmail.com
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
import (
"context"
"fmt"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/dhirajsb/ml-metadata-go-server/ml_metadata/proto"
"github.com/dhirajsb/ml-metadata-go-server/server"
"github.com/dhirajsb/ml-metadata-go-server/server/graph"
"github.com/golang/glog"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
"github.com/soheilhy/cmux"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"
"google.golang.org/grpc/health"
"google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/reflection"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"log"
"net"
"net/http"
"os"
"os/signal"
"syscall"
)
func InterceptorLogger(l *log.Logger) logging.Logger {
return logging.LoggerFunc(func(_ context.Context, lvl logging.Level, msg string, fields ...any) {
switch lvl {
case logging.LevelDebug:
msg = fmt.Sprintf("DEBUG :%v", msg)
case logging.LevelInfo:
msg = fmt.Sprintf("INFO :%v", msg)
case logging.LevelWarn:
msg = fmt.Sprintf("WARN :%v", msg)
case logging.LevelError:
msg = fmt.Sprintf("ERROR :%v", msg)
default:
panic(fmt.Sprintf("unknown level %v", lvl))
}
l.Println(append([]any{"msg", msg}, fields...))
})
}
var (
dbFile string
host = "localhost"
port int = 8080
// serveCmd represents the serve command
serveCmd = &cobra.Command{
Use: "serve",
Short: "Starts the ml-metadata go server",
Long: `This command launches the ml-metadata go server.
The server connects to a SQlite database. It supports options to customize the
location of the database file and the hostname and port where it listens.'`,
RunE: runServer,
}
)
func runServer(cmd *cobra.Command, args []string) error {
glog.Info("server started...")
// Create a channel to receive signals
signalChannel := make(chan os.Signal, 1)
// Notify the channel on SIGINT (Ctrl+C) and SIGTERM signals
signal.Notify(signalChannel, syscall.SIGINT, syscall.SIGTERM)
// connect to the DB using Gorm
db, err := NewDatabaseConnection(dbFile)
if err != nil {
log.Fatalf("db connection failed: %v", err)
}
// listen on host:port
listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", host, port))
if err != nil {
log.Fatalf("server listen failed: %v", err)
}
m := cmux.New(listener)
gqlListener := m.Match(cmux.HTTP1Fast())
grpcListener := m.MatchWithWriters(cmux.HTTP2MatchHeaderFieldSendSettings("content-type", "application/grpc"))
// gRPC server
grpcServer := grpcListenerServer(grpcListener, server.NewGrpcServer(db))
// GraphQL server
gqlServer := graphQlListenerServer(grpcListener, db)
// start cmux listeners
g := new(errgroup.Group)
g.Go(func() error {
glog.Info("starting gRPC server...")
return grpcServer.Serve(listener)
})
g.Go(func() error {
glog.Info("starting GraphQL server...")
return gqlServer.Serve(gqlListener)
})
g.Go(func() error {
return m.Serve()
})
go func() {
err = g.Wait()
// error starting server
if err != nil || err != http.ErrServerClosed || err != grpc.ErrServerStopped || err != cmux.ErrServerClosed {
glog.Errorf("server listener error: %v", err)
}
signalChannel <- syscall.SIGINT
}()
// Wait for a signal
receivedSignal := <-signalChannel
glog.Infof("received signal: %s\n", receivedSignal)
// Perform cleanup or other graceful shutdown actions here
glog.Info("shutting down services...")
grpcServer.Stop()
_ = gqlServer.Shutdown(context.Background())
// close DB
glog.Info("closing DB...")
sqlDB, err := db.DB()
if err != nil {
return fmt.Errorf("error accessing DB: %v", err)
}
err = sqlDB.Close()
if err != nil {
return fmt.Errorf("error closing DB: %v", err)
}
glog.Info("shutdown!")
return nil
}
func graphQlListenerServer(listener net.Listener, db *gorm.DB) *http.Server {
mux := http.NewServeMux()
srv := handler.NewDefaultServer(graph.NewExecutableSchema(graph.Config{Resolvers: &graph.Resolver{}}))
mux.Handle("/", playground.Handler("GraphQL playground", "/query"))
mux.Handle("/query", srv)
return &http.Server{Handler: mux}
}
func grpcListenerServer(listener net.Listener, server proto.MetadataStoreServiceServer) *grpc.Server {
// TODO map server options from flags
logger := log.New(os.Stderr, "", log.Ldate|log.Ltime|log.Lshortfile)
lopts := []logging.Option{
logging.WithLogOnEvents(logging.PayloadReceived, logging.PayloadSent, logging.StartCall, logging.FinishCall),
// Add any other option (check functions starting with logging.With).
}
opts := []grpc.ServerOption{
grpc.ChainUnaryInterceptor(
logging.UnaryServerInterceptor(InterceptorLogger(logger), lopts...),
),
grpc.ChainStreamInterceptor(
logging.StreamServerInterceptor(InterceptorLogger(logger), lopts...),
),
}
grpcServer := grpc.NewServer(opts...)
// simple health check
grpc_health_v1.RegisterHealthServer(grpcServer, health.NewServer())
proto.RegisterMetadataStoreServiceServer(grpcServer, server)
reflection.Register(grpcServer)
return grpcServer
}
func NewDatabaseConnection(dbFile string) (*gorm.DB, error) {
return gorm.Open(sqlite.Open(dbFile), &gorm.Config{})
}
func init() {
rootCmd.AddCommand(serveCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// serveCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
serveCmd.Flags().StringVarP(&dbFile, "db-file", "d", "metadata.sqlite.db", "Sqlite DB file")
serveCmd.Flags().StringVarP(&host, "hostname", "n", host, "Server listen hostname")
serveCmd.Flags().IntVarP(&port, "port", "p", port, "Server listen port")
}