// 首页或是列表页 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"}) }
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 }