diff --git a/README.md b/README.md index 104e755..5b80ac3 100644 --- a/README.md +++ b/README.md @@ -22,13 +22,12 @@ More information/doc on https://blog.csnet.me/gomotics/ A docker image is automatically build with Travis-CI. It is available on [Docker Hub](https://hub.docker.com/r/mch1307/gomotics/) -> docker run -d -P --net host --name gomotics mch1307/gomotics - ### Binaries Download your platform binary from the release page, extract the executable from the archive. ## Running +### Config file gomotics will run with default config if you do not provide a configuration file. If you want to link gomotics with Jeedom, provide the Jeedom URL and API key as follows ``` @@ -53,7 +52,23 @@ host = "x.x.x.x" port = 8000 ``` +### env variables + +Config can also be setup as env variable: +``` +LISTEN_PORT optional default 8081 +LOG_LEVEL optional default INFO +LOG_PATH optional default . (working dir) +JEE_URL mandatory for Jeedom +JEE_APIKEY mandatory for Jeedom +NHC_HOST optional autodiscover +NHC_PORT optional autodiscover on port 8000 +``` Then start gomotics as follows: -> gomotics -conf path/confg.toml \ No newline at end of file +> gomotics -conf path/confg.toml + +Or if using docker: + +> docker run -d -P --net host --name gomotics --JEE_URL=http://jeedom-host/core/api/jeeApi.php --JEE_APIKEY=abcdegf1234 mch1307/gomotics \ No newline at end of file diff --git a/config/config.go b/config/config.go index 02bfc2f..97d6ea3 100644 --- a/config/config.go +++ b/config/config.go @@ -3,6 +3,7 @@ package config import ( "fmt" "os" + "strconv" "github.com/BurntSushi/toml" ) @@ -38,27 +39,43 @@ type GlobalConfig struct { // Conf holds the global configuration var Conf GlobalConfig +func coalesce(str ...string) string { + for _, val := range str { + if val != "" { + return val + } + } + return "" +} + // Initialize populates the Conf variable func Initialize(cfg string) { Conf.JeedomConfig.Enabled = false - if _, err := os.Stat(cfg); err != nil { - //fmt.Println("Invalid config file/path: ", err) - wrkDir, _ := os.Getwd() - Conf.ServerConfig.LogPath = wrkDir - } else { - if _, err := toml.DecodeFile(cfg, &Conf); err != nil { - fmt.Println("Error parsing config file: ", err) + // load config file if any + if cfg != "" { + if _, err := os.Stat(cfg); err != nil { + wrkDir, _ := os.Getwd() + Conf.ServerConfig.LogPath = wrkDir + } else { + if _, err := toml.DecodeFile(cfg, &Conf); err != nil { + fmt.Println("Error parsing config file: ", err) + } } } - if Conf.ServerConfig.ListenPort == 0 { - Conf.ServerConfig.ListenPort = 8081 - } - if len(Conf.ServerConfig.LogLevel) == 0 { - Conf.ServerConfig.LogLevel = "INFO" - } + wrkDir, _ := os.Getwd() + listenPort, _ := strconv.Atoi(coalesce(os.Getenv("LISTEN_PORT"), strconv.Itoa(Conf.ServerConfig.ListenPort), "8081")) + Conf.ServerConfig.ListenPort = listenPort + Conf.ServerConfig.LogLevel = coalesce(os.Getenv("LOG_LEVEL"), Conf.ServerConfig.LogLevel, "INFO") + Conf.ServerConfig.LogPath = coalesce(os.Getenv("LOG_PATH"), Conf.ServerConfig.LogPath, wrkDir) + Conf.JeedomConfig.URL = coalesce(os.Getenv("JEE_URL"), Conf.JeedomConfig.URL) + Conf.JeedomConfig.APIKey = coalesce(os.Getenv("JEE_APIKEY"), Conf.JeedomConfig.APIKey) + Conf.NhcConfig.Host = coalesce(os.Getenv("NHC_HOST"), Conf.NhcConfig.Host) + nhcPort, _ := strconv.Atoi(coalesce(os.Getenv("NHC_PORT"), strconv.Itoa(Conf.NhcConfig.Port), "8000")) + Conf.NhcConfig.Port = nhcPort + if len(Conf.JeedomConfig.APIKey) > 0 { Conf.JeedomConfig.Enabled = true } - fmt.Printf("Starting with config: %+v", Conf.ServerConfig) + fmt.Printf("Starting with config: %+v", Conf) fmt.Println(" ") }