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

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

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

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

type FileConfig struct {
25 26
  Paths []string `json:paths`
  Fields map[string]string `json:fields`
Jordan Sissel's avatar
Jordan Sissel committed
27
  //DeadTime time.Duration `json:"dead time"`
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
}

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",
               path, fi)
    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 {
Jordan Sissel's avatar
Jordan Sissel committed
55
    config.Network.Timeout = 15
56 57
  }

Jordan Sissel's avatar
Jordan Sissel committed
58 59
  config.Network.timeout = time.Duration(config.Network.Timeout) * time.Second

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

66 67
  return
}