2024-09-12 05:07:55 +02:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
2024-09-13 15:02:04 +02:00
|
|
|
"git.defalsify.org/vise.git/db"
|
2024-09-12 05:07:55 +02:00
|
|
|
"git.defalsify.org/vise.git/engine"
|
|
|
|
"git.defalsify.org/vise.git/persist"
|
2024-09-13 15:02:04 +02:00
|
|
|
"git.defalsify.org/vise.git/resource"
|
2024-09-12 05:07:55 +02:00
|
|
|
|
|
|
|
"git.grassecon.net/urdt/ussd/internal/handlers/ussd"
|
2024-09-13 15:02:04 +02:00
|
|
|
"git.grassecon.net/urdt/ussd/internal/storage"
|
2024-09-12 05:07:55 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type BaseSessionHandler struct {
|
|
|
|
cfgTemplate engine.Config
|
|
|
|
rp RequestParser
|
|
|
|
rs resource.Resource
|
|
|
|
hn *ussd.Handlers
|
|
|
|
provider storage.StorageProvider
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBaseSessionHandler(cfg engine.Config, rs resource.Resource, stateDb db.Db, userdataDb db.Db, rp RequestParser, hn *ussd.Handlers) *BaseSessionHandler {
|
|
|
|
return &BaseSessionHandler{
|
|
|
|
cfgTemplate: cfg,
|
|
|
|
rs: rs,
|
|
|
|
hn: hn,
|
|
|
|
rp: rp,
|
|
|
|
provider: storage.NewSimpleStorageProvider(stateDb, userdataDb),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func(f* BaseSessionHandler) Shutdown() {
|
|
|
|
err := f.provider.Close()
|
|
|
|
if err != nil {
|
|
|
|
logg.Errorf("handler shutdown error", "err", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func(f *BaseSessionHandler) GetEngine(cfg engine.Config, rs resource.Resource, pr *persist.Persister) engine.Engine {
|
|
|
|
en := engine.NewEngine(cfg, rs)
|
|
|
|
en = en.WithPersister(pr)
|
|
|
|
return en
|
|
|
|
}
|
|
|
|
|
|
|
|
func(f *BaseSessionHandler) Process(rqs RequestSession) (RequestSession, error) {
|
|
|
|
var r bool
|
|
|
|
var err error
|
|
|
|
var ok bool
|
|
|
|
|
|
|
|
logg.InfoCtxf(rqs.Ctx, "new request", rqs)
|
|
|
|
|
|
|
|
rqs.Storage, err = f.provider.Get(rqs.Config.SessionId)
|
|
|
|
if err != nil {
|
2024-09-12 17:46:11 +02:00
|
|
|
logg.ErrorCtxf(rqs.Ctx, "", "storage get error", err)
|
2024-09-12 05:07:55 +02:00
|
|
|
return rqs, ErrStorage
|
|
|
|
}
|
|
|
|
|
|
|
|
f.hn = f.hn.WithPersister(rqs.Storage.Persister)
|
|
|
|
eni := f.GetEngine(rqs.Config, f.rs, rqs.Storage.Persister)
|
|
|
|
en, ok := eni.(*engine.DefaultEngine)
|
|
|
|
if !ok {
|
2024-09-12 17:46:11 +02:00
|
|
|
perr := f.provider.Put(rqs.Config.SessionId, rqs.Storage)
|
|
|
|
rqs.Storage = nil
|
|
|
|
if perr != nil {
|
|
|
|
logg.ErrorCtxf(rqs.Ctx, "", "storage put error", perr)
|
|
|
|
}
|
2024-09-12 05:07:55 +02:00
|
|
|
return rqs, ErrEngineType
|
|
|
|
}
|
|
|
|
en = en.WithFirst(f.hn.Init)
|
|
|
|
if rqs.Config.EngineDebug {
|
|
|
|
en = en.WithDebug(nil)
|
|
|
|
}
|
|
|
|
rqs.Engine = en
|
|
|
|
|
2024-09-20 12:13:20 +02:00
|
|
|
r, err = rqs.Engine.Exec(rqs.Ctx, rqs.Input)
|
2024-09-12 05:07:55 +02:00
|
|
|
if err != nil {
|
2024-09-12 17:46:11 +02:00
|
|
|
perr := f.provider.Put(rqs.Config.SessionId, rqs.Storage)
|
|
|
|
rqs.Storage = nil
|
|
|
|
if perr != nil {
|
|
|
|
logg.ErrorCtxf(rqs.Ctx, "", "storage put error", perr)
|
|
|
|
}
|
2024-09-12 05:07:55 +02:00
|
|
|
return rqs, err
|
|
|
|
}
|
|
|
|
|
2024-09-12 22:52:35 +02:00
|
|
|
rqs.Continue = r
|
2024-09-12 05:07:55 +02:00
|
|
|
return rqs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func(f *BaseSessionHandler) Output(rqs RequestSession) (RequestSession, error) {
|
|
|
|
var err error
|
|
|
|
_, err = rqs.Engine.WriteResult(rqs.Ctx, rqs.Writer)
|
|
|
|
return rqs, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func(f *BaseSessionHandler) Reset(rqs RequestSession) (RequestSession, error) {
|
|
|
|
defer f.provider.Put(rqs.Config.SessionId, rqs.Storage)
|
|
|
|
return rqs, rqs.Engine.Finish()
|
|
|
|
}
|
|
|
|
|
|
|
|
func(f *BaseSessionHandler) GetConfig() engine.Config {
|
|
|
|
return f.cfgTemplate
|
|
|
|
}
|
|
|
|
|
|
|
|
func(f *BaseSessionHandler) GetRequestParser() RequestParser {
|
|
|
|
return f.rp
|
|
|
|
}
|