Skip to content

Commit

Permalink
refactor: manager server new instance (#464)
Browse files Browse the repository at this point in the history
Signed-off-by: Gaius <gaius.qi@gmail.com>
  • Loading branch information
gaius-qi committed Jul 19, 2021
1 parent 7659def commit a5e9510
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 66 deletions.
11 changes: 2 additions & 9 deletions manager/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,10 @@ func New(cfg *config.Config) (*Server, error) {
searcher := searcher.New()

// Initialize REST service
restService := service.NewREST(
service.WithDatabase(db),
service.WithCache(cache),
)
restService := service.NewREST(db, cache)

// Initialize GRPC service
grpcService := service.NewGRPC(
service.GRPCWithDatabase(db),
service.GRPCWithCache(cache),
service.GRPCWithSearcher(searcher),
)
grpcService := service.NewGRPC(db, cache, searcher)

// Initialize router
router, err := initRouter(cfg.Verbose, restService)
Expand Down
30 changes: 5 additions & 25 deletions manager/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,31 +64,11 @@ type rest struct {
cache *cache.Cache
}

// Option is a functional option for rest
type Option func(s *rest)

// WithDatabase set the database client
func WithDatabase(database *database.Database) Option {
return func(s *rest) {
s.db = database.DB
s.rdb = database.RDB
}
}

// WithCache set the cache client
func WithCache(cache *cache.Cache) Option {
return func(s *rest) {
s.cache = cache
}
}

// NewREST returns a new REST instence
func NewREST(options ...Option) REST {
s := &rest{}

for _, opt := range options {
opt(s)
func NewREST(database *database.Database, cache *cache.Cache) REST {
return &rest{
db: database.DB,
rdb: database.RDB,
cache: cache,
}

return s
}
38 changes: 6 additions & 32 deletions manager/service/service_grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,40 +27,14 @@ type GRPC struct {
searcher searcher.Searcher
}

// Option is a functional option for rest
type GRPCOption func(s *GRPC)

// GRPCWithDatabase set the database client
func GRPCWithDatabase(database *database.Database) GRPCOption {
return func(s *GRPC) {
s.db = database.DB
s.rdb = database.RDB
}
}

// GRPCWithCache set the cache client
func GRPCWithCache(cache *cache.Cache) GRPCOption {
return func(s *GRPC) {
s.cache = cache
}
}

// GRPCWithSearcher set search client
func GRPCWithSearcher(searcher searcher.Searcher) GRPCOption {
return func(s *GRPC) {
s.searcher = searcher
}
}

// NewREST returns a new REST instence
func NewGRPC(options ...GRPCOption) *GRPC {
s := &GRPC{}

for _, opt := range options {
opt(s)
func NewGRPC(database *database.Database, cache *cache.Cache, searcher searcher.Searcher) *GRPC {
return &GRPC{
db: database.DB,
rdb: database.RDB,
cache: cache,
searcher: searcher,
}

return s
}

func (s *GRPC) GetCDN(ctx context.Context, req *manager.GetCDNRequest) (*manager.CDN, error) {
Expand Down

0 comments on commit a5e9510

Please sign in to comment.