Beispiel #1
0
func listDates(store configstore.Store, w http.ResponseWriter, req *http.Request) {
	vars := mux.Vars(req)
	datesStore, err := store.Dates(vars["hostname"])
	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")

		var dates []string
		for _, date := range datesStore {
			dates = append(dates, date.Format(file.DefaultDateFormat))
		}

		enc := json.NewEncoder(w)
		err := enc.Encode(struct {
			Hostname string
			Dates    []string
		}{
			Hostname: vars["hostname"],
			Dates:    dates,
		})
		if err != nil {
			log.Printf("Error: %s", err)
			http.Error(w, "error :(", 500)
			return
		}
	} else {

		var data = struct {
			Hostname string
			Dates    []struct {
				String string
				URL    string
			}
		}{
			Hostname: vars["hostname"],
		}
		for _, date := range datesStore {
			data.Dates = append(data.Dates, struct {
				String string
				URL    string
			}{
				String: date.String(),
				URL:    date.Format(file.DefaultDateFormat),
			})
		}

		io.WriteString(w, mustache.RenderFileInLayout("templates/dates.html.mustache", "templates/layout.html.mustache", data))
	}
}
Beispiel #2
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))
}
Beispiel #3
0
func dashboard(store configstore.Store, w http.ResponseWriter, req *http.Request) {

	names, err := store.Names()
	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")

		var data = []interface{}{}

		for _, name := range names {
			dates, err := store.Dates(name)
			if err != nil {
				continue
			}

			var date *time.Time
			if len(dates) > 0 {
				date = &dates[0]
			} else {
				date = nil
			}

			data = append(data, struct {
				Hostname   string
				LastBackup *time.Time `json:",omitempty"`
			}{
				Hostname:   name,
				LastBackup: date,
			})

		}

		enc := json.NewEncoder(w)
		err = enc.Encode(data)

	} else {

		var data = struct {
			Entries []struct {
				Hostname   string
				LastBackup *time.Time
				Ago        string
				AgoStatus  string
			}
		}{}

		for _, name := range names {
			dates, err := store.Dates(name)
			if err != nil {
				continue
			}

			var date *time.Time
			if len(dates) > 0 {
				date = &dates[0]
			} else {
				date = nil
			}

			data.Entries = append(data.Entries, struct {
				Hostname   string
				LastBackup *time.Time
				Ago        string
				AgoStatus  string
			}{
				Hostname:   name,
				LastBackup: date,
				Ago:        dashboardAgo(date, 24, 72),
				AgoStatus:  dashboardAgoStatus(date, 24, 72),
			})
		}

		io.WriteString(w, mustache.RenderFileInLayout("templates/dashboard.html.mustache", "templates/layout.html.mustache", data))
	}
}