Change project structure a bit

This commit is contained in:
2025-06-02 21:00:50 +03:00
parent 035045ce90
commit b1c34f77ce
7 changed files with 20 additions and 14 deletions
+13
View File
@@ -0,0 +1,13 @@
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
@@ -0,0 +1,55 @@
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 any) Logger
Info(...any)
Error(error, ...any)
FatalError(error, ...any)
}
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 any) Logger {
return &logger{l.FieldLogger.WithField(key, value)}
}
func (l *logger) Error(err error, args ...any) {
l.FieldLogger.WithError(err).Error(args...)
}
func (l *logger) FatalError(err error, args ...any) {
l.FieldLogger.WithError(err).Fatal(args...)
}
@@ -0,0 +1,44 @@
package handler
import (
"context"
"fmt"
"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
}
if ownerChatId == 0 {
logger.Info(fmt.Sprintf("owner chat ID not set. set to %d to use the last chat", update.Message.Chat.ID))
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: messageSentMessage,
}
_, err = b.SendMessage(ctx, sendParams)
if err != nil {
logger.Error(err)
}
}
}
@@ -0,0 +1,6 @@
package handler
const (
greetingMessage = "Жду твоих сообщений!!"
messageSentMessage = "Сообщение отправлено!!"
)
@@ -0,0 +1,27 @@
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) {
if update.Message == nil {
return
}
params := &bot.SendMessageParams{
ChatID: update.Message.Chat.ID,
Text: greetingMessage,
}
_, err := b.SendMessage(ctx, params)
if err != nil {
logger.Error(err)
}
}
}
@@ -0,0 +1,39 @@
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)
}
}
}