Add storage retrieval solution for http handler
This commit is contained in:
parent
e6754a1e92
commit
350b040a59
@ -17,8 +17,9 @@ import (
|
|||||||
"git.defalsify.org/vise.git/persist"
|
"git.defalsify.org/vise.git/persist"
|
||||||
"git.defalsify.org/vise.git/resource"
|
"git.defalsify.org/vise.git/resource"
|
||||||
"git.defalsify.org/vise.git/logging"
|
"git.defalsify.org/vise.git/logging"
|
||||||
|
|
||||||
"git.grassecon.net/urdt/ussd/internal/handlers/ussd"
|
"git.grassecon.net/urdt/ussd/internal/handlers/ussd"
|
||||||
httpserver "git.grassecon.net/urdt/ussd/http"
|
httpserver "git.grassecon.net/urdt/ussd/internal/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -77,7 +78,7 @@ func getHandler(appFlags *asm.FlagParser, rs *resource.DbResource, userdataStore
|
|||||||
return ussdHandlers, nil
|
return ussdHandlers, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getPersister(dbDir string, ctx context.Context) (*persist.Persister, error) {
|
func getStateStore(dbDir string, ctx context.Context) (db.Db, error) {
|
||||||
err := os.MkdirAll(dbDir, 0700)
|
err := os.MkdirAll(dbDir, 0700)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("state dir create exited with error: %v\n", err)
|
return nil, fmt.Errorf("state dir create exited with error: %v\n", err)
|
||||||
@ -85,8 +86,7 @@ func getPersister(dbDir string, ctx context.Context) (*persist.Persister, error)
|
|||||||
store := gdbmdb.NewGdbmDb()
|
store := gdbmdb.NewGdbmDb()
|
||||||
storeFile := path.Join(dbDir, "state.gdbm")
|
storeFile := path.Join(dbDir, "state.gdbm")
|
||||||
store.Connect(ctx, storeFile)
|
store.Connect(ctx, storeFile)
|
||||||
pr := persist.NewPersister(store)
|
return store, nil
|
||||||
return pr, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getUserdataDb(dbDir string, ctx context.Context) db.Db {
|
func getUserdataDb(dbDir string, ctx context.Context) db.Db {
|
||||||
@ -160,7 +160,7 @@ func main() {
|
|||||||
// os.Exit(1)
|
// os.Exit(1)
|
||||||
// }
|
// }
|
||||||
|
|
||||||
store := getUserdataDb(dbDir, ctx)
|
userdataStore := getUserdataDb(dbDir, ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, err.Error())
|
fmt.Fprintf(os.Stderr, err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
@ -171,21 +171,27 @@ func main() {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
hl, err := getHandler(flagParser, dbResource, store)
|
hl, err := getHandler(flagParser, dbResource, userdataStore)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, err.Error())
|
fmt.Fprintf(os.Stderr, err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
sh := httpserver.NewSessionHandler(cfg, rs, store, hl.Init)
|
stateStore, err := getStateStore(dbDir, ctx)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, err.Error())
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
sh := httpserver.NewSessionHandler(cfg, rs, userdataStore, stateStore, hl.Init)
|
||||||
s := &http.Server{
|
s := &http.Server{
|
||||||
Addr: fmt.Sprintf("%s:%s", host, strconv.Itoa(port)),
|
Addr: fmt.Sprintf("%s:%s", host, strconv.Itoa(int(port))),
|
||||||
Handler: sh,
|
Handler: sh,
|
||||||
}
|
}
|
||||||
|
|
||||||
err = engine.Loop(ctx, en, os.Stdin, os.Stdout)
|
err = s.ListenAndServe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "loop exited with error: %v\n", err)
|
fmt.Fprintf(os.Stderr, "Server error: %s", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
20
cmd/main.go
20
cmd/main.go
@ -34,10 +34,11 @@ func getFlags(fp string, debug bool) (*asm.FlagParser, error) {
|
|||||||
|
|
||||||
func getHandler(appFlags *asm.FlagParser, rs *resource.DbResource, pe *persist.Persister, userdataStore db.Db) (*ussd.Handlers, error) {
|
func getHandler(appFlags *asm.FlagParser, rs *resource.DbResource, pe *persist.Persister, userdataStore db.Db) (*ussd.Handlers, error) {
|
||||||
|
|
||||||
ussdHandlers, err := ussd.NewHandlers(appFlags, pe, userdataStore)
|
ussdHandlers, err := ussd.NewHandlers(appFlags, userdataStore)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
ussdHandlers = ussdHandlers.WithPersister(pe)
|
||||||
rs.AddLocalFunc("select_language", ussdHandlers.SetLanguage)
|
rs.AddLocalFunc("select_language", ussdHandlers.SetLanguage)
|
||||||
rs.AddLocalFunc("create_account", ussdHandlers.CreateAccount)
|
rs.AddLocalFunc("create_account", ussdHandlers.CreateAccount)
|
||||||
rs.AddLocalFunc("save_pin", ussdHandlers.SavePin)
|
rs.AddLocalFunc("save_pin", ussdHandlers.SavePin)
|
||||||
@ -104,6 +105,11 @@ func getResource(resourceDir string, ctx context.Context) (resource.Resource, er
|
|||||||
return rfs, nil
|
return rfs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getEngine(cfg engine.Config, rs resource.Resource, pr *persist.Persister) *engine.DefaultEngine {
|
||||||
|
en := engine.NewEngine(cfg, rs)
|
||||||
|
en = en.WithPersister(pr)
|
||||||
|
return en
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var dbDir string
|
var dbDir string
|
||||||
@ -142,7 +148,7 @@ func main() {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
pr, err := getPersister(dbDir, ctx)
|
pe, err := getPersister(dbDir, ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, err.Error())
|
fmt.Fprintf(os.Stderr, err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
@ -156,13 +162,17 @@ func main() {
|
|||||||
|
|
||||||
dbResource, ok := rs.(*resource.DbResource)
|
dbResource, ok := rs.(*resource.DbResource)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
fmt.Fprintf(os.Stderr, err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
hl := getHandler(flagParser, dbResource, store)
|
hl, err := getHandler(flagParser, dbResource, pe, store)
|
||||||
hl = hl.WithPersister(pe)
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, err.Error())
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
en := getEngine(cfg, rs, pr)
|
en := getEngine(cfg, rs, pe)
|
||||||
en = en.WithFirst(hl.Init)
|
en = en.WithFirst(hl.Init)
|
||||||
if debug {
|
if debug {
|
||||||
en = en.WithDebug(nil)
|
en = en.WithDebug(nil)
|
||||||
|
@ -4,7 +4,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
|
||||||
|
|
||||||
"git.defalsify.org/vise.git/db"
|
"git.defalsify.org/vise.git/db"
|
||||||
"git.defalsify.org/vise.git/engine"
|
"git.defalsify.org/vise.git/engine"
|
||||||
@ -37,14 +36,16 @@ type SessionHandler struct {
|
|||||||
rp RequestParser
|
rp RequestParser
|
||||||
rs resource.Resource
|
rs resource.Resource
|
||||||
first resource.EntryFunc
|
first resource.EntryFunc
|
||||||
|
provider StorageProvider
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSessionHandler(cfg engine.Config, rs resource.Resource, userdataDb db.Db, first resource.EntryFunc) *SessionHandler {
|
func NewSessionHandler(cfg engine.Config, rs resource.Resource, stateDb db.Db, userdataDb db.Db, first resource.EntryFunc) *SessionHandler {
|
||||||
return &SessionHandler{
|
return &SessionHandler{
|
||||||
cfgTemplate: cfg,
|
cfgTemplate: cfg,
|
||||||
rs: rs,
|
rs: rs,
|
||||||
first: first,
|
first: first,
|
||||||
rp: RequestParser{},
|
rp: RequestParser{},
|
||||||
|
provider: NewSimpleStorageProvider(stateDb, userdataDb),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,7 +77,12 @@ func(f *SessionHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
|||||||
cfg := f.cfgTemplate
|
cfg := f.cfgTemplate
|
||||||
cfg.SessionId = sessionId
|
cfg.SessionId = sessionId
|
||||||
|
|
||||||
en := getEngine(cfg, f.rs, f.pr)
|
storage, err := f.provider.Get(cfg.SessionId)
|
||||||
|
if err != nil {
|
||||||
|
f.writeError(w, 500, "Storage retrieval fail", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
en := getEngine(cfg, f.rs, storage.Persister)
|
||||||
en = en.WithFirst(f.first)
|
en = en.WithFirst(f.first)
|
||||||
if cfg.EngineDebug {
|
if cfg.EngineDebug {
|
||||||
en = en.WithDebug(nil)
|
en = en.WithDebug(nil)
|
||||||
@ -87,17 +93,11 @@ func(f *SessionHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
|||||||
} else {
|
} else {
|
||||||
r, err = en.Exec(ctx, input)
|
r, err = en.Exec(ctx, input)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = en.Init(ctx)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "engine init exited with error: %v\n", err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
f.writeError(w, 500, "Engine exec fail", err)
|
f.writeError(w, 500, "Engine exec fail", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
w.WriteHeader(200)
|
w.WriteHeader(200)
|
||||||
w.Header().Set("Content-Type", "text/plain")
|
w.Header().Set("Content-Type", "text/plain")
|
||||||
_, err = en.WriteResult(ctx, w)
|
_, err = en.WriteResult(ctx, w)
|
||||||
@ -111,7 +111,6 @@ func(f *SessionHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
_ = r
|
_ = r
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getEngine(cfg engine.Config, rs resource.Resource, pr *persist.Persister) *engine.DefaultEngine {
|
func getEngine(cfg engine.Config, rs resource.Resource, pr *persist.Persister) *engine.DefaultEngine {
|
43
internal/http/storage.go
Normal file
43
internal/http/storage.go
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
package http
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.defalsify.org/vise.git/db"
|
||||||
|
"git.defalsify.org/vise.git/persist"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Storage struct {
|
||||||
|
Persister *persist.Persister
|
||||||
|
UserdataDb db.Db
|
||||||
|
}
|
||||||
|
|
||||||
|
type StorageProvider interface {
|
||||||
|
Get(sessionId string) (Storage, error)
|
||||||
|
Put(sessionId string, storage Storage) error
|
||||||
|
Close() error
|
||||||
|
}
|
||||||
|
|
||||||
|
type SimpleStorageProvider struct {
|
||||||
|
Storage
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSimpleStorageProvider(stateStore db.Db, userdataStore db.Db) StorageProvider {
|
||||||
|
pe := persist.NewPersister(stateStore)
|
||||||
|
return &SimpleStorageProvider{
|
||||||
|
Storage: Storage{
|
||||||
|
Persister: pe,
|
||||||
|
UserdataDb: userdataStore,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*SimpleStorageProvider) Get(sessionId string) (Storage, error) {
|
||||||
|
return Storage{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*SimpleStorageProvider) Put(sessionId string, storage Storage) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*SimpleStorageProvider) Close() error {
|
||||||
|
return nil
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user