valera/internal/config/config.go

68 lines
1.2 KiB
Go

package config
import (
"io/ioutil"
"strings"
)
type AppConfig struct {
Port string
MongoConfig *MongoConfig
TgConfig *TgConfig
}
type MongoConfig struct {
URL string
DBName string
ChatsCollectionName string
WorkoutsCollectionName string
CaloriesCollectionName string
}
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{
Port: ":10002",
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
}