Externalize requestparser, flush persister on http request end

This commit is contained in:
lash 2024-09-10 23:09:10 +01:00
parent 8e3ff27bb8
commit 681f293d3c
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
5 changed files with 17 additions and 9 deletions

View File

@ -195,7 +195,8 @@ func main() {
}
defer stateStore.Close()
sh := httpserver.NewSessionHandler(cfg, rs, stateStore, userdataStore, hl.Init)
rp := &httpserver.DefaultRequestParser{}
sh := httpserver.NewSessionHandler(cfg, rs, stateStore, userdataStore, rp, hl.Init)
s := &http.Server{
Addr: fmt.Sprintf("%s:%s", host, strconv.Itoa(int(port))),
Handler: sh,

2
go.mod
View File

@ -3,7 +3,7 @@ module git.grassecon.net/urdt/ussd
go 1.22.6
require (
git.defalsify.org/vise.git v0.1.0-rc.2.0.20240907200911-15fe28c9d5b0
git.defalsify.org/vise.git v0.1.0-rc.3.0.20240910220239-03876d1a78bd
github.com/alecthomas/assert/v2 v2.2.2
gopkg.in/leonelquinteros/gotext.v1 v1.3.1
)

4
go.sum
View File

@ -1,5 +1,5 @@
git.defalsify.org/vise.git v0.1.0-rc.2.0.20240907200911-15fe28c9d5b0 h1:B9kE2XXjrYmHNIgRV6fR1WLWE8+z8OvDhJSc96lbGPQ=
git.defalsify.org/vise.git v0.1.0-rc.2.0.20240907200911-15fe28c9d5b0/go.mod h1:JDguWmcoWBdsnpw7PUjVZAEpdC/ubBmjdUBy3tjP63M=
git.defalsify.org/vise.git v0.1.0-rc.3.0.20240910220239-03876d1a78bd h1:pEYKEwz8qHiLfhQC+yYPv3jun7+WAa8xlGtaJpOErQ0=
git.defalsify.org/vise.git v0.1.0-rc.3.0.20240910220239-03876d1a78bd/go.mod h1:JDguWmcoWBdsnpw7PUjVZAEpdC/ubBmjdUBy3tjP63M=
github.com/alecthomas/assert/v2 v2.2.2 h1:Z/iVC0xZfWTaFNE6bA3z07T86hd45Xe2eLt6WVy2bbk=
github.com/alecthomas/assert/v2 v2.2.2/go.mod h1:pXcQ2Asjp247dahGEmsZ6ru0UVwnkhktn7S0bBDLxvQ=
github.com/alecthomas/participle/v2 v2.0.0 h1:Fgrq+MbuSsJwIkw3fEj9h75vDP0Er5JzepJ0/HNHv0g=

View File

@ -16,10 +16,15 @@ var (
logg = logging.NewVanilla().WithDomain("httpserver")
)
type RequestParser struct {
type RequestParser interface {
GetSessionId(rq *http.Request) (string, error)
GetInput(rq *http.Request) ([]byte, error)
}
func(rp *RequestParser) GetSessionId(rq *http.Request) (string, error) {
type DefaultRequestParser struct {
}
func(rp *DefaultRequestParser) GetSessionId(rq *http.Request) (string, error) {
v := rq.Header.Get("X-Vise-Session")
if v == "" {
return "", fmt.Errorf("no session found")
@ -27,7 +32,7 @@ func(rp *RequestParser) GetSessionId(rq *http.Request) (string, error) {
return v, nil
}
func(rp *RequestParser) GetInput(rq *http.Request) ([]byte, error) {
func(rp *DefaultRequestParser) GetInput(rq *http.Request) ([]byte, error) {
defer rq.Body.Close()
v, err := ioutil.ReadAll(rq.Body)
if err != nil {
@ -44,12 +49,12 @@ type SessionHandler struct {
provider StorageProvider
}
func NewSessionHandler(cfg engine.Config, rs resource.Resource, stateDb db.Db, userdataDb db.Db, first resource.EntryFunc) *SessionHandler {
func NewSessionHandler(cfg engine.Config, rs resource.Resource, stateDb db.Db, userdataDb db.Db, rp RequestParser, first resource.EntryFunc) *SessionHandler {
return &SessionHandler{
cfgTemplate: cfg,
rs: rs,
first: first,
rp: RequestParser{},
rp: rp,
provider: NewSimpleStorageProvider(stateDb, userdataDb),
}
}
@ -128,6 +133,7 @@ func(f *SessionHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
f.writeError(w, 500, "Engine finish fail", err)
return
}
_ = r
}

View File

@ -22,6 +22,7 @@ type SimpleStorageProvider struct {
func NewSimpleStorageProvider(stateStore db.Db, userdataStore db.Db) StorageProvider {
pe := persist.NewPersister(stateStore)
pe = pe.WithFlush()
return &SimpleStorageProvider{
Storage: Storage{
Persister: pe,