diff --git a/go.mod b/go.mod index 42a3855..ad1039b 100644 --- a/go.mod +++ b/go.mod @@ -1,38 +1,50 @@ module github.com/ihexxa/quickshare -go 1.16 +go 1.18 require ( github.com/boltdb/bolt v1.3.1 github.com/dchest/captcha v0.0.0-20200903113550-03f5f0333e1f - github.com/elazarl/goproxy v0.0.0-20201021153353-00ad82a08272 // indirect github.com/gin-contrib/static v0.0.0-20200916080430-d45d9a37d28e github.com/gin-gonic/gin v1.7.3 - github.com/go-playground/validator/v10 v10.9.0 // indirect - github.com/golang/protobuf v1.5.2 // indirect - github.com/ihexxa/fsearch v0.1.2 // indirect + github.com/ihexxa/fsearch v0.1.2 github.com/ihexxa/gocfg v0.0.1 github.com/ihexxa/multipart v0.0.0-20210916083128-8584a3f00d1d - github.com/ihexxa/q-radix/v3 v3.0.5 - github.com/ihexxa/randstr v0.3.0 // indirect + github.com/ihexxa/randstr v0.3.0 github.com/jessevdk/go-flags v1.4.0 + github.com/mattn/go-sqlite3 v1.14.15 + github.com/natefinch/lumberjack v2.0.0+incompatible + github.com/parnurzeal/gorequest v0.2.16 + github.com/robbert229/jwt v2.0.0+incompatible + github.com/robfig/cron/v3 v3.0.1 + go.uber.org/zap v1.16.0 + golang.org/x/crypto v0.0.0-20210812204632-0ba0e8f03122 +) + +require ( + github.com/elazarl/goproxy v0.0.0-20201021153353-00ad82a08272 // indirect + github.com/gin-contrib/sse v0.1.0 // indirect + github.com/go-playground/locales v0.14.0 // indirect + github.com/go-playground/universal-translator v0.18.0 // indirect + github.com/go-playground/validator/v10 v10.9.0 // indirect + github.com/golang/protobuf v1.5.2 // indirect + github.com/ihexxa/q-radix/v3 v3.0.5 // indirect github.com/json-iterator/go v1.1.11 // indirect + github.com/leodido/go-urn v1.2.1 // indirect github.com/mattn/go-isatty v0.0.13 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.1 // indirect - github.com/natefinch/lumberjack v2.0.0+incompatible - github.com/parnurzeal/gorequest v0.2.16 github.com/pkg/errors v0.9.1 // indirect - github.com/robbert229/jwt v2.0.0+incompatible - github.com/robfig/cron/v3 v3.0.1 github.com/smartystreets/goconvey v1.6.4 // indirect - github.com/ugorji/go v1.2.6 // indirect - go.uber.org/zap v1.16.0 - golang.org/x/crypto v0.0.0-20210812204632-0ba0e8f03122 + github.com/ugorji/go/codec v1.2.6 // indirect + go.uber.org/atomic v1.6.0 // indirect + go.uber.org/multierr v1.5.0 // indirect + golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 // indirect golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e // indirect golang.org/x/text v0.3.7 // indirect google.golang.org/protobuf v1.27.1 // indirect gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect + gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect moul.io/http2curl v1.0.0 // indirect ) diff --git a/go.sum b/go.sum index 14be1b3..7c12a4e 100644 --- a/go.sum +++ b/go.sum @@ -49,8 +49,6 @@ github.com/ihexxa/gocfg v0.0.1 h1:3zsCHY/SYdqKSoo3pwTBWMgivEB7/ctpPPHL3p43UBg= github.com/ihexxa/gocfg v0.0.1/go.mod h1:oqDTq1ywx4Qi9DdhFwwMHoPCYv6Txrfj2SY5WWcgiJs= github.com/ihexxa/multipart v0.0.0-20210916083128-8584a3f00d1d h1:+v33khYHVDPEuuWO/JE1IzhoIu5uNvEcSs5GmXc5Sjw= github.com/ihexxa/multipart v0.0.0-20210916083128-8584a3f00d1d/go.mod h1:rhOAe/52S/J1fq1VnXvKX8FHuo65I+IcYUozW4M7+wE= -github.com/ihexxa/q-radix/v3 v3.0.4 h1:Hk+Av57Q+fwFtHobd5uQjOFdY/V31nl4p+K4mO3crDg= -github.com/ihexxa/q-radix/v3 v3.0.4/go.mod h1:DYXQuLlICQ2pCdPQOzyovzSPq0P8sKwxv5HhVQAwSVQ= github.com/ihexxa/q-radix/v3 v3.0.5 h1:dF3+NOZXSK3dSL/46BS5JC/gBe4kw7qiKITRPppo+rQ= github.com/ihexxa/q-radix/v3 v3.0.5/go.mod h1:DYXQuLlICQ2pCdPQOzyovzSPq0P8sKwxv5HhVQAwSVQ= github.com/ihexxa/randstr v0.3.0 h1:rXG67TwC2g71eSjMvXX1RauWc32wkZrdf6OOSIfun80= @@ -80,6 +78,8 @@ github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2y github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/mattn/go-isatty v0.0.13 h1:qdl+GuBjcsKKDco5BsxPJlId98mSWNKqYA+Co0SC1yA= github.com/mattn/go-isatty v0.0.13/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +github.com/mattn/go-sqlite3 v1.14.15 h1:vfoHhTN1af61xCRSWzFIWzx2YskyMTwHLrExkBOjvxI= +github.com/mattn/go-sqlite3 v1.14.15/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= @@ -116,7 +116,6 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= -github.com/ugorji/go v1.2.6 h1:tGiWC9HENWE2tqYycIqFTNorMmFRVhNwCpDOpWqnk8E= github.com/ugorji/go v1.2.6/go.mod h1:anCg0y61KIhDlPZmnH+so+RQbysYVyDko0IMgJv0Nn0= github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= github.com/ugorji/go/codec v1.2.6 h1:7kbGefxLoDBuYXOms4yD7223OpNMMPNPZxXk5TvFcyQ= diff --git a/src/db/rdb/interface.go b/src/db/rdb/interface.go new file mode 100644 index 0000000..63793cd --- /dev/null +++ b/src/db/rdb/interface.go @@ -0,0 +1,25 @@ +package rdb + +import ( + "context" + "database/sql" + _ "github.com/mattn/go-sqlite3" +) + +// TODO: expose more APIs if needed +type IDB interface { + BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error) + Close() error + PingContext(ctx context.Context) error + PrepareContext(ctx context.Context, query string) (*sql.Stmt, error) + ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error) + QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error) + QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row + // Conn(ctx context.Context) (*Conn, error) + // Driver() driver.Driver + // SetConnMaxIdleTime(d time.Duration) + // SetConnMaxLifetime(d time.Duration) + // SetMaxIdleConns(n int) + // SetMaxOpenConns(n int) + // Stats() DBStats +} diff --git a/src/db/rdb/sqlite/sqlite.go b/src/db/rdb/sqlite/sqlite.go new file mode 100644 index 0000000..4175461 --- /dev/null +++ b/src/db/rdb/sqlite/sqlite.go @@ -0,0 +1,25 @@ +package sqlite + +import ( + "database/sql" + _ "github.com/mattn/go-sqlite3" + + "github.com/ihexxa/quickshare/src/db/rdb" +) + +type SQLite struct { + rdb.IDB + dbPath string +} + +func NewSQLite(dbPath string) (*SQLite, error) { + db, err := sql.Open("sqlite3", dbPath) + if err != nil { + return nil, err + } + + return &SQLite{ + IDB: db, + dbPath: dbPath, + }, nil +} diff --git a/src/depidx/deps.go b/src/depidx/deps.go index b2a14ff..cdc663a 100644 --- a/src/depidx/deps.go +++ b/src/depidx/deps.go @@ -8,6 +8,7 @@ import ( "github.com/ihexxa/quickshare/src/cryptoutil" "github.com/ihexxa/quickshare/src/db/boltstore" "github.com/ihexxa/quickshare/src/db/fileinfostore" + "github.com/ihexxa/quickshare/src/db/rdb" "github.com/ihexxa/quickshare/src/db/sitestore" "github.com/ihexxa/quickshare/src/db/userstore" "github.com/ihexxa/quickshare/src/fs" @@ -40,6 +41,7 @@ type Deps struct { boltStore *boltstore.BoltStore cron cron.ICron fileIndex fileindex.IFileIndex + db rdb.IDB } func NewDeps(cfg gocfg.ICfg) *Deps { @@ -146,6 +148,14 @@ func (deps *Deps) FileIndex() fileindex.IFileIndex { return deps.fileIndex } -func (deps *Deps) SetIFileIndex(index fileindex.IFileIndex) { +func (deps *Deps) SetFileIndex(index fileindex.IFileIndex) { deps.fileIndex = index } + +func (deps *Deps) DB() rdb.IDB { + return deps.db +} + +func (deps *Deps) SetDB(db rdb.IDB) { + deps.db = db +} diff --git a/src/server/server.go b/src/server/server.go index 1a10364..cc023db 100644 --- a/src/server/server.go +++ b/src/server/server.go @@ -242,7 +242,7 @@ func initDeps(cfg gocfg.ICfg) *depidx.Deps { logger.Infof("failed to reindex file index: %s", err) } } - deps.SetIFileIndex(fileIndex) + deps.SetFileIndex(fileIndex) return deps }