feat: replaced middleware implementations with generic ones, added logger field to dispatcher
This commit is contained in:
1 parent
c4e06aeecd
commit
7bb08f60f3
2 files changed
+30
-32
No files matched your search
+16
-7
@@ -10,7 +10,7 @@ import (
|
||||
"github.com/google/uuid"
|
||||
maxbot "github.com/max-messenger/max-bot-api-client-go/v2"
|
||||
"github.com/max-messenger/max-bot-api-client-go/v2/model"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/rs/zerolog"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
@@ -52,6 +52,7 @@ type Dispatcher struct {
|
||||
fsmStorage FSMStorage
|
||||
eventIsolation EventIsolation
|
||||
rt *Router
|
||||
logger zerolog.Logger
|
||||
wg sync.WaitGroup
|
||||
}
|
||||
|
||||
@@ -59,6 +60,7 @@ type dispatcherOptions struct {
|
||||
poller Poller
|
||||
fsmStorage FSMStorage
|
||||
eventIsolation EventIsolation
|
||||
logger zerolog.Logger
|
||||
}
|
||||
|
||||
type dispatcherOptionWrapper struct {
|
||||
@@ -122,7 +124,7 @@ func (d *Dispatcher) handleUpdates(ch <-chan model.Update) {
|
||||
UserID: upd.UserID,
|
||||
ChatID: upd.ChatID,
|
||||
}
|
||||
l := log.With().
|
||||
l := d.logger.With().
|
||||
Str("update_id", key.UpdateID.String()).
|
||||
Int64("user_id", key.UserID).
|
||||
Int64("chat_id", key.ChatID).
|
||||
@@ -132,7 +134,7 @@ func (d *Dispatcher) handleUpdates(ch <-chan model.Update) {
|
||||
defer cancel()
|
||||
|
||||
if err := d.eventIsolation.Lock(lockCtx, key); err != nil {
|
||||
log.Error().
|
||||
d.logger.Error().
|
||||
Err(err).
|
||||
Int64("user_id", key.UserID).
|
||||
Int64("chat_id", key.ChatID).
|
||||
@@ -164,7 +166,7 @@ func (d *Dispatcher) handleUpdate(ctx context.Context, upd model.Update) {
|
||||
|
||||
snapshot, err := d.fsmStorage.Get(ctx, isolation.Scope{UserID: upd.UserID, ChatID: upd.ChatID})
|
||||
if err != nil && !errors.Is(err, state.ErrStateNotFound) {
|
||||
log.Error().Err(err).Send()
|
||||
d.logger.Error().Err(err).Send()
|
||||
return
|
||||
}
|
||||
if errors.Is(err, state.ErrStateNotFound) {
|
||||
@@ -176,20 +178,20 @@ func (d *Dispatcher) handleUpdate(ctx context.Context, upd model.Update) {
|
||||
c := mcontext.New(ctx, upd, st, d.api)
|
||||
|
||||
if err := d.rt.HandleUpdate(c); err != nil {
|
||||
log.Error().Err(err).Send()
|
||||
d.logger.Error().Err(err).Send()
|
||||
return
|
||||
}
|
||||
|
||||
st = c.State()
|
||||
if st.IsEmpty() {
|
||||
if err := d.fsmStorage.Delete(ctx, isolation.Scope{UserID: upd.UserID, ChatID: upd.ChatID}); err != nil {
|
||||
log.Error().Err(err).Send()
|
||||
d.logger.Error().Err(err).Send()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if err := d.fsmStorage.Put(ctx, st.Snapshot()); err != nil {
|
||||
log.Error().Err(err).Send()
|
||||
d.logger.Error().Err(err).Send()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -211,11 +213,18 @@ func WithEventIsolation(eventIsolation EventIsolation) DispatcherOption {
|
||||
})
|
||||
}
|
||||
|
||||
func WithZerolog(logger zerolog.Logger) DispatcherOption {
|
||||
return newOption(func(options *dispatcherOptions) {
|
||||
options.logger = logger
|
||||
})
|
||||
}
|
||||
|
||||
func newDispatcherOptions(api *maxbot.Api) *dispatcherOptions {
|
||||
return &dispatcherOptions{
|
||||
poller: NewLongPoller(api),
|
||||
fsmStorage: memory.NewFSMStorage(),
|
||||
eventIsolation: memory.NewEventIsolation(defaultTTL),
|
||||
logger: zerolog.Nop(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+14
-25
@@ -2,20 +2,20 @@ package middleware
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
mcontext "github.com/abelith/gomax/v1/context"
|
||||
"github.com/abelith/gomax/v1/core"
|
||||
merrors "github.com/abelith/gomax/v1/errors"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func MwPanicHandler() core.Middleware {
|
||||
func MwPanicHandler(onPanic func()) core.Middleware {
|
||||
return func(next core.HandlerFunc) core.HandlerFunc {
|
||||
return func(c *mcontext.Context) error {
|
||||
return func(c *mcontext.Context) (err error) {
|
||||
defer func() {
|
||||
p := recover()
|
||||
if p != nil {
|
||||
log.Fatal().Any("panic", p).Send()
|
||||
onPanic()
|
||||
err = fmt.Errorf("panic: %v", p)
|
||||
}
|
||||
}()
|
||||
return next(c)
|
||||
@@ -30,45 +30,34 @@ func MwErrorResponder(defaultMsg string) core.Middleware {
|
||||
var maxErr *merrors.GoMaxError
|
||||
|
||||
if errors.As(err, &maxErr) {
|
||||
return c.Respond(maxErr.Message)
|
||||
return errors.Join(c.Respond(maxErr.Message), maxErr)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return c.Respond(defaultMsg)
|
||||
return errors.Join(c.Respond(defaultMsg), err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func MwLoggerInjector(logger zerolog.Logger) core.Middleware {
|
||||
func MwCallback(callback func(c *mcontext.Context)) core.Middleware {
|
||||
return func(next core.HandlerFunc) core.HandlerFunc {
|
||||
return func(c *mcontext.Context) error {
|
||||
ctx := logger.With().Int64("user_id", c.Update().UserID).
|
||||
Logger().WithContext(c.Context())
|
||||
c.SetContext(ctx)
|
||||
callback(c)
|
||||
return next(c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func MwDebugState() core.Middleware {
|
||||
func MwErrorHandler(onError func(err error)) core.Middleware {
|
||||
return func(next core.HandlerFunc) core.HandlerFunc {
|
||||
return func(c *mcontext.Context) error {
|
||||
log.Debug().Any("state", c.State()).Any("update", c.Update()).Send()
|
||||
return next(c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func MwErrorLogger() core.Middleware {
|
||||
return func(next core.HandlerFunc) core.HandlerFunc {
|
||||
return func(c *mcontext.Context) error {
|
||||
if err := next(c); err != nil {
|
||||
log.Ctx(c).Err(err).Send()
|
||||
return err
|
||||
var err error
|
||||
if err = next(c); err != nil {
|
||||
onError(err)
|
||||
}
|
||||
return nil
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user