valera/internal/config/config.go

66 lines
1.2 KiB
Go
Raw Normal View History

2023-03-11 10:17:30 +00:00
package config
2023-04-07 08:01:17 +00:00
import (
"io/ioutil"
"strings"
)
type AppConfig struct {
MongoConfig *MongoConfig
TgConfig *TgConfig
}
type MongoConfig struct {
URL string
2023-03-11 11:30:12 +00:00
DBName string
ChatsCollectionName string
WorkoutsCollectionName string
2023-03-12 06:53:50 +00:00
CaloriesCollectionName string
2023-03-11 10:17:30 +00:00
}
2023-04-07 08:01:17 +00:00
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
2023-03-11 10:17:30 +00:00
}
2023-04-07 08:01:17 +00:00
str := string(b)
return str, nil
2023-03-11 10:17:30 +00:00
}