@@ -1,19 +1,65 @@
|
||||
package config
|
||||
|
||||
type Config struct {
|
||||
MongoURL string
|
||||
import (
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type AppConfig struct {
|
||||
MongoConfig *MongoConfig
|
||||
TgConfig *TgConfig
|
||||
}
|
||||
|
||||
type MongoConfig struct {
|
||||
URL string
|
||||
DBName string
|
||||
ChatsCollectionName string
|
||||
WorkoutsCollectionName string
|
||||
CaloriesCollectionName string
|
||||
}
|
||||
|
||||
func NewConfig(mongoURl string, dbName string) *Config {
|
||||
return &Config{
|
||||
MongoURL: mongoURl,
|
||||
DBName: dbName,
|
||||
ChatsCollectionName: "chats",
|
||||
WorkoutsCollectionName: "workouts",
|
||||
CaloriesCollectionName: "calories",
|
||||
type TgConfig struct {
|
||||
Token string
|
||||
}
|
||||
|
||||
func NewAppConfig() *AppConfig {
|
||||
mongoURL, err := readFile("mongo_url.txt")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
mongoURL = strings.ReplaceAll(mongoURL, "\n", "")
|
||||
|
||||
dbName, err := readFile("db_name.txt")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
dbName = strings.ReplaceAll(dbName, "\n", "")
|
||||
|
||||
token, err := readFile("token.txt")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
token = strings.ReplaceAll(token, "\n", "")
|
||||
|
||||
return &AppConfig{
|
||||
MongoConfig: &MongoConfig{
|
||||
URL: mongoURL,
|
||||
DBName: dbName,
|
||||
ChatsCollectionName: "chats",
|
||||
WorkoutsCollectionName: "workouts",
|
||||
CaloriesCollectionName: "calories",
|
||||
},
|
||||
TgConfig: &TgConfig{
|
||||
Token: token,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func readFile(filename string) (string, error) {
|
||||
b, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
str := string(b)
|
||||
return str, nil
|
||||
}
|
||||
|
||||
+7
-8
@@ -4,11 +4,12 @@ import (
|
||||
"context"
|
||||
"log"
|
||||
"time"
|
||||
"valera/internal/config"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
|
||||
"valera/internal/config"
|
||||
)
|
||||
|
||||
type DB struct {
|
||||
@@ -18,14 +19,12 @@ type DB struct {
|
||||
}
|
||||
|
||||
func NewDB(
|
||||
mongoURL string,
|
||||
dbName string,
|
||||
cfg *config.AppConfig,
|
||||
) (*DB, error) {
|
||||
ctx := context.Background()
|
||||
opt := options.Client().
|
||||
ApplyURI(mongoURL).
|
||||
ApplyURI(cfg.MongoConfig.URL).
|
||||
SetMinPoolSize(10)
|
||||
cfg := config.NewConfig(mongoURL, dbName)
|
||||
|
||||
client, err := mongo.Connect(ctx, opt)
|
||||
if err != nil {
|
||||
@@ -33,9 +32,9 @@ func NewDB(
|
||||
}
|
||||
|
||||
return &DB{
|
||||
chatsColection: client.Database(dbName).Collection(cfg.ChatsCollectionName),
|
||||
workoutsColection: client.Database(dbName).Collection(cfg.WorkoutsCollectionName),
|
||||
caloriesColection: client.Database(dbName).Collection(cfg.CaloriesCollectionName),
|
||||
chatsColection: client.Database(cfg.MongoConfig.DBName).Collection(cfg.MongoConfig.ChatsCollectionName),
|
||||
workoutsColection: client.Database(cfg.MongoConfig.DBName).Collection(cfg.MongoConfig.WorkoutsCollectionName),
|
||||
caloriesColection: client.Database(cfg.MongoConfig.DBName).Collection(cfg.MongoConfig.CaloriesCollectionName),
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user