Update config to be passed in as environment variables
This commit is contained in:
34
cmd/invertergui/config.go
Normal file
34
cmd/invertergui/config.go
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/jessevdk/go-flags"
|
||||||
|
)
|
||||||
|
|
||||||
|
type config struct {
|
||||||
|
Address string `long:"address" env:"ADDRESS" default:":8080" description:"The IP/DNS and port of the machine that the application is running on."`
|
||||||
|
Data struct {
|
||||||
|
Source string `long:"data.source" env:"DATA_SOURCE" default:"serial" description:"Set the source of data for the inverter gui. \"serial\", \"tcp\" or \"mock\""`
|
||||||
|
Host string `long:"data.host" env:"DATA_HOST" default:"localhost:8139" description:"Host to connect when source is set to tcp."`
|
||||||
|
Device string `long:"data.device" env:"DATA_DEVICE" default:"/dev/ttyUSB0" description:"TTY device to use when source is set to serial."`
|
||||||
|
}
|
||||||
|
Cli struct {
|
||||||
|
Enabled bool `long:"cli.enabled" env:"CLI_ENABLED" description:"Enable CLI output."`
|
||||||
|
}
|
||||||
|
MQTT struct {
|
||||||
|
Enabled bool `long:"mqtt.enabled" env:"MQTT_ENABLED" description:"Enable MQTT publishing."`
|
||||||
|
Broker string `long:"mqtt.broker" env:"MQTT_BROKER" default:"tcp://localhost:1883" description:"Set the host port and scheme of the MQTT broker."`
|
||||||
|
ClientID string `long:"mqtt.client_id" env:"MQTT_CLIENT_ID" default:"interter-gui" description:"Set the client ID for the MQTT connection."`
|
||||||
|
Topic string `long:"mqtt.topic" env:"MQTT_TOPIC" default:"invertergui/updates" description:"Set the MQTT topic updates published to."`
|
||||||
|
Username string `long:"mqtt.username" env:"MQTT_USERNAME" default:"" description:"Set the MQTT username"`
|
||||||
|
Password string `long:"mqtt.password" env:"MQTT_PASSWORD" default:"" description:"Set the MQTT password"`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseConfig() (*config, error) {
|
||||||
|
conf := &config{}
|
||||||
|
parser := flags.NewParser(conf, flags.Default)
|
||||||
|
if _, err := parser.Parse(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return conf, nil
|
||||||
|
}
|
||||||
@@ -31,7 +31,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
@@ -41,6 +40,7 @@ import (
|
|||||||
"github.com/diebietse/invertergui/mk2core"
|
"github.com/diebietse/invertergui/mk2core"
|
||||||
"github.com/diebietse/invertergui/mk2driver"
|
"github.com/diebietse/invertergui/mk2driver"
|
||||||
"github.com/diebietse/invertergui/plugins/cli"
|
"github.com/diebietse/invertergui/plugins/cli"
|
||||||
|
"github.com/diebietse/invertergui/plugins/mqttclient"
|
||||||
"github.com/diebietse/invertergui/plugins/munin"
|
"github.com/diebietse/invertergui/plugins/munin"
|
||||||
"github.com/diebietse/invertergui/plugins/prometheus"
|
"github.com/diebietse/invertergui/plugins/prometheus"
|
||||||
"github.com/diebietse/invertergui/plugins/webui"
|
"github.com/diebietse/invertergui/plugins/webui"
|
||||||
@@ -50,19 +50,17 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
source := flag.String("source", "serial", "Set the source of data for the inverter gui. \"serial\", \"tcp\" or \"mock\"")
|
conf, err := parseConfig()
|
||||||
addr := flag.String("addr", ":8080", "TCP address to listen on.")
|
if err != nil {
|
||||||
ip := flag.String("ip", "localhost:8139", "IP to connect when using tcp connection.")
|
os.Exit(1)
|
||||||
dev := flag.String("dev", "/dev/ttyUSB0", "TTY device to use.")
|
}
|
||||||
cliEnable := flag.Bool("cli", false, "Enable CLI output")
|
|
||||||
flag.Parse()
|
|
||||||
|
|
||||||
mk2 := getMk2Device(*source, *ip, *dev)
|
mk2 := getMk2Device(conf.Data.Source, conf.Data.Host, conf.Data.Device)
|
||||||
defer mk2.Close()
|
defer mk2.Close()
|
||||||
|
|
||||||
core := mk2core.NewCore(mk2)
|
core := mk2core.NewCore(mk2)
|
||||||
|
|
||||||
if *cliEnable {
|
if conf.Cli.Enabled {
|
||||||
cli.NewCli(core.NewSubscription())
|
cli.NewCli(core.NewSubscription())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,7 +78,21 @@ func main() {
|
|||||||
prometheus.NewPrometheus(core.NewSubscription())
|
prometheus.NewPrometheus(core.NewSubscription())
|
||||||
http.Handle("/metrics", promhttp.Handler())
|
http.Handle("/metrics", promhttp.Handler())
|
||||||
|
|
||||||
log.Fatal(http.ListenAndServe(*addr, nil))
|
// MQTT
|
||||||
|
if conf.MQTT.Enabled {
|
||||||
|
mqttConf := mqttclient.Config{
|
||||||
|
Broker: conf.MQTT.Broker,
|
||||||
|
Topic: conf.MQTT.Topic,
|
||||||
|
ClientID: conf.MQTT.ClientID,
|
||||||
|
Username: conf.MQTT.Username,
|
||||||
|
Password: conf.MQTT.Password,
|
||||||
|
}
|
||||||
|
if err := mqttclient.New(core.NewSubscription(), mqttConf); err != nil {
|
||||||
|
log.Printf("Could not setup MQTT client: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Fatal(http.ListenAndServe(conf.Address, nil))
|
||||||
}
|
}
|
||||||
|
|
||||||
func getMk2Device(source, ip, dev string) mk2driver.Mk2 {
|
func getMk2Device(source, ip, dev string) mk2driver.Mk2 {
|
||||||
|
|||||||
Reference in New Issue
Block a user