示例#1
0
func createHouse(app *app, w http.ResponseWriter, r *http.Request) error {
	html := r.FormValue("html")
	if strings.TrimSpace(html) == "" {
		return impart.HTTPError{http.StatusBadRequest, "Supply something to publish."}
	}
	public := r.FormValue("public") == "true"

	houseID := store.GenerateFriendlyRandomString(8)

	_, err := app.db.Exec("INSERT INTO houses (id, html) VALUES (?, ?)", houseID, html)
	if err != nil {
		return err
	}

	if err = app.session.writeToken(w, houseID); err != nil {
		return err
	}

	resUser := newSessionInfo(houseID)

	if public {
		go addPublicAccess(app, houseID, html)
	}

	return impart.WriteSuccess(w, resUser, http.StatusCreated)
}
示例#2
0
func getPublicHousesData(app *app, w http.ResponseWriter, r *http.Request) error {
	houses, err := getPublicHouses(app, true)
	if err != nil {
		return err
	}

	for i := range *houses {
		(*houses)[i].process(app)
	}

	return impart.WriteSuccess(w, houses, http.StatusOK)
}
示例#3
0
func renovateHouse(app *app, w http.ResponseWriter, r *http.Request) error {
	vars := mux.Vars(r)
	houseID := vars["house"]
	html := r.FormValue("html")
	if strings.TrimSpace(html) == "" {
		return impart.HTTPError{http.StatusBadRequest, "Supply something to publish."}
	}
	public := r.FormValue("public") == "true"

	authHouseID, err := app.session.readToken(r)
	if err != nil {
		return err
	}

	if authHouseID != houseID {
		return impart.HTTPError{http.StatusUnauthorized, "Bad token for this ⌂ house ⌂."}
	}

	_, err = app.db.Exec("UPDATE houses SET html = ? WHERE id = ?", html, houseID)
	if err != nil {
		return err
	}

	if err = app.session.writeToken(w, houseID); err != nil {
		return err
	}

	resUser := newSessionInfo(houseID)

	if public {
		go addPublicAccess(app, houseID, html)
	} else {
		go removePublicAccess(app, houseID)
	}

	return impart.WriteSuccess(w, resUser, http.StatusOK)
}