Add bolt store

This commit is contained in:
2021-12-04 09:58:16 +01:00
parent 6d904c724b
commit bf9f8d80cd
8 changed files with 166 additions and 20 deletions

View File

@@ -10,7 +10,9 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"gitea.benny.dog/torjus/ezshare/store"
"github.com/pelletier/go-toml"
"google.golang.org/grpc/credentials"
)
@@ -26,23 +28,33 @@ type CertificatePaths struct {
}
type ServerConfig struct {
LogLevel string `toml:"LogLevel"`
Hostname string `toml:"Hostname"`
GRPC *ServerGRPCConfig `toml:"GRPC"`
HTTP *ServerHTTPConfig `toml:"HTTP"`
LogLevel string `toml:"LogLevel"`
Hostname string `toml:"Hostname"`
StoreConfig *ServerStoreConfig `toml:"Store"`
GRPC *ServerGRPCConfig `toml:"GRPC"`
HTTP *ServerHTTPConfig `toml:"HTTP"`
}
type ServerStoreConfig struct {
Type string `toml:"Type"`
FSStoreConfig *FSStoreConfig `toml:"Filesystem"`
Type string `toml:"Type"`
FSStoreConfig *FSStoreConfig `toml:"Filesystem"`
BoltStoreConfig *BoltStoreConfig `toml:"Bolt"`
}
type BoltStoreConfig struct {
Path string `toml:"Path"`
}
type FSStoreConfig struct {
Dir string `toml:"Dir"`
}
type ServerGRPCConfig struct {
ListenAddr string `toml:"ListenAddr"`
CACerts *CertificatePaths `toml:"CACerts"`
Certs *CertificatePaths `toml:"Certs"`
}
type ServerHTTPConfig struct {
ListenAddr string `toml:"ListenAddr"`
}
@@ -213,3 +225,24 @@ func (c *Config) ToDefaultFile() error {
}
return fmt.Errorf("config-file already exists")
}
func (sc *ServerStoreConfig) GetStore() (store.FileStore, func() error, error) {
nopCloseFunc := func() error { return nil }
if strings.EqualFold(sc.Type, "bolt") {
s, err := store.NewBoltStore(sc.BoltStoreConfig.Path)
if err != nil {
return nil, nil, err
}
return s, s.Close, err
}
if strings.EqualFold(sc.Type, "filesystem") {
s := store.NewFileSystemStore(sc.FSStoreConfig.Dir)
return s, nopCloseFunc, nil
}
if strings.EqualFold(sc.Type, "memory") {
return store.NewMemoryFileStore(), nopCloseFunc, nil
}
return nil, nil, fmt.Errorf("invalid store config")
}