예제 #1
0
파일: front.go 프로젝트: caixw/typing
// /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"})
}
예제 #2
0
파일: front.go 프로젝트: caixw/typing
func initRoute() error {
	m, err := web.NewModule("front")
	if err != nil {
		return err
	}

	m.Get("/", etagHandler(handlers.CompressFunc(pageRoot))).
		Get(app.TagsURL(), etagHandler(handlers.CompressFunc(pageTags))).
		Get(app.TagURL("{id}", 1), etagHandler(handlers.CompressFunc(pageTag))).
		Get(app.PostsURL(1), etagHandler(handlers.CompressFunc(pagePosts))).
		Get(app.PostURL("{id}"), etagHandler(handlers.CompressFunc(pagePost))). // 获取文章详细内容
		Post(app.PostURL("{id}"), etagHandler(handlers.CompressFunc(pagePost))) // 提交评论

	// TODO 静态文件压缩
	m.Get(cfg.UploadURLPrefix+"/", http.StripPrefix(cfg.UploadURLPrefix, http.FileServer(http.Dir(cfg.UploadDir)))).
		Get(cfg.ThemeURLPrefix+"/", http.StripPrefix(cfg.ThemeURLPrefix, http.FileServer(http.Dir(cfg.ThemeDir))))

	// API
	m.Prefix(cfg.FrontAPIPrefix).
		PostFunc("/posts/{id:\\d+}/comments", frontPostPostComment).
		GetFunc("/posts/{id:\\d+}/comments", frontGetPostComments)

	return nil
}