Compare commits

..

No commits in common. "1e9c9cf6ad5e9fa5642c784f41184f7583eb3dde" and "b53658e0389f5d612dfe30ddb5b7fdeaeb66987d" have entirely different histories.

5 changed files with 52 additions and 103 deletions

View File

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

View File

@ -1,6 +1,8 @@
package handlers
import (
"io"
"git.defalsify.org/vise.git/db"
"git.defalsify.org/vise.git/engine"
"git.defalsify.org/vise.git/persist"
@ -88,6 +90,25 @@ func(f *BaseSessionHandler) Output(rqs RequestSession) (RequestSession, error)
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) {
defer f.provider.Put(rqs.Config.SessionId, rqs.Storage)
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
Process(rs RequestSession) (RequestSession, error)
Output(rs RequestSession) (RequestSession, error)
AtOutput(rs RequestSession) (RequestSession, error)
Reset(rs RequestSession) (RequestSession, error)
Shutdown()
}

View File

@ -1,93 +0,0 @@
package http
import (
"io"
"net/http"
"git.grassecon.net/urdt/ussd/internal/handlers"
)
type ATSessionHandler struct {
*SessionHandler
}
func NewATSessionHandler(h handlers.RequestHandler) *ATSessionHandler {
return &ATSessionHandler{
SessionHandler: ToSessionHandler(h),
}
}
func (ash *ATSessionHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
var code int
var err error
rqs := handlers.RequestSession{
Ctx: req.Context(),
Writer: w,
}
rp := ash.GetRequestParser()
cfg := ash.GetConfig()
cfg.SessionId, err = rp.GetSessionId(req)
if err != nil {
logg.ErrorCtxf(rqs.Ctx, "", "header processing error", err)
ash.writeError(w, 400, err)
}
rqs.Config = cfg
rqs.Input, err = rp.GetInput(req)
if err != nil {
logg.ErrorCtxf(rqs.Ctx, "", "header processing error", err)
ash.writeError(w, 400, err)
return
}
rqs, err = ash.Process(rqs)
switch err {
case handlers.ErrStorage:
code = 500
case handlers.ErrEngineInit:
code = 500
case handlers.ErrEngineExec:
code = 500
default:
code = 200
}
if code != 200 {
ash.writeError(w, 500, err)
return
}
w.WriteHeader(200)
w.Header().Set("Content-Type", "text/plain")
rqs, err = ash.ATOutput(rqs)
if err != nil {
ash.writeError(w, 500, err)
return
}
rqs, err = ash.Reset(rqs)
if err != nil {
ash.writeError(w, 500, err)
return
}
}
func (ash *ATSessionHandler) ATOutput(rqs handlers.RequestSession) (handlers.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
}

View File

@ -43,14 +43,30 @@ func(rp *DefaultRequestParser) GetInput(rq any) ([]byte, error) {
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 {
return &SessionHandler{
type SessionHandler struct {
handlers.RequestHandler
useAtOutput bool
}
func ToSessionHandler(h handlers.RequestHandler, opts ...SessionHandlerOption) *SessionHandler {
sh := &SessionHandler{
RequestHandler: h,
useAtOutput: false,
}
for _, opt := range opts {
opt(sh)
}
return sh
}
func(f *SessionHandler) writeError(w http.ResponseWriter, code int, err error) {
@ -68,7 +84,6 @@ func(f *SessionHandler) writeError(w http.ResponseWriter, code int, err error) {
func(f *SessionHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
var code int
var err error
var perr error
rqs := handlers.RequestSession{
Ctx: req.Context(),
@ -109,14 +124,19 @@ func(f *SessionHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(200)
w.Header().Set("Content-Type", "text/plain")
if f.useAtOutput {
rqs, err = f.AtOutput(rqs)
} else {
rqs, err = f.Output(rqs)
rqs, perr = f.Reset(rqs)
}
if err != nil {
f.writeError(w, 500, err)
return
}
if perr != nil {
f.writeError(w, 500, perr)
rqs, err = f.Reset(rqs)
if err != nil {
f.writeError(w, 500, err)
return
}
}