Refactoring

1. Move all pre-defined messages to constants in a separate file
2. Remove pkg/errors dependency
This commit is contained in:
2025-05-15 22:20:37 +03:00
parent 0a8409e66d
commit 9e38b000ec
10 changed files with 34 additions and 20 deletions
-13
View File
@@ -1,13 +0,0 @@
package jsonlog
import "github.com/sirupsen/logrus"
type Level logrus.Level
const (
FatalLevel = Level(logrus.FatalLevel)
ErrorLevel = Level(logrus.ErrorLevel)
WarnLevel = Level(logrus.WarnLevel)
InfoLevel = Level(logrus.InfoLevel)
DebugLevel = Level(logrus.DebugLevel)
)
-55
View File
@@ -1,55 +0,0 @@
package jsonlog
import (
"time"
"github.com/sirupsen/logrus"
)
const appNameKey = "app_name"
var fieldMap = logrus.FieldMap{
logrus.FieldKeyTime: "@timestamp",
logrus.FieldKeyMsg: "message",
}
type Logger interface {
WithField(key string, value interface{}) Logger
Info(...interface{})
Error(error, ...interface{})
FatalError(error, ...interface{})
}
type Config struct {
AppName string
Level Level
}
func NewLogger(config *Config) Logger {
impl := logrus.New()
impl.SetFormatter(&logrus.JSONFormatter{
TimestampFormat: time.RFC3339Nano,
FieldMap: fieldMap,
})
impl.SetLevel(logrus.Level(config.Level))
return &logger{
FieldLogger: impl.WithField(appNameKey, config.AppName),
}
}
type logger struct {
logrus.FieldLogger
}
func (l *logger) WithField(key string, value interface{}) Logger {
return &logger{l.FieldLogger.WithField(key, value)}
}
func (l *logger) Error(err error, args ...interface{}) {
l.FieldLogger.WithError(err).Error(args)
}
func (l *logger) FatalError(err error, args ...interface{}) {
l.FieldLogger.WithError(err).Fatal(args...)
}
@@ -1,38 +0,0 @@
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)
}
}
}
@@ -1,23 +0,0 @@
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)
}
}
}
@@ -1,39 +0,0 @@
package middleware
import (
"context"
"fmt"
"github.com/nightnoryu/anon3anon/pkg/infrastructure/jsonlog"
"github.com/go-telegram/bot"
"github.com/go-telegram/bot/models"
)
const (
chatIDField = "chat_id"
usernameField = "username"
)
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 {
return
}
chatLogger := logger.
WithField(chatIDField, update.Message.Chat.ID).
WithField(usernameField, update.Message.From.Username)
text := update.Message.Text
if len(update.Message.Caption) > 0 {
text = update.Message.Caption
}
chatLogger.Info(fmt.Sprintf("new message: %s", text))
next(ctx, bot, update)
}
}
}