66 lines
971 B
Go
66 lines
971 B
Go
package config
|
|
|
|
import (
|
|
"bytes"
|
|
"os"
|
|
|
|
"github.com/BurntSushi/toml"
|
|
)
|
|
|
|
type Device struct {
|
|
Alias string `form:"Alias"`
|
|
MAC string `form:"MAC"`
|
|
IP string `form:"IP"`
|
|
}
|
|
|
|
type config struct {
|
|
Server string
|
|
PassHash string
|
|
SessionTTL float64
|
|
Devices []Device
|
|
}
|
|
|
|
var Config config
|
|
|
|
var configPath string
|
|
|
|
func init() {
|
|
Config = config{
|
|
Server: ":8080",
|
|
SessionTTL: 10,
|
|
}
|
|
|
|
// Locations to look for a config file for
|
|
checkPaths := []string{
|
|
"config.toml",
|
|
"config/config.toml",
|
|
"/etc/miniwol/config.toml",
|
|
}
|
|
|
|
var err error
|
|
|
|
for _, path := range checkPaths {
|
|
_, err = toml.DecodeFile(path, &Config)
|
|
if err == nil {
|
|
configPath = path
|
|
return
|
|
}
|
|
}
|
|
panic(err)
|
|
}
|
|
|
|
func Save() error {
|
|
var buffer bytes.Buffer
|
|
var err error
|
|
encoder := toml.NewEncoder(&buffer)
|
|
err = encoder.Encode(Config)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = os.WriteFile(configPath, buffer.Bytes(), 0666)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|