config.go 960 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package main
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "os"
  6. )
  7. var configFile = "config.json"
  8. var config = &configMain{
  9. Admins: map[string]configAdmin{},
  10. Boards: map[string]configBoard{},
  11. }
  12. type configMain struct{
  13. Admins map[string]configAdmin `json:"admins"`
  14. Boards map[string]configBoard `json:"boards"`
  15. }
  16. type configAdmin struct{
  17. Username string `json:"user"`
  18. Hash []byte `json:"hash"`
  19. }
  20. type configBoard struct{
  21. ID uint16 `json:"ID"`
  22. KEY []byte `json:"KEY"`
  23. }
  24. func loadConfig() {
  25. if _, err := os.Stat(configFile); os.IsNotExist(err) {
  26. return
  27. }
  28. jsonFile, err := os.Open(configFile)
  29. errCheckExit(err, "Unable to open config file")
  30. defer jsonFile.Close()
  31. byteValue, _ := ioutil.ReadAll(jsonFile)
  32. err = json.Unmarshal(byteValue, config)
  33. errCheck(err, "Failed to unmarshal config")
  34. }
  35. func saveConfig() {
  36. jsonValue, _ := json.Marshal(config)
  37. err := ioutil.WriteFile(configFile, jsonValue, 0644)
  38. errCheck(err, "Failed to write config")
  39. }