ipld-eth-server/pkg/config/config.go

49 lines
960 B
Go
Raw Normal View History

package config
2017-11-02 22:37:27 +00:00
import (
"log"
"os"
"fmt"
"path/filepath"
"path"
"runtime"
"github.com/BurntSushi/toml"
)
type Config struct {
Database Database
2017-11-02 22:37:27 +00:00
Client Client
}
func NewConfig(environment string) Config {
filenameWithExtension := fmt.Sprintf("%s.toml", environment)
2017-11-06 18:53:43 +00:00
absolutePath := filepath.Join(ProjectRoot(), "pkg", "config", "environments", filenameWithExtension)
2017-11-02 22:37:27 +00:00
config := parseConfigFile(absolutePath)
if !filepath.IsAbs(config.Client.IPCPath) {
config.Client.IPCPath = filepath.Join(ProjectRoot(), config.Client.IPCPath)
}
2017-11-02 22:37:27 +00:00
return config
}
func ProjectRoot() string {
var _, filename, _, _ = runtime.Caller(0)
2017-11-06 18:53:43 +00:00
return path.Join(path.Dir(filename), "..", "..")
2017-11-02 22:37:27 +00:00
}
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
}