func PostApiHosts(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") db := context.Get(r, "db").(*sqlx.DB) accessTokenRow := context.Get(r, "accessTokenRow").(*dal.AccessTokenRow) dataJson, err := ioutil.ReadAll(r.Body) if err != nil { libhttp.HandleErrorJson(w, err) return } hostRow, err := dal.NewHost(db).CreateOrUpdate(nil, accessTokenRow, dataJson) if err != nil { libhttp.HandleErrorJson(w, err) return } hostRowJson, err := json.Marshal(hostRow) if err != nil { libhttp.HandleErrorJson(w, err) return } w.Write(hostRowJson) }
func GetApiHosts(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") db := context.Get(r, "db").(*sqlx.DB) accessTokenRow := context.Get(r, "accessTokenRow").(*dal.AccessTokenRow) query := r.URL.Query().Get("q") hosts, err := dal.NewHost(db).AllByClusterIDAndQuery(nil, accessTokenRow.ID, query) if err != nil { libhttp.HandleErrorJson(w, err) return } hostRowsJson, err := json.Marshal(hosts) if err != nil { libhttp.HandleErrorJson(w, err) return } w.Write(hostRowsJson) }
func GetHosts(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html") cookieStore := context.Get(r, "cookieStore").(*sessions.CookieStore) session, _ := cookieStore.Get(r, "resourcedmaster-session") currentUserRow, ok := session.Values["user"].(*dal.UserRow) if !ok { http.Redirect(w, r, "/logout", 301) return } currentClusterInterface := session.Values["currentCluster"] if currentClusterInterface == nil { http.Redirect(w, r, "/", 301) return } currentCluster := currentClusterInterface.(*dal.ClusterRow) db := context.Get(r, "db").(*sqlx.DB) query := r.URL.Query().Get("q") hosts, err := dal.NewHost(db).AllByClusterIDAndQuery(nil, currentCluster.ID, query) if err != nil { libhttp.HandleErrorJson(w, err) return } savedQueries, err := dal.NewSavedQuery(db).AllByClusterID(nil, currentCluster.ID) if err != nil { libhttp.HandleErrorJson(w, err) return } accessTokenRow, err := dal.NewAccessToken(db).GetByUserID(nil, currentUserRow.ID) if err != nil { libhttp.HandleErrorJson(w, err) return } data := struct { Addr string CurrentUser *dal.UserRow AccessToken *dal.AccessTokenRow Clusters []*dal.ClusterRow CurrentClusterJson string Hosts []*dal.HostRow SavedQueries []*dal.SavedQueryRow }{ context.Get(r, "addr").(string), currentUserRow, accessTokenRow, context.Get(r, "clusters").([]*dal.ClusterRow), string(context.Get(r, "currentClusterJson").([]byte)), hosts, savedQueries, } tmpl, err := template.ParseFiles("templates/dashboard.html.tmpl", "templates/hosts/list.html.tmpl") if err != nil { libhttp.HandleErrorJson(w, err) return } tmpl.Execute(w, data) }