config.go 1.47 KB
Newer Older
1 2 3 4 5
package main

import (
  "encoding/json"
  "log"
6
  "os"
7
  "time"
8 9 10
)

type Config struct {
11
  Network NetworkConfig `json:network`
12
  Files   []FileConfig  `json:files`
13 14 15
}

type NetworkConfig struct {
16 17 18 19 20 21 22
  Servers        []string `json:servers`
  SSLCertificate string   `json:"ssl certificate"`
  SSLKey         string   `json:"ssl key"`
  SSLCA          string   `json:"ssl ca"`
  Timeout        int64    `json:timeout`
  timeout        time.Duration
}
23 24

type FileConfig struct {
25
  Paths  []string          `json:paths`
26
  Fields map[string]string `json:fields`
27
  //DeadTime time.Duration `json:"dead time"`
28 29 30 31 32 33 34 35 36 37 38 39
}

func LoadConfig(path string) (config Config, err error) {
  config_file, err := os.Open(path)
  if err != nil {
    log.Printf("Failed to open config file '%s': %s\n", path, err)
    return
  }

  fi, _ := config_file.Stat()
  if fi.Size() > (10 << 20) {
    log.Printf("Config file too large? Aborting, just in case. '%s' is %d bytes\n",
40
      path, fi)
41 42 43 44 45 46 47 48 49 50 51 52 53
    return
  }

  buffer := make([]byte, fi.Size())
  _, err = config_file.Read(buffer)
  log.Printf("%s\n", buffer)

  err = json.Unmarshal(buffer, &config)
  if err != nil {
    log.Printf("Failed unmarshalling json: %s\n", err)
    return
  }

54
  if config.Network.Timeout == 0 {
55
    config.Network.Timeout = 15
56 57
  }

58 59
  config.Network.timeout = time.Duration(config.Network.Timeout) * time.Second

60
  //for _, fileconfig := range config.Files {
61 62 63
  //if fileconfig.DeadTime == 0 {
  //fileconfig.DeadTime = 24 * time.Hour
  //}
64 65
  //}

66 67
  return
}