add app config
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
4ceb63a307
commit
92634ce744
|
@ -1,21 +1,17 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"valera/internal/calories"
|
||||
"valera/internal/commands"
|
||||
"valera/internal/config"
|
||||
"valera/internal/db"
|
||||
"valera/internal/pause"
|
||||
|
||||
tgbot "github.com/go-telegram-bot-api/telegram-bot-api"
|
||||
"github.com/umputun/go-flags"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -28,71 +24,22 @@ var (
|
|||
)
|
||||
|
||||
const (
|
||||
version = "v1.6.2"
|
||||
version = "v1.6.3"
|
||||
)
|
||||
|
||||
type Opts struct {
|
||||
Token string `short:"t" long:"token" description:"Telegram api token"`
|
||||
Name string `short:"n" long:"name" description:"Telegram bot name" default:"@body_weight_loss_bot"`
|
||||
}
|
||||
|
||||
var opts Opts
|
||||
|
||||
func readFile(filename string) (string, error) {
|
||||
b, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
str := string(b)
|
||||
return str, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
run()
|
||||
}
|
||||
|
||||
func run() {
|
||||
p := flags.NewParser(&opts, flags.PrintErrors|flags.PassDoubleDash|flags.HelpFlag)
|
||||
p.SubcommandsOptional = true
|
||||
if _, err := p.Parse(); err != nil {
|
||||
if err.(*flags.Error).Type != flags.ErrHelp {
|
||||
log.Println(errors.New("[ERROR] cli error: " + err.Error()))
|
||||
}
|
||||
os.Exit(2)
|
||||
}
|
||||
cfg := config.NewAppConfig()
|
||||
|
||||
if opts.Token == "" {
|
||||
token, err := readFile("token.txt")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
opts.Token = strings.ReplaceAll(token, "\n", "")
|
||||
}
|
||||
fmt.Println(opts.Token)
|
||||
|
||||
bot, err := tgbot.NewBotAPI(opts.Token)
|
||||
bot, err := tgbot.NewBotAPI(cfg.TgConfig.Token)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
mongoURL, err := readFile("mongo_url.txt")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
mongoURL = strings.ReplaceAll(mongoURL, "\n", "")
|
||||
fmt.Println(mongoURL)
|
||||
|
||||
dbName, err := readFile("db_name.txt")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
dbName = strings.ReplaceAll(dbName, "\n", "")
|
||||
fmt.Println(dbName)
|
||||
|
||||
dataBase, err := db.NewDB(
|
||||
mongoURL,
|
||||
dbName,
|
||||
)
|
||||
dataBase, err := db.NewDB(cfg)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
@ -150,8 +97,6 @@ func run() {
|
|||
log.Fatal(http.ListenAndServe(port, nil))
|
||||
}()
|
||||
|
||||
log.Println("Run", opts.Name)
|
||||
|
||||
for update := range updates {
|
||||
|
||||
if update.Message == nil {
|
||||
|
@ -252,7 +197,7 @@ func run() {
|
|||
continue
|
||||
}
|
||||
|
||||
command := commands.Command(strings.Replace(text, opts.Name, "", 1))
|
||||
command := commands.Command(text)
|
||||
switch command {
|
||||
case commands.Start:
|
||||
_, _ = bot.Send(tgbot.NewMessage(chatID, fmt.Sprintf("Здорова, я Валера (%s), твой тренер (%d).", version, chatID)))
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue