Handle events
- Adds interfaces for developers to build handlers that update data in response to log events - Resolves #29
This commit is contained in:
@@ -1,79 +1,6 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"fmt"
|
||||
|
||||
"path/filepath"
|
||||
|
||||
"path"
|
||||
"runtime"
|
||||
|
||||
"errors"
|
||||
|
||||
"net/url"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Database Database
|
||||
Client Client
|
||||
}
|
||||
|
||||
var NewErrConfigFileNotFound = func(environment string) error {
|
||||
return errors.New(fmt.Sprintf("No configuration found for environment: %v", environment))
|
||||
}
|
||||
|
||||
var NewErrBadConnectionString = func(connectionString string) error {
|
||||
return errors.New(fmt.Sprintf("connection string is invalid: %v", connectionString))
|
||||
}
|
||||
|
||||
func NewConfig(environment string) (Config, error) {
|
||||
filenameWithExtension := fmt.Sprintf("%s.toml", environment)
|
||||
absolutePath := filepath.Join(ProjectRoot(), "environments", filenameWithExtension)
|
||||
config, err := parseConfigFile(absolutePath)
|
||||
if err != nil {
|
||||
return Config{}, NewErrConfigFileNotFound(environment)
|
||||
} else {
|
||||
if !filepath.IsAbs(config.Client.IPCPath) && !isUrl(config.Client.IPCPath) {
|
||||
config.Client.IPCPath = filepath.Join(ProjectRoot(), config.Client.IPCPath)
|
||||
}
|
||||
return config, nil
|
||||
}
|
||||
}
|
||||
|
||||
func ProjectRoot() string {
|
||||
var _, filename, _, _ = runtime.Caller(0)
|
||||
return path.Join(path.Dir(filename), "..", "..")
|
||||
}
|
||||
|
||||
func isUrl(s string) bool {
|
||||
_, err := url.ParseRequestURI(s)
|
||||
if err == nil {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func fileExists(s string) bool {
|
||||
_, err := os.Stat(s)
|
||||
if err == nil {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func parseConfigFile(filePath string) (Config, error) {
|
||||
var cfg Config
|
||||
if !isUrl(filePath) && !fileExists(filePath) {
|
||||
return Config{}, NewErrBadConnectionString(filePath)
|
||||
} else {
|
||||
_, err := toml.DecodeFile(filePath, &cfg)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
return cfg, nil
|
||||
}
|
||||
}
|
||||
|
||||
+10
-27
@@ -1,41 +1,24 @@
|
||||
package config_test
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
cfg "github.com/vulcanize/vulcanizedb/pkg/config"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var _ = Describe("Loading the config", func() {
|
||||
|
||||
It("reads the private config using the environment", func() {
|
||||
privateConfig, err := cfg.NewConfig("private")
|
||||
|
||||
testConfig := viper.New()
|
||||
testConfig.SetConfigName("private")
|
||||
testConfig.AddConfigPath("$GOPATH/src/github.com/vulcanize/vulcanizedb/environments/")
|
||||
err := testConfig.ReadInConfig()
|
||||
Expect(viper.Get("client.ipcpath")).To(BeNil())
|
||||
Expect(err).To(BeNil())
|
||||
Expect(privateConfig.Database.Hostname).To(Equal("localhost"))
|
||||
Expect(privateConfig.Database.Name).To(Equal("vulcanize_private"))
|
||||
Expect(privateConfig.Database.Port).To(Equal(5432))
|
||||
expandedPath := filepath.Join(cfg.ProjectRoot(), "test_data_dir/geth.ipc")
|
||||
Expect(privateConfig.Client.IPCPath).To(Equal(expandedPath))
|
||||
})
|
||||
|
||||
It("returns an error when there is no matching config file", func() {
|
||||
config, err := cfg.NewConfig("bad-config")
|
||||
|
||||
Expect(config).To(Equal(cfg.Config{}))
|
||||
Expect(err).NotTo(BeNil())
|
||||
})
|
||||
|
||||
It("reads the infura config using the environment", func() {
|
||||
infuraConfig, err := cfg.NewConfig("infura")
|
||||
|
||||
Expect(err).To(BeNil())
|
||||
Expect(infuraConfig.Database.Hostname).To(Equal("localhost"))
|
||||
Expect(infuraConfig.Database.Name).To(Equal("vulcanize_private"))
|
||||
Expect(infuraConfig.Database.Port).To(Equal(5432))
|
||||
Expect(infuraConfig.Client.IPCPath).To(Equal("https://mainnet.infura.io/J5Vd2fRtGsw0zZ0Ov3BL"))
|
||||
Expect(testConfig.Get("database.hostname")).To(Equal("localhost"))
|
||||
Expect(testConfig.Get("database.name")).To(Equal("vulcanize_private"))
|
||||
Expect(testConfig.Get("database.port")).To(Equal(int64(5432)))
|
||||
Expect(testConfig.Get("client.ipcpath")).To(Equal("test_data_dir/geth.ipc"))
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user