2021-11-08 04:53:03 +00:00
|
|
|
package usermngmt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/Selly-Modules/mongodb"
|
2021-11-10 04:01:39 +00:00
|
|
|
"github.com/Selly-Modules/usermngmt/database"
|
2021-11-10 02:54:49 +00:00
|
|
|
"github.com/Selly-Modules/usermngmt/role"
|
|
|
|
"github.com/Selly-Modules/usermngmt/user"
|
2021-11-08 04:53:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// MongoDBConfig ...
|
|
|
|
type MongoDBConfig struct {
|
|
|
|
Host, User, Password, DBName, Mechanism, Source string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Config ...
|
|
|
|
type Config struct {
|
|
|
|
// MongoDB config, for save documents
|
|
|
|
MongoDB MongoDBConfig
|
|
|
|
// Table prefix, each service has its own prefix
|
|
|
|
TablePrefix string
|
|
|
|
}
|
|
|
|
|
2021-11-10 02:54:49 +00:00
|
|
|
// Handler ...
|
|
|
|
type Handler struct {
|
|
|
|
User user.Handle
|
|
|
|
Role role.Handle
|
|
|
|
}
|
|
|
|
|
2021-11-08 04:53:03 +00:00
|
|
|
// Service ...
|
|
|
|
type Service struct {
|
2021-11-10 02:54:49 +00:00
|
|
|
config Config
|
|
|
|
handler Handler
|
2021-11-08 04:53:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var s *Service
|
|
|
|
|
|
|
|
// Init ...
|
|
|
|
func Init(config Config) (*Service, error) {
|
2021-11-08 10:04:01 +00:00
|
|
|
if config.MongoDB.Host == "" {
|
2021-11-08 04:53:03 +00:00
|
|
|
return nil, errors.New("please provide all necessary information for init user")
|
|
|
|
}
|
|
|
|
|
2021-11-08 11:21:52 +00:00
|
|
|
// If prefixTable is empty then it is usermngmt
|
|
|
|
if config.TablePrefix == "" {
|
|
|
|
config.TablePrefix = tablePrefixDefault
|
|
|
|
}
|
|
|
|
|
2021-11-08 04:53:03 +00:00
|
|
|
// Connect MongoDB
|
|
|
|
db, err := mongodb.Connect(
|
|
|
|
config.MongoDB.Host,
|
|
|
|
config.MongoDB.User,
|
|
|
|
config.MongoDB.Password,
|
|
|
|
config.MongoDB.DBName,
|
|
|
|
config.MongoDB.Mechanism,
|
|
|
|
config.MongoDB.Source,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Cannot init module User MANAGEMENT", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-11-10 04:01:39 +00:00
|
|
|
// Set database
|
|
|
|
database.Set(db, config.TablePrefix)
|
|
|
|
|
2021-11-08 04:53:03 +00:00
|
|
|
s = &Service{
|
2021-11-10 02:54:49 +00:00
|
|
|
config: config,
|
2021-11-10 04:01:39 +00:00
|
|
|
handler: Handler{
|
|
|
|
User: user.Handle{},
|
|
|
|
Role: role.Handle{},
|
2021-11-10 02:54:49 +00:00
|
|
|
},
|
2021-11-08 04:53:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetInstance ...
|
|
|
|
func GetInstance() *Service {
|
|
|
|
return s
|
|
|
|
}
|