Implement http server for the URDT vise engine #41

Merged
lash merged 8 commits from lash/http-server into master 2024-09-12 00:13:15 +02:00
5 changed files with 17 additions and 9 deletions
Showing only changes of commit 681f293d3c - Show all commits

View File

@ -195,7 +195,8 @@ func main() {
} }
defer stateStore.Close() 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{ 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,

2
go.mod
View File

@ -3,7 +3,7 @@ module git.grassecon.net/urdt/ussd
go 1.22.6 go 1.22.6
require ( 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 github.com/alecthomas/assert/v2 v2.2.2
gopkg.in/leonelquinteros/gotext.v1 v1.3.1 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.3.0.20240910220239-03876d1a78bd h1:pEYKEwz8qHiLfhQC+yYPv3jun7+WAa8xlGtaJpOErQ0=
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/go.mod h1:JDguWmcoWBdsnpw7PUjVZAEpdC/ubBmjdUBy3tjP63M=
github.com/alecthomas/assert/v2 v2.2.2 h1:Z/iVC0xZfWTaFNE6bA3z07T86hd45Xe2eLt6WVy2bbk= 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/assert/v2 v2.2.2/go.mod h1:pXcQ2Asjp247dahGEmsZ6ru0UVwnkhktn7S0bBDLxvQ=
github.com/alecthomas/participle/v2 v2.0.0 h1:Fgrq+MbuSsJwIkw3fEj9h75vDP0Er5JzepJ0/HNHv0g= github.com/alecthomas/participle/v2 v2.0.0 h1:Fgrq+MbuSsJwIkw3fEj9h75vDP0Er5JzepJ0/HNHv0g=

View File

@ -16,10 +16,15 @@ var (
logg = logging.NewVanilla().WithDomain("httpserver") 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") v := rq.Header.Get("X-Vise-Session")
if v == "" { if v == "" {
return "", fmt.Errorf("no session found") return "", fmt.Errorf("no session found")
@ -27,7 +32,7 @@ func(rp *RequestParser) GetSessionId(rq *http.Request) (string, error) {
return v, nil 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() defer rq.Body.Close()
v, err := ioutil.ReadAll(rq.Body) v, err := ioutil.ReadAll(rq.Body)
if err != nil { if err != nil {
@ -44,12 +49,12 @@ type SessionHandler struct {
provider StorageProvider 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{ return &SessionHandler{
cfgTemplate: cfg, cfgTemplate: cfg,
rs: rs, rs: rs,
first: first, first: first,
rp: RequestParser{}, rp: rp,
provider: NewSimpleStorageProvider(stateDb, userdataDb), 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) f.writeError(w, 500, "Engine finish fail", err)
return return
} }
_ = r _ = r
} }

View File

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