예제 #1
0
파일: handlers.go 프로젝트: husio/jinx
func HandlePageDetail(ctx context.Context, w http.ResponseWriter, r *http.Request) {
	var content struct {
		Page *Page
		User *auth.User
	}
	if u, ok := auth.CurrentUser(ctx); ok {
		content.User = u.User
	}
	page, err := Store(ctx).GetBySlug(web.Args(ctx).ByName("slug"))
	if err == ErrNotFound {
		http.Redirect(w, r, r.URL.Path+"/edit", http.StatusFound)
		return
	}
	if err != nil {
		log.Printf("cannot get page %q: %s", web.Args(ctx).ByName("slug"), err)
		web.StdHTMLResp(w, http.StatusServiceUnavailable)
		return
	}

	if protectPrivatePage(ctx, w, r, page) {
		return
	}

	content.Page = page
	if web.CheckLastModified(ctx, w, r, page.Updated) {
		return
	}
	render(w, "page-details", content)
}
예제 #2
0
파일: handlers.go 프로젝트: husio/jinx
func HandlePageHistory(ctx context.Context, w http.ResponseWriter, r *http.Request) {
	page, err := Store(ctx).GetBySlug(web.Args(ctx).ByName("slug"))
	if err != nil {
		if err == ErrNotFound {
			web.HTMLErr(w, "Page does not exist", http.StatusNotFound)
		} else {
			log.Printf("cannot get page %q: %s", web.Args(ctx).ByName("slug"), err)
			web.StdHTMLResp(w, http.StatusServiceUnavailable)
		}
		return
	}

	if protectPrivatePage(ctx, w, r, page) {
		return
	}

	content := struct {
		Page *Page
		User *auth.User
	}{
		Page: page,
	}
	if u, ok := auth.CurrentUser(ctx); ok {
		content.User = u.User
	}
	render(w, "page-history", content)
}
예제 #3
0
파일: handlers.go 프로젝트: husio/jinx
func HandlePageDelete(ctx context.Context, w http.ResponseWriter, r *http.Request) {
	slug := web.Args(ctx).ByName("slug")
	page, err := Store(ctx).GetBySlug(slug)
	if err != nil {
		if err == ErrNotFound {
			web.HTMLErr(w, "Page does not exist", http.StatusNotFound)
		} else {
			log.Printf("cannot get page %q: %s", slug, err)
			web.StdHTMLResp(w, http.StatusServiceUnavailable)
		}
		return
	}

	if protectPrivatePage(ctx, w, r, page) {
		return
	}

	if r.Method == "GET" {
		render(w, "page-delete-confirm", page)
		return
	}

	page.Deleted = true
	if _, err := Store(ctx).Put(*page); err != nil {
		log.Printf("cannot delete page %d: %s", page.PageID, err)
		web.StdHTMLResp(w, http.StatusServiceUnavailable)
	} else {
		http.Redirect(w, r, "/", http.StatusFound)
	}
}
예제 #4
0
파일: handlers.go 프로젝트: husio/jinx
func HandlePageEdit(ctx context.Context, w http.ResponseWriter, r *http.Request) {
	page, err := Store(ctx).GetBySlug(web.Args(ctx).ByName("slug"))

	if err == ErrNotFound {
		page = &Page{
			Slug: web.Args(ctx).ByName("slug"),
		}
		render(w, "page-edit", page)
		return
	}

	if err != nil {
		log.Printf("cannot get page %q: %s", web.Args(ctx).ByName("slug"), err)
		web.StdHTMLResp(w, http.StatusServiceUnavailable)
		return
	}

	if protectPrivatePage(ctx, w, r, page) {
		return
	}

	render(w, "page-edit", page)
}
예제 #5
0
파일: handlers.go 프로젝트: husio/jinx
func HandlePageSave(ctx context.Context, w http.ResponseWriter, r *http.Request) {
	page, err := Store(ctx).GetBySlug(web.Args(ctx).ByName("slug"))
	if err != nil && err != ErrNotFound {
		log.Printf("cannot get page %q: %s", web.Args(ctx).ByName("slug"), err)
		web.StdHTMLResp(w, http.StatusServiceUnavailable)
		return
	}

	user, _ := auth.CurrentUser(ctx)
	if err == ErrNotFound {
		page = &Page{
			IsPublic: true,
			OwnerID:  user.User.UserID,
			Slug:     web.Args(ctx).ByName("slug"),
		}
	}

	if protectPrivatePage(ctx, w, r, page) {
		return
	}

	content := r.FormValue("content")
	if content == "" {
		web.HTMLErr(w, "content is required", http.StatusBadRequest)
		return
	}

	page.IsPublic = r.FormValue("public") == "on" // checkbox
	page.Content = content
	if p, err := Store(ctx).Put(*page); err != nil {
		log.Printf("cannot update page: %s", err)
		web.StdHTMLResp(w, http.StatusServiceUnavailable)
	} else {
		http.Redirect(w, r, fmt.Sprintf("/p/%s", p.Slug), http.StatusFound)
	}
}