コード例 #1
0
ファイル: webserver.go プロジェクト: rbjorklin/simple-go-blog
func postsHandler(w http.ResponseWriter, r *http.Request, title string) {
	log.Print("Title is: " + title)
	log.Print(len(strings.Split(title, "/")))
	if len(strings.Split(title, "/")) > 2 {
		p, err := loadPage(title+".html", config.RootDesc, config.SiteName)
		if err != nil {
			log.Print(err)
			http.Redirect(w, r, "/", http.StatusFound)
			return
		}
		renderTemplate(w, "root", p)
	} else {
		r := strings.NewReplacer("_", " ")
		var data bytes.Buffer
		for i := len(posts) - 1; i >= 0; i-- {
			s := posts[i]
			row := fmt.Sprintf("<p><a href=\"/%s\">%s</a>\n\t\t\t\t<span class=\"blog-post-meta\">%s</span>\n\t\t\t\t</p>\n",
				strings.Split(s, ".html")[0], r.Replace(strings.Split(filepath.Base(s), ".html")[0]), dateFromPath(s))
			log.Print(s)
			data.Write([]byte(row))
		}
		p := &Page{SiteName: config.SiteName, PageTitle: "blargh", Body: template.HTML(data.String()), PostDate: "N/A", PostTitle: "plupp", Description: config.PostsDesc}
		renderTemplate(w, "posts", p)
	}
}