Skip to content

Commit

Permalink
add config through env variables
Browse files Browse the repository at this point in the history
  • Loading branch information
mch1307 committed Nov 3, 2017
1 parent 9eff4ad commit 3c09634
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 17 deletions.
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

```
Expand All @@ -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
> 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
45 changes: 31 additions & 14 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"fmt"
"os"
"strconv"

"github.com/BurntSushi/toml"
)
Expand Down Expand Up @@ -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(" ")
}

0 comments on commit 3c09634

Please sign in to comment.