mirror of
https://github.com/linka-cloud/grpc.git
synced 2024-11-21 18:36:25 +00:00
gorm db openned in options
This commit is contained in:
parent
3375b4f379
commit
ac9a81d3e7
@ -144,9 +144,11 @@ func WithKey(path string) Option {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func WithDB(db *gorm.DB) Option {
|
func WithDB(dialect string, args ...interface{}) Option {
|
||||||
|
db, err := gorm.Open(dialect, args...)
|
||||||
return func(o *options) {
|
return func(o *options) {
|
||||||
o.db = db
|
o.db = db
|
||||||
|
o.error = err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -236,6 +238,8 @@ type options struct {
|
|||||||
|
|
||||||
clientInterceptors []grpc.UnaryClientInterceptor
|
clientInterceptors []grpc.UnaryClientInterceptor
|
||||||
streamClientInterceptors []grpc.StreamClientInterceptor
|
streamClientInterceptors []grpc.StreamClientInterceptor
|
||||||
|
|
||||||
|
error error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *options) Name() string {
|
func (o *options) Name() string {
|
||||||
|
@ -8,6 +8,7 @@ import (
|
|||||||
grpcmiddleware "github.com/grpc-ecosystem/go-grpc-middleware"
|
grpcmiddleware "github.com/grpc-ecosystem/go-grpc-middleware"
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"go.uber.org/multierr"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -45,6 +46,9 @@ func newService(opts ...Option) (*service, error) {
|
|||||||
for _, f := range opts {
|
for _, f := range opts {
|
||||||
f(s.opts)
|
f(s.opts)
|
||||||
}
|
}
|
||||||
|
if s.opts.error != nil {
|
||||||
|
return nil, s.opts.error
|
||||||
|
}
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
@ -144,5 +148,9 @@ func (s *service) Stop() error {
|
|||||||
func (s *service) Close() error {
|
func (s *service) Close() error {
|
||||||
s.mu.Lock()
|
s.mu.Lock()
|
||||||
defer s.mu.Unlock()
|
defer s.mu.Unlock()
|
||||||
return s.Stop()
|
err := multierr.Combine(s.Stop())
|
||||||
|
if s.opts.db != nil {
|
||||||
|
err = multierr.Append(s.opts.db.Close(), err)
|
||||||
|
}
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user