audit/audit.go

60 lines
940 B
Go
Raw Normal View History

2021-08-09 04:33:23 +00:00
package audit
import (
"errors"
"fmt"
2022-10-10 03:42:39 +00:00
"git.selly.red/Selly-Modules/mongodb"
2021-08-09 04:33:23 +00:00
"go.mongodb.org/mongo-driver/mongo"
)
// Config ...
type Config struct {
2021-08-17 07:29:47 +00:00
// Targets: staff, article, ...
Targets []string
2021-08-09 04:33:23 +00:00
// MongoDB config, for save documents
2022-03-14 03:52:29 +00:00
MongoDB mongodb.Config
2021-08-09 04:33:23 +00:00
}
// Service ...
type Service struct {
Config
DB *mongo.Database
}
var s *Service
// NewInstance ...
func NewInstance(config Config) error {
2021-08-17 08:54:06 +00:00
if len(config.Targets) == 0 || config.MongoDB.Host == "" {
return errors.New("please provide all necessary information: targets, mongodb")
2021-08-09 04:33:23 +00:00
}
// Connect MongoDB
2022-03-14 03:52:29 +00:00
db, err := mongodb.Connect(config.MongoDB)
2021-08-09 04:33:23 +00:00
if err != nil {
fmt.Println("Cannot init module AUDIT", err)
return err
}
s = &Service{
Config: config,
2021-10-08 08:10:50 +00:00
DB: db,
2021-08-09 04:33:23 +00:00
}
// index mongo
s.indexDB()
return nil
}
// GetInstance ...
func GetInstance() *Service {
return s
}
// getColName ...
func getColName(target string) string {
return target
}