devicemngmt/devicemngt.go

70 lines
1.3 KiB
Go
Raw Normal View History

2021-11-05 07:21:57 +00:00
package devicemngmt
import (
"errors"
"fmt"
2021-11-24 09:26:03 +00:00
"github.com/Selly-Modules/logger"
2021-11-05 07:21:57 +00:00
"github.com/Selly-Modules/mongodb"
"go.mongodb.org/mongo-driver/mongo"
)
// Config ...
type Config struct {
// MongoDB config, for save documents
2022-02-24 08:12:04 +00:00
MongoDB mongodb.Config
2021-11-05 07:21:57 +00:00
// Table prefix, each service has its own prefix
TablePrefix string
}
// Service ...
type Service struct {
Config
DB *mongo.Database
}
var s *Service
// Init ...
2021-11-05 08:40:07 +00:00
func Init(config Config) (*Service, error) {
2021-11-08 10:23:03 +00:00
if config.MongoDB.Host == "" {
2021-11-05 08:40:07 +00:00
return nil, errors.New("please provide all necessary information for init device")
2021-11-05 07:21:57 +00:00
}
2021-11-10 10:08:39 +00:00
// If prefixTable is empty then it is devicemngmt
if config.TablePrefix == "" {
config.TablePrefix = tablePrefixDefault
}
2021-11-05 07:21:57 +00:00
// Connect MongoDB
2022-02-24 08:12:04 +00:00
db, err := mongodb.Connect(config.MongoDB)
2021-11-05 07:21:57 +00:00
if err != nil {
fmt.Println("Cannot init module DEVICE MANAGEMENT", err)
2021-11-05 08:40:07 +00:00
return nil, err
2021-11-05 07:21:57 +00:00
}
2021-11-24 09:26:03 +00:00
logger.Init(fmt.Sprintf("%s-devicemngmt", config.TablePrefix), "")
2021-11-05 07:21:57 +00:00
s = &Service{
Config: config,
DB: db,
}
2021-11-05 08:40:07 +00:00
return s, nil
2021-11-05 07:21:57 +00:00
}
// GetInstance ...
func GetInstance() *Service {
return s
}
2022-10-05 09:37:16 +00:00
// GetConnectOptions ...
func GetConnectOptions(Host, DBName string) mongodb.Config {
return mongodb.Config{
Host: Host,
DBName: DBName,
Standalone: &mongodb.ConnectStandaloneOpts{},
TLS: &mongodb.ConnectTLSOpts{},
}
}