Refactoring: implement proper JSON logging, better structure for app layers

This commit is contained in:
2025-04-30 08:57:20 +03:00
parent 5e77403557
commit 5900903304
6 changed files with 128 additions and 28 deletions
@@ -0,0 +1,38 @@
package handler
import (
"context"
"github.com/nightnoryu/anon3anon/pkg/infrastructure/jsonlog"
"github.com/go-telegram/bot"
"github.com/go-telegram/bot/models"
)
func NewAnonymousMessagesHandler(logger jsonlog.Logger, ownerChatId int) bot.HandlerFunc {
return func(ctx context.Context, b *bot.Bot, update *models.Update) {
if update.Message == nil {
return
}
params := &bot.CopyMessageParams{
ChatID: ownerChatId,
FromChatID: update.Message.Chat.ID,
MessageID: update.Message.ID,
}
_, err := b.CopyMessage(ctx, params)
if err != nil {
logger.Error(err)
return
}
sendParams := &bot.SendMessageParams{
ChatID: update.Message.Chat.ID,
Text: "Сообщение отправлено!!",
}
_, err = b.SendMessage(ctx, sendParams)
if err != nil {
logger.Error(err)
}
}
}
@@ -0,0 +1,23 @@
package handler
import (
"context"
"github.com/nightnoryu/anon3anon/pkg/infrastructure/jsonlog"
"github.com/go-telegram/bot"
"github.com/go-telegram/bot/models"
)
func NewStartCommandHandler(logger jsonlog.Logger) bot.HandlerFunc {
return func(ctx context.Context, b *bot.Bot, update *models.Update) {
params := &bot.SendMessageParams{
ChatID: update.Message.Chat.ID,
Text: "Жду твоих сообщений!!",
}
_, err := b.SendMessage(ctx, params)
if err != nil {
logger.Error(err)
}
}
}
@@ -0,0 +1,26 @@
package middleware
import (
"context"
"fmt"
"github.com/nightnoryu/anon3anon/pkg/infrastructure/jsonlog"
"github.com/go-telegram/bot"
"github.com/go-telegram/bot/models"
)
func NewLoggingMiddleware(logger jsonlog.Logger) bot.Middleware {
return func(next bot.HandlerFunc) bot.HandlerFunc {
return func(ctx context.Context, bot *bot.Bot, update *models.Update) {
if update.Message != nil {
text := update.Message.Text
if len(update.Message.Caption) > 0 {
text = update.Message.Caption
}
logger.Info(fmt.Sprintf("message from %s: %s", update.Message.From.Username, text))
}
next(ctx, bot, update)
}
}
}