1
0
Fork 0
hats/pkg/config/config.go

64 lines
2.0 KiB
Go
Raw Permalink Normal View History

2023-10-12 17:23:35 +00:00
package config
import (
"fmt"
"strconv"
"code.jhot.me/jhot/hats/internal/util"
)
type HatsConfig struct {
HomeAssistantHost string
HomeAssistantPort string
HomeAssistantSecure bool
HomeAssistantBaseUrl string
HomeAssistantWebsocketUrl string
HomeAssistantToken string
NatsHost string
NatsPort string
NatsBaseUrl string
NatsToken string
NatsClientName string
2023-10-13 20:31:23 +00:00
HatsHost string
HatsPort string
HatsSecure bool
HatsBaseUrl string
2023-10-12 17:23:35 +00:00
}
func FromEnvironment() *HatsConfig {
config := &HatsConfig{
HomeAssistantHost: util.GetEnvWithDefault("HASS_HOST", "127.0.0.1"),
HomeAssistantPort: util.GetEnvWithDefault("HASS_PORT", "8123"),
HomeAssistantToken: util.GetEnvWithDefault("HASS_TOKEN", ""),
NatsHost: util.GetEnvWithDefault("NATS_HOST", "127.0.0.1"),
NatsPort: util.GetEnvWithDefault("NATS_PORT", "4222"),
NatsToken: util.GetEnvWithDefault("NATS_TOKEN", ""),
NatsClientName: util.GetEnvWithDefault("NATS_CLIENT_NAME", "hats"),
2023-10-13 20:31:23 +00:00
HatsHost: util.GetEnvWithDefault("HATS_HOST", "hats"),
HatsPort: util.GetEnvWithDefault("HATS_PORT", "8888"),
2023-10-12 17:23:35 +00:00
}
config.HomeAssistantSecure, _ = strconv.ParseBool(util.GetEnvWithDefault("HASS_SECURE", "false"))
hassProtocol := "http"
HassWsProtocol := "ws"
if config.HomeAssistantSecure {
hassProtocol += "s"
HassWsProtocol += "s"
}
config.HomeAssistantBaseUrl = fmt.Sprintf("%s://%s:%s", hassProtocol, config.HomeAssistantHost, config.HomeAssistantPort)
config.HomeAssistantWebsocketUrl = fmt.Sprintf("%s://%s:%s/api/websocket", HassWsProtocol, config.HomeAssistantHost, config.HomeAssistantPort)
2023-10-13 20:31:23 +00:00
2023-10-12 17:23:35 +00:00
config.NatsBaseUrl = fmt.Sprintf("nats://%s:%s", config.NatsHost, config.NatsPort)
2023-10-13 20:31:23 +00:00
config.HatsSecure, _ = strconv.ParseBool(util.GetEnvWithDefault("HATS_SECURE", "false"))
hatsProtocol := "http"
if config.HatsSecure {
hatsProtocol += "s"
}
config.HatsBaseUrl = fmt.Sprintf("%s://%s:%s", hatsProtocol, config.HatsHost, config.HatsPort)
2023-10-12 17:23:35 +00:00
return config
}