コード例 #1
0
ファイル: handlers.go プロジェクト: nrolans/netbweb
func listHosts(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")

		enc := json.NewEncoder(w)
		err := enc.Encode(struct {
			Hostnames []string
		}{
			Hostnames: names,
		})

		if err != nil {
			log.Printf("Error: %s", err)
			http.Error(w, "error :(", 500)
			return
		}
	} else {
		io.WriteString(w, mustache.RenderFileInLayout("templates/hosts.html.mustache", "templates/layout.html.mustache", names))
	}
}
コード例 #2
0
ファイル: handlers.go プロジェクト: nrolans/netbweb
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))
	}
}