usermngmt/usermngmt.go

89 lines
1.7 KiB
Go
Raw Permalink Normal View History

2021-11-08 04:53:03 +00:00
package usermngmt
import (
"errors"
"fmt"
2021-11-16 05:16:06 +00:00
"github.com/Selly-Modules/logger"
2021-11-08 04:53:03 +00:00
"github.com/Selly-Modules/mongodb"
2022-02-24 08:17:15 +00:00
2021-11-11 03:20:08 +00:00
"github.com/Selly-Modules/usermngmt/cache"
2021-12-07 07:28:52 +00:00
configMoudle "github.com/Selly-Modules/usermngmt/config"
2021-11-10 04:01:39 +00:00
"github.com/Selly-Modules/usermngmt/database"
2021-11-10 05:07:58 +00:00
"github.com/Selly-Modules/usermngmt/internal"
2021-11-08 04:53:03 +00:00
)
2021-12-08 04:42:31 +00:00
// RedisConfig ...
type RedisConfig struct {
URI, Password string
}
2021-11-08 04:53:03 +00:00
// Config ...
type Config struct {
// MongoDB config, for save documents
2022-02-24 08:17:15 +00:00
MongoDB mongodb.Config
2021-12-08 04:42:31 +00:00
// Redis
Redis RedisConfig
2021-11-08 04:53:03 +00:00
// Table prefix, each service has its own prefix
TablePrefix string
2021-12-07 07:28:52 +00:00
// Email is unique
EmailIsUnique bool
// phone number is unique
PhoneNumberIsUnique bool
2021-11-08 04:53:03 +00:00
}
// Service ...
type Service struct {
2021-11-10 04:42:23 +00:00
config Config
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 == "" {
2021-11-10 05:07:58 +00:00
config.TablePrefix = internal.TablePrefixDefault
2021-11-08 11:21:52 +00:00
}
2021-11-08 04:53:03 +00:00
// Connect MongoDB
2022-02-24 08:17:15 +00:00
db, err := mongodb.Connect(config.MongoDB)
2021-11-08 04:53:03 +00:00
if err != nil {
fmt.Println("Cannot init module User MANAGEMENT", err)
return nil, err
}
2021-11-24 09:28:01 +00:00
logger.Init(fmt.Sprintf("%s-usermngmt", config.TablePrefix), "")
2021-11-16 05:16:06 +00:00
2021-11-10 04:01:39 +00:00
// Set database
database.Set(db, config.TablePrefix)
2021-12-07 07:28:52 +00:00
// Set config module
configMoudle.Set(&configMoudle.Configuration{
EmailIsUnique: config.EmailIsUnique,
PhoneNumberIsUnique: config.PhoneNumberIsUnique,
})
2021-11-16 04:11:04 +00:00
// Init cache
2021-12-08 04:42:31 +00:00
cache.Init(config.Redis.URI, config.Redis.Password)
2021-11-16 04:11:04 +00:00
2021-11-08 04:53:03 +00:00
s = &Service{
2021-11-10 02:54:49 +00:00
config: config,
2021-11-08 04:53:03 +00:00
}
return s, nil
}
// GetInstance ...
func GetInstance() *Service {
return s
}