Skip to content

Commit

Permalink
Replace BadgerDB with bbolt
Browse files Browse the repository at this point in the history
  • Loading branch information
dstotijn committed Jan 13, 2025
1 parent 24c2ecf commit fcf0e1c
Show file tree
Hide file tree
Showing 23 changed files with 1,153 additions and 1,244 deletions.
27 changes: 11 additions & 16 deletions cmd/hetty/hetty.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@ import (
"strings"

"github.com/chromedp/chromedp"
badgerdb "github.com/dgraph-io/badger/v3"
"github.com/gorilla/mux"
"github.com/mitchellh/go-homedir"
"github.com/peterbourgon/ff/v3/ffcli"
"go.etcd.io/bbolt"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"

"github.com/dstotijn/hetty/pkg/api"
"github.com/dstotijn/hetty/pkg/chrome"
"github.com/dstotijn/hetty/pkg/db/badger"
"github.com/dstotijn/hetty/pkg/db/bolt"
"github.com/dstotijn/hetty/pkg/proj"
"github.com/dstotijn/hetty/pkg/proxy"
"github.com/dstotijn/hetty/pkg/proxy/intercept"
Expand Down Expand Up @@ -89,7 +88,7 @@ func NewHettyCommand() (*ffcli.Command, *Config) {
"Path to root CA certificate. Creates a new certificate if file doesn't exist.")
fs.StringVar(&cmd.key, "key", "~/.hetty/hetty_key.pem",
"Path to root CA private key. Creates a new private key if file doesn't exist.")
fs.StringVar(&cmd.db, "db", "~/.hetty/db", "Database directory path.")
fs.StringVar(&cmd.db, "db", "~/.hetty/hetty.db", "Database file path. Creates file if it doesn't exist.")
fs.StringVar(&cmd.addr, "addr", ":8080", "TCP address to listen on, in the form \"host:port\".")
fs.BoolVar(&cmd.chrome, "chrome", false, "Launch Chrome with proxy settings applied and certificate errors ignored.")
fs.BoolVar(&cmd.version, "version", false, "Output version.")
Expand Down Expand Up @@ -154,25 +153,21 @@ func (cmd *HettyCommand) Exec(ctx context.Context, _ []string) error {
cmd.config.logger.Fatal("Failed to load or create CA key pair.", zap.Error(err))
}

// BadgerDB logs some verbose entries with `INFO` level, so unless
// we're running in debug mode, bump the minimal level to `WARN`.
dbLogger := cmd.config.logger.Named("badgerdb").WithOptions(zap.IncreaseLevel(zapcore.WarnLevel))
dbLogger := cmd.config.logger.Named("boltdb").Sugar()
boltOpts := *bbolt.DefaultOptions
boltOpts.Logger = &bolt.Logger{SugaredLogger: dbLogger}

dbSugaredLogger := dbLogger.Sugar()

badger, err := badger.OpenDatabase(
badgerdb.DefaultOptions(dbPath).WithLogger(badger.NewLogger(dbSugaredLogger)),
)
boltDB, err := bolt.OpenDatabase(dbPath, &boltOpts)
if err != nil {
cmd.config.logger.Fatal("Failed to open database.", zap.Error(err))
}
defer badger.Close()
defer boltDB.Close()

scope := &scope.Scope{}

reqLogService := reqlog.NewService(reqlog.Config{
Scope: scope,
Repository: badger,
Repository: boltDB,
Logger: cmd.config.logger.Named("reqlog").Sugar(),
})

Expand All @@ -181,12 +176,12 @@ func (cmd *HettyCommand) Exec(ctx context.Context, _ []string) error {
})

senderService := sender.NewService(sender.Config{
Repository: badger,
Repository: boltDB,
ReqLogService: reqLogService,
})

projService, err := proj.NewService(proj.Config{
Repository: badger,
Repository: boltDB,
InterceptService: interceptService,
ReqLogService: reqLogService,
SenderService: senderService,
Expand Down
21 changes: 5 additions & 16 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,43 +1,34 @@
module github.com/dstotijn/hetty

go 1.17
go 1.23

toolchain go1.23.4

require (
github.com/99designs/gqlgen v0.14.0
github.com/chromedp/chromedp v0.7.8
github.com/dgraph-io/badger/v3 v3.2103.2
github.com/google/go-cmp v0.5.6
github.com/gorilla/mux v1.7.4
github.com/mitchellh/go-homedir v1.1.0
github.com/oklog/ulid v1.3.1
github.com/peterbourgon/ff/v3 v3.1.2
github.com/smallstep/truststore v0.11.0
github.com/vektah/gqlparser/v2 v2.2.0
go.etcd.io/bbolt v1.4.0-beta.0
go.uber.org/zap v1.21.0
)

require (
github.com/agnivade/levenshtein v1.1.0 // indirect
github.com/cespare/xxhash v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/chromedp/cdproto v0.0.0-20220217222649-d8c14a5c6edf // indirect
github.com/chromedp/sysutil v1.0.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d // indirect
github.com/dgraph-io/ristretto v0.1.0 // indirect
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/gobwas/httphead v0.1.0 // indirect
github.com/gobwas/pool v0.2.1 // indirect
github.com/gobwas/ws v1.1.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6 // indirect
github.com/golang/protobuf v1.3.1 // indirect
github.com/golang/snappy v0.0.3 // indirect
github.com/google/flatbuffers v1.12.1 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/hashicorp/golang-lru v0.5.1 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/klauspost/compress v1.12.3 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/matryer/moq v0.2.5 // indirect
github.com/mitchellh/mapstructure v1.1.2 // indirect
Expand All @@ -46,12 +37,10 @@ require (
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/urfave/cli/v2 v2.1.1 // indirect
github.com/vektah/dataloaden v0.2.1-0.20190515034641-a19b9a6e7c9e // indirect
go.opencensus.io v0.22.5 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
golang.org/x/mod v0.4.2 // indirect
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 // indirect
golang.org/x/sys v0.0.0-20220209214540-3681064d5158 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/tools v0.1.5 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
gopkg.in/yaml.v2 v2.2.8 // indirect
Expand Down
Loading

0 comments on commit fcf0e1c

Please sign in to comment.