Пример #1
0
func ensureCookie(r *http.Request, w http.ResponseWriter) string {
	cookie, _ := r.Cookie(CookieName)
	if cookie != nil {
		return cookie.Value
	}
	sessionID := utils.GenerateID()

	cookie = &http.Cookie{
		Name:    CookieName,
		Value:   sessionID,
		Expires: time.Now().Add(5 * time.Minute),
	}
	http.SetCookie(w, cookie)

	return sessionID
}
Пример #2
0
// SavePostHandler ...
func SavePostHandler(s *session.Session, rnd render.Render, r *http.Request, db *mgo.Database) {
	if !s.IsAuthorized {
		rnd.Redirect("/")
	}
	id := r.FormValue("id")
	title := r.FormValue("title")
	contentMarkdown := r.FormValue("content")
	contentHTML := utils.ConvertMarkdownToHTML(contentMarkdown)

	postDocument := documents.PostDocument{id, title, contentHTML, contentMarkdown}

	postsCollection := db.C("posts")
	if id != "" {
		postsCollection.UpdateId(id, postDocument)
	} else {
		id = utils.GenerateID()
		postDocument.ID = id
		postsCollection.Insert(postDocument)
	}

	rnd.Redirect("/")
}