示例#1
0
func diffBackup(store configstore.Store, w http.ResponseWriter, req *http.Request) {
	vars := mux.Vars(req)

	t1, err := time.Parse(file.DefaultDateFormat, vars["date1"])
	if err != nil {
		log.Printf("Error: %s", err)
		http.Error(w, "error :(", 500)
		return
	}

	t2, err := time.Parse(file.DefaultDateFormat, vars["date2"])
	if err != nil {
		log.Printf("Error: %s", err)
		http.Error(w, "error :(", 500)
		return
	}

	e1 := configstore.Entry{
		Name: vars["hostname"],
		Date: t1,
	}

	e2 := configstore.Entry{
		Name: vars["hostname"],
		Date: t2,
	}

	err = store.Get(&e1)
	if err != nil {
		log.Printf("Error: %s", err)
		http.Error(w, "error :(", 500)
		return
	}

	err = store.Get(&e2)
	if err != nil {
		log.Printf("Error: %s", err)
		http.Error(w, "error :(", 500)
		return
	}

	dmp := diffmatchpatch.New()
	diffs := dmp.DiffMain(e1.Content.String(), e2.Content.String(), true)

	em := struct {
		Hostname string
		Date1    time.Time
		Date2    time.Time
		Diff     string
	}{
		vars["hostname"],
		t1,
		t2,
		dmp.DiffPrettyHtml(diffs),
	}

	io.WriteString(w, mustache.RenderFileInLayout("templates/diff.html.mustache", "templates/layout.html.mustache", em))
}
示例#2
0
func hostBackup(store configstore.Store, w http.ResponseWriter, req *http.Request) {
	vars := mux.Vars(req)

	t, err := time.Parse(file.DefaultDateFormat, vars["date"])
	if err != nil {
		log.Printf("Error: %s", err)
		http.Error(w, "error :(", 500)
		return
	}

	e := configstore.Entry{
		Name: vars["hostname"],
		Date: t,
	}

	err = store.Get(&e)
	if err != nil {
		log.Printf("Error: %s", err)
		http.Error(w, "error :(", 500)
		return
	}

	if req.Header.Get("Accept") == "application/json" {
		w.Header().Set("Content-type", "application/json")

		enc := json.NewEncoder(w)
		err := enc.Encode(struct {
			Hostname string
			Date     string
			Content  string
		}{
			Hostname: e.Name,
			Date:     e.Date.Format(file.DefaultDateFormat),
			Content:  e.Content.String(),
		})
		if err != nil {
			log.Printf("Error: %s", err)
			http.Error(w, "error :(", 500)
			return
		}
	} else {

		em := struct {
			Hostname string
			Date     string
			Content  string
		}{
			vars["hostname"],
			t.String(),
			e.Content.String(),
		}

		io.WriteString(w, mustache.RenderFileInLayout("templates/entry.html.mustache", "templates/layout.html.mustache", em))
	}
}
示例#3
0
func showBackupDate(store configstore.Store, w http.ResponseWriter, req *http.Request) {
	vars := mux.Vars(req)

	t, err := time.Parse(file.DefaultDateFormat, vars["date"])
	if err != nil {
		log.Printf("Error: %s", err)
		http.Error(w, "error :(", 500)
		return
	}

	datesStore, err := store.Dates(vars["hostname"])
	if err != nil {
		log.Printf("Error: %s", err)
		http.Error(w, "error :(", 500)
		return
	}

	// Find the backup on or after the specified date
	var idx int
	for i, date := range datesStore {
		if t.Before(date) {
			idx = i - 1
		}
	}

	if idx == -1 {
		idx = 0
	}

	e := configstore.Entry{
		Name: vars["hostname"],
		Date: datesStore[idx],
	}

	err = store.Get(&e)
	if err != nil {
		log.Printf("Error: %s", err)
		http.Error(w, "error :(", 500)
		return
	}

	em := struct {
		Hostname string
		Date     string
		Content  string
	}{
		vars["hostname"],
		datesStore[idx].String(),
		e.Content.String(),
	}

	io.WriteString(w, mustache.RenderFileInLayout("templates/entry.html.mustache", "templates/layout.html.mustache", em))
}