miniwol/config/config.go

68 lines
1.0 KiB
Go
Raw Permalink Normal View History

2022-02-17 12:31:01 +00:00
package config
import (
"bytes"
"os"
"github.com/BurntSushi/toml"
)
type Device struct {
Alias string `form:"Alias"`
MAC string `form:"MAC"`
IP string `form:"IP"`
2022-02-17 12:31:01 +00:00
}
type config struct {
2022-04-28 09:08:11 +00:00
Server string
PassHash string
SessionTTL float64
StrictCookies bool
Devices []Device
2022-02-17 12:31:01 +00:00
}
var Config config
var configPath string
func init() {
Config = config{
2022-04-28 09:08:11 +00:00
Server: ":8080",
SessionTTL: 10,
StrictCookies: true,
2022-02-17 12:31:01 +00:00
}
// 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
}