commit 9828fc359d69a95eff6d7488d2820e68cfac61cf
parent 80788f044129b600cc535d9c88c2ec192a4d8ceb
Author: lash <dev@holbrook.no>
Date: Fri, 30 Aug 2024 04:33:50 +0100
Add session id in db interface
Diffstat:
5 files changed, 23 insertions(+), 13 deletions(-)
diff --git a/db/db.go b/db/db.go
@@ -11,12 +11,13 @@ const (
DATATYPE_BIN
DATATYPE_TEMPLATE
DATATYPE_STATE
+ DATATYPE_USERSTART
)
type Db interface {
Connect(ctx context.Context, connStr string) error
- Get(ctx context.Context, key []byte) ([]byte, error)
- Put(ctx context.Context, key []byte, val []byte) error
+ Get(ctx context.Context, sessionId string, key []byte) ([]byte, error)
+ Put(ctx context.Context, sessionId string, key []byte, val []byte) error
}
func ToDbKey(typ uint8, s string, l *lang.Language) []byte {
diff --git a/db/fs.go b/db/fs.go
@@ -25,8 +25,8 @@ func(fdb *FsDb) Connect(ctx context.Context, connStr string) error {
return nil
}
-func(fdb *FsDb) Get(ctx context.Context, key []byte) ([]byte, error) {
- fp := fdb.pathFor(key)
+func(fdb *FsDb) Get(ctx context.Context, sessionId string, key []byte) ([]byte, error) {
+ fp := fdb.pathFor(sessionId, key)
f, err := os.Open(fp)
if err != nil {
return nil, err
@@ -39,11 +39,14 @@ func(fdb *FsDb) Get(ctx context.Context, key []byte) ([]byte, error) {
return b, nil
}
-func(fdb *FsDb) Put(ctx context.Context, key []byte, val []byte) error {
- fp := fdb.pathFor(key)
+func(fdb *FsDb) Put(ctx context.Context, sessionId string, key []byte, val []byte) error {
+ fp := fdb.pathFor(sessionId, key)
return ioutil.WriteFile(fp, val, 0600)
}
-func(fdb *FsDb) pathFor(key []byte) string{
- return path.Join(fdb.dir, string(key))
+func(fdb *FsDb) pathFor(sessionId string, key []byte) string{
+ k := sessionId + "." + string(key)
+ kb := ToDbKey(DATATYPE_USERSTART, k, nil)
+ kb[0] += 30
+ return path.Join(fdb.dir, string(kb))
}
diff --git a/db/fs_test.go b/db/fs_test.go
@@ -9,6 +9,7 @@ import (
func TestPutGet(t *testing.T) {
ctx := context.Background()
+ sid := "ses"
d, err := ioutil.TempDir("", "vise-db-*")
if err != nil {
t.Fatal(err)
@@ -18,18 +19,18 @@ func TestPutGet(t *testing.T) {
if err != nil {
t.Fatal(err)
}
- err = db.Put(ctx, []byte("foo"), []byte("bar"))
+ err = db.Put(ctx, sid, []byte("foo"), []byte("bar"))
if err != nil {
t.Fatal(err)
}
- v, err := db.Get(ctx, []byte("foo"))
+ v, err := db.Get(ctx, sid, []byte("foo"))
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(v, []byte("bar")) {
t.Fatalf("expected value 'bar', found '%s'", v)
}
- _, err = db.Get(ctx, []byte("bar"))
+ _, err = db.Get(ctx, sid, []byte("bar"))
if err == nil {
t.Fatal("expected get error for key 'bar'")
}
diff --git a/db/pg.go b/db/pg.go
@@ -78,10 +78,14 @@ func(pdb *PgDb) prepare(ctx context.Context) error {
return nil
}
-func(pdb *PgDb) Put(ctx context.Context, key []byte, val []byte) error {
+func(pdb *PgDb) domainId(ctx context.Context, domain string) {
+
+}
+
+func(pdb *PgDb) Put(ctx context.Context, sessionId string, key []byte, val []byte) error {
return nil
}
-func(pdb *PgDb) Get(ctx context.Context, key []byte) ([]byte, error) {
+func(pdb *PgDb) Get(ctx context.Context, sessionId string, key []byte) ([]byte, error) {
return nil, nil
}
diff --git a/db/pg_test.go b/db/pg_test.go
@@ -6,6 +6,7 @@ import (
)
func TestCreate(t *testing.T) {
+ t.Skip("need postgresql mock")
db := NewPgDb().WithSchema("govise")
ctx := context.Background()
err := db.Connect(ctx, "postgres://vise:esiv@localhost:5432/visedb")