Exemple #1
0
// 首页或是列表页
func pagePosts(w http.ResponseWriter, r *http.Request) {
	info, err := getInfo()
	if err != nil {
		logs.Error("pagePosts:", err)
		pageHttpStatusCode(w, r, http.StatusInternalServerError)
		return
	}

	page := conv.MustInt(r.FormValue("page"), 1)
	if page == 1 {
		info.Canonical = app.URL(app.HomeURL())
	} else if page > 1 { // 为1的时候,不需要prev
		info.Canonical = app.URL(app.PostsURL(page))
		info.PrevPage = &Anchor{Title: "上一页", Link: app.PostsURL(page - 1)}
	}

	if page*opt.SidebarSize < info.PostSize {
		info.NextPage = &Anchor{Title: "下一页", Link: app.PostsURL(page + 1)}
	}

	posts, err := getPosts(page - 1)
	if err != nil {
		logs.Error("pagePosts:", err)
		pageHttpStatusCode(w, r, http.StatusInternalServerError)
		return
	}

	data := map[string]interface{}{
		"info":  info,
		"posts": posts,
	}
	render(w, r, "posts", data, map[string]string{"Content-Type": "text/html"})
}
Exemple #2
0
// /tags
func pageTags(w http.ResponseWriter, r *http.Request) {
	info, err := getInfo()
	if err != nil {
		logs.Error("pageTags:", err)
		pageHttpStatusCode(w, r, http.StatusInternalServerError)
		return
	}
	info.Canonical = app.URL(app.TagsURL())
	info.Title = "标签"

	sql := `SELECT {id} AS {ID}, {name} AS {Name}, {title} AS {Title} FROM #tags`
	rows, err := db.Query(true, sql)
	if err != nil {
		logs.Error("pageTags:", err)
		pageHttpStatusCode(w, r, http.StatusInternalServerError)
		return
	}
	defer rows.Close()

	tags := make([]*Tag, 0, 100)
	if _, err = fetch.Obj(&tags, rows); err != nil {
		logs.Error("pageTags:", err)
		pageHttpStatusCode(w, r, http.StatusInternalServerError)
		return
	}

	data := map[string]interface{}{"info": info, "tags": tags}
	render(w, r, "tags", data, map[string]string{"Content-Type": "text/html"})
}
Exemple #3
0
// /tags/1.html
func pageTag(w http.ResponseWriter, r *http.Request) {
	tagName, ok := util.ParamString(w, r, "id")
	if !ok {
		return
	}
	tagName = strings.TrimSuffix(tagName, opt.Suffix)

	sql := `SELECT {id} AS {ID}, {name} AS {Name}, {title} AS {Title}, {description} AS {Description}
	FROM #tags
	WHERE {name}=?`
	rows, err := db.Query(true, sql, tagName)
	if err != nil {
		logs.Error("pageTag:", err)
		pageHttpStatusCode(w, r, http.StatusInternalServerError)
		return
	}
	defer rows.Close()

	tag := &Tag{}
	if _, err = fetch.Obj(tag, rows); err != nil {
		logs.Error("pageTag:", err)
		pageHttpStatusCode(w, r, http.StatusInternalServerError)
		return
	}

	info, err := getInfo()
	if err != nil {
		logs.Error("pageTag:", err)
		pageHttpStatusCode(w, r, http.StatusInternalServerError)
		return
	}

	info.Canonical = app.URL(tag.Permalink())
	info.Title = tag.Title

	page := conv.MustInt(r.FormValue("page"), 1)
	if page < 1 { // 不能小于1
		page = 1
	} else if page > 1 { // 为1的时候,不需要prev
		info.PrevPage = &Anchor{Title: "上一页", Link: app.TagURL(tagName, page-1)}
	}
	if page*opt.SidebarSize < tag.Count() {
		info.NextPage = &Anchor{Title: "下一页", Link: app.TagURL(tagName, page+1)}
	}
	posts, err := getTagPosts(page-1, tag.ID)
	if err != nil {
		logs.Error("pageTag:", err)
		pageHttpStatusCode(w, r, http.StatusInternalServerError)
		return
	}

	data := map[string]interface{}{
		"info":  info,
		"tag":   tag,
		"posts": posts,
	}
	render(w, r, "tag", data, map[string]string{"Content-Type": "text/html"})
}
Exemple #4
0
// /posts/1.html
// /posts/about.html
func pagePost(w http.ResponseWriter, r *http.Request) {
	idStr, ok := util.ParamString(w, r, "id")
	if !ok {
		return
	}
	idStr = strings.TrimSuffix(idStr, opt.Suffix)

	var rows *sql.Rows
	var err error
	postID, err := strconv.ParseInt(idStr, 10, 64)
	if err != nil {
		sql := `SELECT * FROM #posts WHERE {name}=?`
		rows, err = db.Query(true, sql, idStr)
	} else {
		sql := `SELECT * FROM #posts WHERE {id}=?`
		rows, err = db.Query(true, sql, postID)
	}
	if err != nil {
		logs.Error("pagePost:", err)
		pageHttpStatusCode(w, r, http.StatusInternalServerError)
		return
	}
	defer rows.Close()

	mp := &models.Post{}
	if _, err = fetch.Obj(mp, rows); err != nil {
		logs.Error("pagePost:", err)
		pageHttpStatusCode(w, r, http.StatusInternalServerError)
		return
	}

	if len(mp.Title) == 0 || mp.State != models.PostStatePublished {
		pageHttpStatusCode(w, r, http.StatusNotFound)
		return
	}

	if r.Method == "POST" {
		if err := insertComment(mp.ID, r); err != nil {
			logs.Error("pagePost:", err)
		} else {
			stats.WaitingCommentsSize++
			stats.CommentsSize++
		}
	}

	post := &Post{
		ID:           mp.ID,
		Name:         mp.Name,
		Title:        mp.Title,
		Summary:      mp.Summary,
		Content:      mp.Content,
		Author:       opt.ScreenName,
		Created:      mp.Created,
		Modified:     mp.Modified,
		AllowComment: mp.AllowComment,
	}

	info, err := getInfo()
	if err != nil {
		logs.Error("pagePost:", err)
		pageHttpStatusCode(w, r, http.StatusInternalServerError)
		return
	}
	info.Canonical = app.URL(post.Permalink())
	info.Title = post.Title
	info.Description = post.Summary
	info.Keywords = post.Keywords()

	data := map[string]interface{}{
		"info": info,
		"post": post,
	}
	render(w, r, "post", data, map[string]string{"Content-Type": "text/html"})
}