valera/internal/config/config.go

70 lines
1.3 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 {
2023-04-07 08:11:19 +00:00
Port string
2023-04-07 08:01:17 +00:00
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-04-09 08:48:31 +00:00
WeightCollectionName 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{
2023-04-07 08:11:19 +00:00
Port: ":10002",
2023-04-07 08:01:17 +00:00
MongoConfig: &MongoConfig{
URL: mongoURL,
DBName: dbName,
ChatsCollectionName: "chats",
WorkoutsCollectionName: "workouts",
CaloriesCollectionName: "calories",
2023-04-09 08:48:31 +00:00
WeightCollectionName: "weights",
2023-04-07 08:01:17 +00:00
},
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
}