Redis -> YDB

This commit is contained in:
Artyom Belousov 2023-05-27 22:10:31 +03:00
parent 1f4888069f
commit a33680d527
21 changed files with 448 additions and 322 deletions

View file

@ -1,55 +1,81 @@
package main
import (
"encoding/json"
"io/ioutil"
"context"
"fmt"
"log"
"os"
"time"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
environ "github.com/ydb-platform/ydb-go-sdk-auth-environ"
"github.com/ydb-platform/ydb-go-sdk/v3"
"gitlab.com/flygrounder/go-mtg-vk/internal/caching"
"gitlab.com/flygrounder/go-mtg-vk/internal/cardsinfo"
"gitlab.com/flygrounder/go-mtg-vk/internal/scenario"
"gitlab.com/flygrounder/go-mtg-vk/internal/caching"
"gitlab.com/flygrounder/go-mtg-vk/internal/telegram"
)
const welcomeMessage = "Здравствуйте, вас приветствует бот для поиска цен на карты MTG, введите название карты, которая вас интересует."
func main() {
dict, _ := os.Open("./assets/additional_cards.json")
dictBytes, _ := ioutil.ReadAll(dict)
var dictMap map[string]string
_ = json.Unmarshal(dictBytes, &dictMap)
bot, _ := tgbotapi.NewBotAPI(os.Getenv("TG_TOKEN"))
token, exists := os.LookupEnv("TG_TOKEN")
if !exists {
panic("TG_TOKEN environment variable not defined")
}
bot, _ := tgbotapi.NewBotAPI(token)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
dsn, exists := os.LookupEnv("YDB_CONNECTION_STRING")
if !exists {
panic("YDB_CONNECTION_STRING environment variable not defined")
}
db, err := ydb.Open(ctx,
dsn,
environ.WithEnvironCredentials(ctx),
)
if err != nil {
panic(fmt.Errorf("connect error: %w", err))
}
defer func() { _ = db.Close(ctx) }()
sender := &telegram.Sender{
API: bot,
API: bot,
}
cache := &caching.CacheClient{
Storage: db.Table(),
Expiration: 12 * time.Hour,
Prefix: db.Name(),
}
err = cache.Init(context.Background())
if err != nil {
panic(fmt.Errorf("init error: %w", err))
}
sc := &scenario.Scenario{
Sender: sender,
Logger: log.New(os.Stdout, "", 0),
Cache: caching.NewClient("redis:6379", "", time.Hour*24, 0),
InfoFetcher: &cardsinfo.Fetcher{
Dict: dictMap,
},
Sender: sender,
Logger: log.New(os.Stdout, "", 0),
Cache: cache,
InfoFetcher: &cardsinfo.Fetcher{},
}
u := tgbotapi.NewUpdate(0)
updates, _ := bot.GetUpdatesChan(u)
for update := range updates {
if update.Message == nil {
continue
}
if update.Message == nil {
continue
}
if update.Message.Text == "/start" {
sender.Send(update.Message.Chat.ID, welcomeMessage)
continue
}
if update.Message.Text == "/start" {
sender.Send(update.Message.Chat.ID, welcomeMessage)
continue
}
go sc.HandleSearch(&scenario.UserMessage{
Body: update.Message.Text,
UserId: update.Message.Chat.ID,
})
}
go sc.HandleSearch(context.Background(), &scenario.UserMessage{
Body: update.Message.Text,
UserId: update.Message.Chat.ID,
})
}
}

View file

@ -1,19 +1,21 @@
package main
import (
"encoding/json"
"io/ioutil"
"context"
"fmt"
"log"
"math/rand"
"os"
"strconv"
"time"
"gitlab.com/flygrounder/go-mtg-vk/internal/caching"
"gitlab.com/flygrounder/go-mtg-vk/internal/cardsinfo"
"gitlab.com/flygrounder/go-mtg-vk/internal/scenario"
"github.com/gin-gonic/gin"
"gitlab.com/flygrounder/go-mtg-vk/internal/caching"
environ "github.com/ydb-platform/ydb-go-sdk-auth-environ"
"github.com/ydb-platform/ydb-go-sdk/v3"
"gitlab.com/flygrounder/go-mtg-vk/internal/vk"
)
@ -22,11 +24,34 @@ func main() {
r := gin.Default()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
dsn, exists := os.LookupEnv("YDB_CONNECTION_STRING")
if !exists {
panic("YDB_CONNECTION_STRING environment variable not defined")
}
db, err := ydb.Open(ctx,
dsn,
environ.WithEnvironCredentials(ctx),
)
if err != nil {
panic(fmt.Errorf("connect error: %w", err))
}
defer func() { _ = db.Close(ctx) }()
cache := &caching.CacheClient{
Storage: db.Table(),
Expiration: 12 * time.Hour,
Prefix: db.Name(),
}
err = cache.Init(context.Background())
if err != nil {
panic(fmt.Errorf("init error: %w", err))
}
groupId, _ := strconv.ParseInt(os.Getenv("VK_GROUP_ID"), 10, 64)
dict, _ := os.Open("./assets/additional_cards.json")
dictBytes, _ := ioutil.ReadAll(dict)
var dictMap map[string]string
_ = json.Unmarshal(dictBytes, &dictMap)
logger := log.New(os.Stdout, "", 0)
handler := vk.Handler{
Scenario: &scenario.Scenario{
@ -34,11 +59,9 @@ func main() {
Token: os.Getenv("VK_TOKEN"),
Logger: logger,
},
Logger: logger,
Cache: caching.NewClient("redis:6379", "", time.Hour*24, 0),
InfoFetcher: &cardsinfo.Fetcher{
Dict: dictMap,
},
Logger: logger,
InfoFetcher: &cardsinfo.Fetcher{},
Cache: cache,
},
SecretKey: os.Getenv("VK_SECRET_KEY"),
GroupId: groupId,