forked from cerc-io/ipld-eth-server
Nest packages under pkg
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
package config
|
||||
|
||||
type Client struct {
|
||||
IPCPath string
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"fmt"
|
||||
|
||||
"path/filepath"
|
||||
|
||||
"path"
|
||||
"runtime"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Database Database
|
||||
Client Client
|
||||
}
|
||||
|
||||
func NewConfig(environment string) Config {
|
||||
filenameWithExtension := fmt.Sprintf("%s.toml", environment)
|
||||
absolutePath := filepath.Join(ProjectRoot(), "pkg", "config", "environments", filenameWithExtension)
|
||||
config := parseConfigFile(absolutePath)
|
||||
config.Client.IPCPath = filepath.Join(ProjectRoot(), config.Client.IPCPath)
|
||||
return config
|
||||
}
|
||||
|
||||
func ProjectRoot() string {
|
||||
var _, filename, _, _ = runtime.Caller(0)
|
||||
return path.Join(path.Dir(filename), "..", "..")
|
||||
}
|
||||
|
||||
func parseConfigFile(configfile string) Config {
|
||||
var cfg Config
|
||||
_, err := os.Stat(configfile)
|
||||
if err != nil {
|
||||
log.Fatal("Config file is missing: ", configfile)
|
||||
}
|
||||
|
||||
if _, err := toml.DecodeFile(configfile, &cfg); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return cfg
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package config_test
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestConfig(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
RunSpecs(t, "Config Suite")
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package config_test
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
|
||||
"github.com/8thlight/vulcanizedb/pkg/config"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("Loading the config", func() {
|
||||
|
||||
It("reads the private config using the environment", func() {
|
||||
privateConfig := config.NewConfig("private")
|
||||
|
||||
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(config.ProjectRoot(), "test_data_dir/geth.ipc")
|
||||
Expect(privateConfig.Client.IPCPath).To(Equal(expandedPath))
|
||||
})
|
||||
|
||||
})
|
||||
@@ -0,0 +1,13 @@
|
||||
package config
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Database struct {
|
||||
Hostname string
|
||||
Name string
|
||||
Port int
|
||||
}
|
||||
|
||||
func DbConnectionString(dbConfig Database) string {
|
||||
return fmt.Sprintf("postgresql://%s:%d/%s?sslmode=disable", dbConfig.Hostname, dbConfig.Port, dbConfig.Name)
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
[database]
|
||||
name = "vulcanize_private"
|
||||
hostname = "localhost"
|
||||
port = 5432
|
||||
|
||||
[client]
|
||||
ipcPath = "test_data_dir/geth.ipc"
|
||||
Reference in New Issue
Block a user