lockserver/config/config.go

55 lines
1,011 B
Go
Raw Normal View History

package config
import (
"encoding/json"
"errors"
"os"
"github.com/sirupsen/logrus"
)
type Config struct {
2024-11-23 03:50:18 +00:00
ZWaveJSServer string `json:"zwave-js-server"`
SqliteDatabase string `json:"sqlite-database"`
HTTPBind string `json:"http-bind"`
GeneratedCodeLength int `json:"generated-code-length"`
}
var C = Config{
2024-11-23 03:50:18 +00:00
ZWaveJSServer: "ws://addon_core_zwave_js:3000",
SqliteDatabase: "/data/lockserver.db",
HTTPBind: ":8099",
GeneratedCodeLength: 10,
}
2024-04-24 00:53:17 +00:00
var configFiles = []string{"lockserver.json", "/etc/lockserver.json"}
func Load() error {
2024-04-24 00:53:17 +00:00
for _, path := range configFiles {
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
}