// 首页或是列表页 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 pageRoot(w http.ResponseWriter, r *http.Request) { // 首页的匹配模式为:/,可以匹配任意路径。所以此处作个判断,只有完全匹配的,才是显示首页 if r.URL.Path == app.HomeURL() { pagePosts(w, r) return } path := cfg.RootDir + r.URL.Path if utils.FileExists(path) { http.ServeFile(w, r, path) return } pageHttpStatusCode(w, r, http.StatusNotFound) }