vietguys/vietguys.go

83 lines
1.7 KiB
Go
Raw Normal View History

2021-08-09 04:41:57 +00:00
package vietguys
import (
"context"
"errors"
"fmt"
"net/http"
2022-10-10 03:16:28 +00:00
"git.selly.red/Selly-Modules/mongodb"
2021-08-09 04:41:57 +00:00
"go.mongodb.org/mongo-driver/mongo"
)
// MongoDBConfig ...
type MongoDBConfig struct {
Host, User, Password, DBName, Mechanism, Source string
}
// Config ...
type Config struct {
// Endpoint which will use to call api
Endpoint string
// For auth
User string
// For auth
Password string
// Brand name
From string
// MongoDB config, for save documents
MongoDB MongoDBConfig
// Limit send time per ip
IPMaxSendPerDay int
// Limit send time per phone number
PhoneMaxSendPerDay int
}
// Service ...
type Service struct {
Config
Client *http.Client
DB *mongo.Collection
}
var s *Service
var bgCtx = context.Background()
// NewInstance for using send sms method
func NewInstance(config Config) error {
if config.Endpoint == "" || config.User == "" || config.Password == "" || config.From == "" || config.MongoDB.Host == "" {
return errors.New("please provide all information that needed: endpoint, user, password, from, mongodb")
}
// Connect MongoDB
2022-10-10 04:50:23 +00:00
_, err := mongodb.Connect(mongodb.Config{
Host: config.MongoDB.Host,
DBName: config.MongoDB.DBName,
Monitor: nil,
TLS: nil,
Standalone: &mongodb.ConnectStandaloneOpts{
AuthMechanism: config.MongoDB.Mechanism,
AuthSource: config.MongoDB.Source,
Username: config.MongoDB.User,
Password: config.MongoDB.Password,
},
})
2021-08-09 04:41:57 +00:00
if err != nil {
fmt.Println("Cannot init module VIETGUYS", err)
return err
}
s = &Service{
Config: config,
Client: &http.Client{},
DB: mongodb.GetInstance().Collection(logCollectionName),
}
return nil
}
// GetInstance ...
func GetInstance() *Service {
return s
}