v0.0.28 Added --read-only flag & read_only: true|false config option. Closes gh-33
This commit is contained in:
parent
efbfe72bba
commit
19e047b142
28
main.go
28
main.go
|
@ -30,7 +30,7 @@ import (
|
||||||
var static embed.FS
|
var static embed.FS
|
||||||
|
|
||||||
var (
|
var (
|
||||||
version = "0.0.27"
|
version = "0.0.28"
|
||||||
device pingvin.Pingvin
|
device pingvin.Pingvin
|
||||||
config Conf
|
config Conf
|
||||||
usernamehash [32]byte
|
usernamehash [32]byte
|
||||||
|
@ -50,6 +50,7 @@ type Conf struct {
|
||||||
LogFile string `yaml:"log_file"`
|
LogFile string `yaml:"log_file"`
|
||||||
LogAccess bool `yaml:"log_access"`
|
LogAccess bool `yaml:"log_access"`
|
||||||
Debug bool `yaml:"debug"`
|
Debug bool `yaml:"debug"`
|
||||||
|
ReadOnly bool `yaml:"read_only"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// HTTP Basic Authentication middleware for http.HandlerFunc
|
// HTTP Basic Authentication middleware for http.HandlerFunc
|
||||||
|
@ -137,7 +138,11 @@ func coils(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
device.WriteCoil(uint16(intaddr), boolval)
|
if config.ReadOnly {
|
||||||
|
log.Println("WARNING: Read only mode, refusing to write to device")
|
||||||
|
} else {
|
||||||
|
device.WriteCoil(uint16(intaddr), boolval)
|
||||||
|
}
|
||||||
json.NewEncoder(w).Encode(device.Coils[intaddr])
|
json.NewEncoder(w).Encode(device.Coils[intaddr])
|
||||||
} else if len(pathparams[0]) > 0 && r.Method == "POST" && len(pathparams) == 1 {
|
} else if len(pathparams[0]) > 0 && r.Method == "POST" && len(pathparams) == 1 {
|
||||||
intaddr, err := strconv.Atoi(pathparams[0])
|
intaddr, err := strconv.Atoi(pathparams[0])
|
||||||
|
@ -146,7 +151,11 @@ func coils(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
device.WriteCoil(uint16(intaddr), !device.Coils[intaddr].Value)
|
if config.ReadOnly {
|
||||||
|
log.Println("WARNING: Read only mode, refusing to write to device")
|
||||||
|
} else {
|
||||||
|
device.WriteCoil(uint16(intaddr), !device.Coils[intaddr].Value)
|
||||||
|
}
|
||||||
json.NewEncoder(w).Encode(device.Coils[intaddr])
|
json.NewEncoder(w).Encode(device.Coils[intaddr])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -179,9 +188,13 @@ func registers(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
_, err = device.WriteRegister(uint16(intaddr), uint16(intval))
|
if config.ReadOnly {
|
||||||
if err != nil {
|
log.Println("WARNING: Read only mode, refusing to write to device")
|
||||||
log.Println(err)
|
} else {
|
||||||
|
_, err = device.WriteRegister(uint16(intaddr), uint16(intval))
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
json.NewEncoder(w).Encode(device.Registers[intaddr])
|
json.NewEncoder(w).Encode(device.Registers[intaddr])
|
||||||
}
|
}
|
||||||
|
@ -300,6 +313,7 @@ func initDefaultConfig(confpath string) {
|
||||||
LogAccess: false,
|
LogAccess: false,
|
||||||
LogFile: "",
|
LogFile: "",
|
||||||
Debug: false,
|
Debug: false,
|
||||||
|
ReadOnly: false,
|
||||||
}
|
}
|
||||||
conffile := confpath + "/configuration.yaml"
|
conffile := confpath + "/configuration.yaml"
|
||||||
confbytes, err := yaml.Marshal(&config)
|
confbytes, err := yaml.Marshal(&config)
|
||||||
|
@ -327,6 +341,7 @@ func configure() {
|
||||||
promflag := flag.Bool("enable-metrics", config.EnableMetrics, "Enable the built-in Prometheus exporter")
|
promflag := flag.Bool("enable-metrics", config.EnableMetrics, "Enable the built-in Prometheus exporter")
|
||||||
logflag := flag.String("logfile", config.LogFile, "Path to log file. Default is empty string, log to stdout")
|
logflag := flag.String("logfile", config.LogFile, "Path to log file. Default is empty string, log to stdout")
|
||||||
serialflag := flag.String("serial", config.SerialAddress, "Path to serial console for RS-485 connection. Defaults to /dev/ttyS0")
|
serialflag := flag.String("serial", config.SerialAddress, "Path to serial console for RS-485 connection. Defaults to /dev/ttyS0")
|
||||||
|
readOnly := flag.Bool("read-only", config.ReadOnly, "Read only mode, no writes to device are allowed")
|
||||||
// TODO: log file flag
|
// TODO: log file flag
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
config.Debug = *debugflag
|
config.Debug = *debugflag
|
||||||
|
@ -340,6 +355,7 @@ func configure() {
|
||||||
config.EnableMetrics = *promflag
|
config.EnableMetrics = *promflag
|
||||||
config.LogFile = *logflag
|
config.LogFile = *logflag
|
||||||
config.SerialAddress = *serialflag
|
config.SerialAddress = *serialflag
|
||||||
|
config.ReadOnly = *readOnly
|
||||||
usernamehash = sha256.Sum256([]byte(config.Username))
|
usernamehash = sha256.Sum256([]byte(config.Username))
|
||||||
passwordhash = sha256.Sum256([]byte(config.Password))
|
passwordhash = sha256.Sum256([]byte(config.Password))
|
||||||
if len(config.LogFile) != 0 {
|
if len(config.LogFile) != 0 {
|
||||||
|
|
Loading…
Reference in New Issue