add app config
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Владимир Фёдоров 2023-04-07 15:01:17 +07:00
parent 4ceb63a307
commit 92634ce744
3 changed files with 68 additions and 78 deletions

View File

@ -1,21 +1,17 @@
package main package main
import ( import (
"errors"
"fmt" "fmt"
"io/ioutil"
"log" "log"
"net/http" "net/http"
"os"
"strconv" "strconv"
"strings"
"valera/internal/calories" "valera/internal/calories"
"valera/internal/commands" "valera/internal/commands"
"valera/internal/config"
"valera/internal/db" "valera/internal/db"
"valera/internal/pause" "valera/internal/pause"
tgbot "github.com/go-telegram-bot-api/telegram-bot-api" tgbot "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/umputun/go-flags"
) )
var ( var (
@ -28,71 +24,22 @@ var (
) )
const ( 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() { func main() {
run() run()
} }
func run() { func run() {
p := flags.NewParser(&opts, flags.PrintErrors|flags.PassDoubleDash|flags.HelpFlag) cfg := config.NewAppConfig()
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)
}
if opts.Token == "" { bot, err := tgbot.NewBotAPI(cfg.TgConfig.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)
if err != nil { if err != nil {
panic(err) panic(err)
} }
mongoURL, err := readFile("mongo_url.txt") dataBase, err := db.NewDB(cfg)
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,
)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -150,8 +97,6 @@ func run() {
log.Fatal(http.ListenAndServe(port, nil)) log.Fatal(http.ListenAndServe(port, nil))
}() }()
log.Println("Run", opts.Name)
for update := range updates { for update := range updates {
if update.Message == nil { if update.Message == nil {
@ -252,7 +197,7 @@ func run() {
continue continue
} }
command := commands.Command(strings.Replace(text, opts.Name, "", 1)) command := commands.Command(text)
switch command { switch command {
case commands.Start: case commands.Start:
_, _ = bot.Send(tgbot.NewMessage(chatID, fmt.Sprintf("Здорова, я Валера (%s), твой тренер (%d).", version, chatID))) _, _ = bot.Send(tgbot.NewMessage(chatID, fmt.Sprintf("Здорова, я Валера (%s), твой тренер (%d).", version, chatID)))

View File

@ -1,19 +1,65 @@
package config package config
type Config struct { import (
MongoURL string "io/ioutil"
"strings"
)
type AppConfig struct {
MongoConfig *MongoConfig
TgConfig *TgConfig
}
type MongoConfig struct {
URL string
DBName string DBName string
ChatsCollectionName string ChatsCollectionName string
WorkoutsCollectionName string WorkoutsCollectionName string
CaloriesCollectionName string CaloriesCollectionName string
} }
func NewConfig(mongoURl string, dbName string) *Config { type TgConfig struct {
return &Config{ Token string
MongoURL: mongoURl, }
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, DBName: dbName,
ChatsCollectionName: "chats", ChatsCollectionName: "chats",
WorkoutsCollectionName: "workouts", WorkoutsCollectionName: "workouts",
CaloriesCollectionName: "calories", 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
}

View File

@ -4,11 +4,12 @@ import (
"context" "context"
"log" "log"
"time" "time"
"valera/internal/config"
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/options"
"valera/internal/config"
) )
type DB struct { type DB struct {
@ -18,14 +19,12 @@ type DB struct {
} }
func NewDB( func NewDB(
mongoURL string, cfg *config.AppConfig,
dbName string,
) (*DB, error) { ) (*DB, error) {
ctx := context.Background() ctx := context.Background()
opt := options.Client(). opt := options.Client().
ApplyURI(mongoURL). ApplyURI(cfg.MongoConfig.URL).
SetMinPoolSize(10) SetMinPoolSize(10)
cfg := config.NewConfig(mongoURL, dbName)
client, err := mongo.Connect(ctx, opt) client, err := mongo.Connect(ctx, opt)
if err != nil { if err != nil {
@ -33,9 +32,9 @@ func NewDB(
} }
return &DB{ return &DB{
chatsColection: client.Database(dbName).Collection(cfg.ChatsCollectionName), chatsColection: client.Database(cfg.MongoConfig.DBName).Collection(cfg.MongoConfig.ChatsCollectionName),
workoutsColection: client.Database(dbName).Collection(cfg.WorkoutsCollectionName), workoutsColection: client.Database(cfg.MongoConfig.DBName).Collection(cfg.MongoConfig.WorkoutsCollectionName),
caloriesColection: client.Database(dbName).Collection(cfg.CaloriesCollectionName), caloriesColection: client.Database(cfg.MongoConfig.DBName).Collection(cfg.MongoConfig.CaloriesCollectionName),
}, nil }, nil
} }