From 46d210ad2692dc571759e27abb4363b53787b9df Mon Sep 17 00:00:00 2001 From: Nam Huynh Date: Tue, 17 Aug 2021 14:29:47 +0700 Subject: [PATCH] add targets to init --- audit.go | 6 ++++-- constant.go | 12 ------------ db_index.go | 8 +------- 3 files changed, 5 insertions(+), 21 deletions(-) diff --git a/audit.go b/audit.go index 5f4bea0..cec6ae9 100644 --- a/audit.go +++ b/audit.go @@ -17,6 +17,8 @@ type MongoDBConfig struct { type Config struct { // Source of server, e.g: selly Source string + // Targets: staff, article, ... + Targets []string // MongoDB config, for save documents MongoDB MongoDBConfig } @@ -31,8 +33,8 @@ var s *Service // NewInstance ... func NewInstance(config Config) error { - if config.Source == "" || config.MongoDB.Host == "" { - return errors.New("please provide all necessary information: source, mongodb") + if config.Source == "" || len(config.Targets) == 0 || config.MongoDB.Host == "" { + return errors.New("please provide all necessary information: source, targets, mongodb") } // Connect MongoDB diff --git a/constant.go b/constant.go index 8db5881..f81b44a 100644 --- a/constant.go +++ b/constant.go @@ -10,15 +10,3 @@ const ( ActionUpdate = "update" ActionUpdatePermissions = "update-permissions" ) - -// List targets -const ( - TargetSellyStaff = "staffs" - TargetSellyStaffRole = "staff-roles" -) - -// SellyTargets ... -var SellyTargets = []string{ - TargetSellyStaff, - TargetSellyStaffRole, -} diff --git a/db_index.go b/db_index.go index bc251b5..4ff1dd0 100644 --- a/db_index.go +++ b/db_index.go @@ -5,17 +5,11 @@ import ( ) func (s Service) indexDB() { - // Get list targets - var targets = make([]string, 0) - if s.Source == SourceSelly { - targets = SellyTargets - } - // Index key commonIndex := mongodb.NewIndexKey("source", "target", "targetId") // Index all allowed sources - for _, target := range targets { + for _, target := range s.Targets { mongodb.CreateIndex(getColName(target), commonIndex) } }