Skip to content

Commit

Permalink
set storage in context
Browse files Browse the repository at this point in the history
  • Loading branch information
gernest committed Feb 9, 2024
1 parent 5f06c00 commit 4477cc9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
11 changes: 11 additions & 0 deletions db/persistance.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package db

import (
"bytes"
"context"
"errors"
"fmt"
"log/slog"
Expand Down Expand Up @@ -200,6 +201,16 @@ type KV struct {
db *badger.DB
}

type dbKey struct{}

func With(ctx context.Context, kv Storage) context.Context {
return context.WithValue(ctx, dbKey{}, kv)
}

func Get(ctx context.Context) Storage {
return ctx.Value(dbKey{}).(Storage)
}

func NewKV(path string) (*KV, error) {
db, err := badger.Open(badger.DefaultOptions(filepath.Join(path, "db")).
WithLogger(&badgerLogger{
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func app() *cli.Command {
if err != nil {
return err
}

ctx = db.With(ctx, store)
log.Info("Setup session")
alloc := memory.NewGoAllocator()
log.Info("Loading primary index")
Expand Down

0 comments on commit 4477cc9

Please sign in to comment.