Compare commits

..

2 Commits

Author SHA1 Message Date
b53658e038
have AtOutput as an option 2024-09-13 16:03:31 +03:00
512460fdeb
Added Custom AtOutput to append CON or END to output 2024-09-13 16:02:04 +03:00
4 changed files with 53 additions and 11 deletions

View File

@ -232,7 +232,7 @@ func main() {
rp := &atRequestParser{} rp := &atRequestParser{}
bsh := handlers.NewBaseSessionHandler(cfg, rs, stateStore, userdataStore, rp, hl) bsh := handlers.NewBaseSessionHandler(cfg, rs, stateStore, userdataStore, rp, hl)
sh := httpserver.ToSessionHandler(bsh) sh := httpserver.ToSessionHandler(bsh, httpserver.WithAtOutput())
s := &http.Server{ s := &http.Server{
Addr: fmt.Sprintf("%s:%s", host, strconv.Itoa(int(port))), Addr: fmt.Sprintf("%s:%s", host, strconv.Itoa(int(port))),
Handler: sh, Handler: sh,

View File

@ -1,13 +1,15 @@
package handlers package handlers
import ( import (
"git.defalsify.org/vise.git/engine" "io"
"git.defalsify.org/vise.git/resource"
"git.defalsify.org/vise.git/persist" "git.defalsify.org/vise.git/db"
"git.defalsify.org/vise.git/db" "git.defalsify.org/vise.git/engine"
"git.defalsify.org/vise.git/persist"
"git.defalsify.org/vise.git/resource"
"git.grassecon.net/urdt/ussd/internal/storage"
"git.grassecon.net/urdt/ussd/internal/handlers/ussd" "git.grassecon.net/urdt/ussd/internal/handlers/ussd"
"git.grassecon.net/urdt/ussd/internal/storage"
) )
type BaseSessionHandler struct { type BaseSessionHandler struct {
@ -88,6 +90,25 @@ func(f *BaseSessionHandler) Output(rqs RequestSession) (RequestSession, error)
return rqs, err return rqs, err
} }
func (f *BaseSessionHandler) AtOutput(rqs RequestSession) (RequestSession, error) {
var err error
var prefix string
if rqs.Continue {
prefix = "CON "
} else {
prefix = "END "
}
_, err = io.WriteString(rqs.Writer, prefix)
if err != nil {
return rqs, err
}
_, err = rqs.Engine.WriteResult(rqs.Ctx, rqs.Writer)
return rqs, err
}
func(f *BaseSessionHandler) Reset(rqs RequestSession) (RequestSession, error) { func(f *BaseSessionHandler) Reset(rqs RequestSession) (RequestSession, error) {
defer f.provider.Put(rqs.Config.SessionId, rqs.Storage) defer f.provider.Put(rqs.Config.SessionId, rqs.Storage)
return rqs, rqs.Engine.Finish() return rqs, rqs.Engine.Finish()

View File

@ -51,6 +51,7 @@ type RequestHandler interface {
GetEngine(cfg engine.Config, rs resource.Resource, pe *persist.Persister) engine.Engine GetEngine(cfg engine.Config, rs resource.Resource, pe *persist.Persister) engine.Engine
Process(rs RequestSession) (RequestSession, error) Process(rs RequestSession) (RequestSession, error)
Output(rs RequestSession) (RequestSession, error) Output(rs RequestSession) (RequestSession, error)
AtOutput(rs RequestSession) (RequestSession, error)
Reset(rs RequestSession) (RequestSession, error) Reset(rs RequestSession) (RequestSession, error)
Shutdown() Shutdown()
} }

View File

@ -43,14 +43,30 @@ func(rp *DefaultRequestParser) GetInput(rq any) ([]byte, error) {
return v, nil return v, nil
} }
type SessionHandler struct {
handlers.RequestHandler
type SessionHandlerOption func(*SessionHandler)
func WithAtOutput() SessionHandlerOption {
return func(sh *SessionHandler) {
sh.useAtOutput = true
}
} }
func ToSessionHandler(h handlers.RequestHandler) *SessionHandler { type SessionHandler struct {
return &SessionHandler{ handlers.RequestHandler
useAtOutput bool
}
func ToSessionHandler(h handlers.RequestHandler, opts ...SessionHandlerOption) *SessionHandler {
sh := &SessionHandler{
RequestHandler: h, RequestHandler: h,
useAtOutput: false,
} }
for _, opt := range opts {
opt(sh)
}
return sh
} }
func(f *SessionHandler) writeError(w http.ResponseWriter, code int, err error) { func(f *SessionHandler) writeError(w http.ResponseWriter, code int, err error) {
@ -108,7 +124,11 @@ func(f *SessionHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(200) w.WriteHeader(200)
w.Header().Set("Content-Type", "text/plain") w.Header().Set("Content-Type", "text/plain")
rqs, err = f.Output(rqs) if f.useAtOutput {
rqs, err = f.AtOutput(rqs)
} else {
rqs, err = f.Output(rqs)
}
if err != nil { if err != nil {
f.writeError(w, 500, err) f.writeError(w, 500, err)
return return