func apiHandler(f RouteHandler) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { resp, err := f(w, r) w.Header().Set("Cache-Control", "must-revalidate") w.Header().Set("Content-Type", "application/json") if err != nil { if e, ok := err.(*APIResponse); ok { errString, _ := json.Marshal(err) w.WriteHeader(e.HTTPCode) w.Write(errString) } else { w.WriteHeader(500) w.Write([]byte(`{"message": "Server Error"}`)) } } else { if len(resp.Message) == 0 { resp.Message = "success" } j, err := json.Marshal(resp) if err != nil { log.Debug("apiHandler", err.Error()) j = []byte(`{"message": "Trouble marshalling the success response"}`) } w.WriteHeader(resp.HTTPCode) w.Write(j) } } }
func main() { var cPath string flag.StringVar(&cPath, "config", "", "Path to the config file") flag.Parse() conf, err := config.ParseConfig(cPath) if err != nil { fmt.Println("Error parsing config file:", err) os.Exit(1) } log.Init(conf.Log) log.Infof("main", "coordinator starting. Version: %s", version.Version.String()) log.Debug("main", "config:", *conf) err = manager.Init(conf) if err != nil { log.Fatalf("main", "Error initializing manager: %v", err) } httpAPI := new(httpapi.Server) address := strings.Split(conf.Host.Address, ":") if len(address) != 2 { log.Fatalf("main", "Invalid host address. Must be in form ip:port") } httpAPI.Configure(apihandler.Routes) httpAPI.Start(address[0], address[1]) }
func agentStatus(w http.ResponseWriter, r *http.Request) (*httpapi.APIResponse, error) { log.Debug("apihandler", "agentStatus called") status := make(map[string]interface{}) status["date"] = time.Now().String() status["status"] = manager.Status() return &httpapi.APIResponse{Data: status, HTTPCode: 200}, nil }
// Builds the file list for the backup // Listens for the cancel chanel to close to cancel walk // Walks the file tree in JobPaths and sends any found file that isn't excluded on the return chan // If there is an error, it sends the error on the error channel and returns func buildBackupFileList(cancel <-chan struct{}, jobPaths []spec.BackupPath) (<-chan string, <-chan error) { paths := make(chan string) errc := make(chan error, 1) go func() { log.Debug("backupJob", "file list routine started") defer close(paths) for _, jobPath := range jobPaths { log.Debugf("backupJob", "Walking filepath: %v", jobPath) errc <- filepath.Walk(jobPath.Path, func(path string, info os.FileInfo, err error) error { log.Debugf("backupJob", "Walk Found: %v", path) if err != nil { return err } if !info.Mode().IsRegular() { return nil } if shouldExclude(path, jobPath.Excludes) { return nil } select { case paths <- path: case <-cancel: log.Info("backupJob", "Walk Canceled") return errors.New("Walk Canceled") } return nil }) } }() return paths, errc }