2024-04-09 04:25:36 +00:00
|
|
|
package config
|
|
|
|
|
2024-04-24 00:34:57 +00:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2024-04-09 04:25:36 +00:00
|
|
|
type Config struct {
|
2024-04-24 02:05:37 +00:00
|
|
|
ZWaveJSServer string `json:"zwave-js-server"`
|
|
|
|
SqliteDatabase string `json:"sqlite-database"`
|
|
|
|
HTTPBind string `json:"http-bind"`
|
2024-04-09 04:25:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var C = Config{
|
2024-04-24 04:19:36 +00:00
|
|
|
ZWaveJSServer: "ws://addon_core_zwave_js:3000",
|
2024-04-24 02:16:40 +00:00
|
|
|
SqliteDatabase: "/data/lockserver.db",
|
2024-04-24 06:06:16 +00:00
|
|
|
HTTPBind: ":8099",
|
2024-04-24 00:34:57 +00:00
|
|
|
}
|
|
|
|
|
2024-04-24 00:53:17 +00:00
|
|
|
var configFiles = []string{"lockserver.json", "/etc/lockserver.json"}
|
|
|
|
|
2024-04-24 00:34:57 +00:00
|
|
|
func Load() error {
|
2024-04-24 00:53:17 +00:00
|
|
|
for _, path := range configFiles {
|
2024-04-24 00:34:57 +00:00
|
|
|
err := load(path)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
logrus.WithField("file", path).Info("loaded config")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func load(path string) error {
|
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
if err := json.NewDecoder(f).Decode(&C); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|