initial commit

This commit is contained in:
abelith committed 2026-10-02 00:48:55 +03:00
commit bec2cfd017
18 files changed
+781

No files matched your search

+118
View File
@@ -0,0 +1,118 @@
package core
import (
"context"
"errors"
mcontext "github.com/abelith/gomax/context"
"github.com/abelith/gomax/state"
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"
"time"
)
type FSMStorage interface {
Get(ctx context.Context, userID int) (state.Snapshot, error)
Put(ctx context.Context, state state.Snapshot) error
Delete(ctx context.Context, userID int) error
}
type UpdateHandler interface {
HandleUpdate(c *mcontext.Context) error
Use(mw ...Middleware)
}
type Dispatcher struct {
PollInterval time.Duration
Api *maxbot.Api
Handler UpdateHandler
FSMStorage FSMStorage
}
func (d *Dispatcher) getUpdates(ctx context.Context) <-chan model.Update {
var updates []model.Update
var marker int64
var err error
ch := make(chan model.Update)
go func() {
defer close(ch)
for {
select {
case <-ctx.Done():
return
case <-time.Tick(d.PollInterval):
start := time.Now()
updates, marker, err = d.Api.Subscriptions.GetUpdates(ctx, marker)
latency := time.Since(start)
if err != nil && !errors.Is(err, context.Canceled) {
log.Error().Err(err).Send()
}
log.Ctx(ctx).Debug().
Int("received", len(updates)).
Dur("latency", latency).
Msg("received updates")
for _, update := range updates {
select {
case <-ctx.Done():
return
case ch <- update:
}
}
}
}
}()
return ch
}
func (d *Dispatcher) handleUpdates(ch <-chan model.Update) {
for update := range ch {
go d.handleUpdate(update)
}
}
func (d *Dispatcher) StartPolling(ctx context.Context) {
d.handleUpdates(d.getUpdates(ctx))
<-ctx.Done()
}
func (d *Dispatcher) handleUpdate(upd model.Update) {
ctx := context.Background()
var st *state.Context
snapshot, err := d.FSMStorage.Get(ctx, int(upd.UserID))
if err != nil && !errors.Is(err, state.ErrStateNotFound) {
log.Error().Err(err).Send()
return
}
if errors.Is(err, state.ErrStateNotFound) {
st = state.New(int(upd.UserID))
} else {
st = state.Restore(snapshot)
}
c := mcontext.New(ctx, upd, st, d.Api)
if err := d.Handler.HandleUpdate(c); err != nil {
log.Error().Err(err).Send()
return
}
st = c.State()
if st.State() == state.ClearState {
if err := d.FSMStorage.Delete(ctx, int(upd.UserID)); err != nil {
log.Error().Err(err).Send()
return
}
}
if st.State() != "" {
if err := d.FSMStorage.Put(ctx, st.Snapshot()); err != nil {
log.Error().Err(err).Send()
return
}
}
}
+7
View File
@@ -0,0 +1,7 @@
package core
import "fmt"
var (
ErrRouteNotFound = fmt.Errorf("route not foun")
)
+13
View File
@@ -0,0 +1,13 @@
package core
import mcontext "github.com/abelith/gomax/context"
type HandlerFunc func(c *mcontext.Context) error
func (h HandlerFunc) HandleUpdate(c *mcontext.Context) error {
return h(c)
}
func (h HandlerFunc) Use(_ ...Middleware) {
panic("HandlerFunc.Use must never be called")
}
+72
View File
@@ -0,0 +1,72 @@
package core
import (
"errors"
mcontext "github.com/abelith/gomax/context"
)
type Middleware func(next HandlerFunc) HandlerFunc
type Filter func(c *mcontext.Context) bool
type route struct {
h UpdateHandler
filters []func(c *mcontext.Context) bool
}
type Router struct {
routes []route
mw []Middleware
}
func (r *Router) HandleUpdate(c *mcontext.Context) error {
var current HandlerFunc = r.dispatch
for i := len(r.mw) - 1; i >= 0; i-- {
current = r.mw[i](current)
}
return current.HandleUpdate(c)
}
func (r *Router) Use(mws ...Middleware) {
r.mw = append(r.mw, mws...)
}
func (r *Router) HandleFunc(h HandlerFunc, filters ...func(c *mcontext.Context) bool) {
r.routes = append(r.routes, route{
h: h,
filters: filters,
})
}
func (r *Router) Handle(h UpdateHandler, filters ...func(c *mcontext.Context) bool) {
r.routes = append(r.routes, route{
h: h,
filters: filters,
})
}
func (r *Router) dispatch(c *mcontext.Context) error {
for _, rt := range r.routes {
if match(rt.filters, c) {
err := rt.h.HandleUpdate(c)
if errors.Is(err, ErrRouteNotFound) {
continue
}
return err
}
}
return ErrRouteNotFound
}
func match(filters []func(c *mcontext.Context) bool, c *mcontext.Context) bool {
for _, filter := range filters {
if !filter(c) {
return false
}
}
return true
}