ipld-eth-server/config/config.go

47 lines
899 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)
absolutePath := filepath.Join(ProjectRoot(), "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
}