{{.Title}}
- +{{.Title}}
+
{{.HTML}}
diff --git a/web.go b/web.go
index 397f114..67d74bd 100644
--- a/web.go
+++ b/web.go
@@ -8,6 +8,7 @@ import (
func StartServer(blog *Blog) {
+ /* Handle home page (/): list of blog entries */
http.HandleFunc("GET /{$}",
func(w http.ResponseWriter, r *http.Request) {
tpl, err := template.New("index").Parse(indexTemplate)
@@ -22,29 +23,22 @@ func StartServer(blog *Blog) {
}
})
+ /* Handle one post display(/post/{post ID}) */
http.HandleFunc("GET /post/{id}",
func(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
- log.Println("showing post:", id)
- blog.mutex.Lock()
- defer blog.mutex.Unlock()
- for _, p := range blog.Posts {
- if p.Id == id {
- tpl, err := template.New("entry").Parse(entryTemplate)
- if err != nil {
- log.Fatal(err)
- }
- post := p
- post.BlogTitle = blog.Title
- post.Lang = blog.Lang
- err = tpl.ExecuteTemplate(w, "entry", post)
- if err != nil {
- log.Fatal(err)
- }
- return
- }
+ post, found := blog.GetPost(id)
+ if !found {
+ w.WriteHeader(404)
+ }
+ tpl, err := template.New("entry").Parse(entryTemplate)
+ if err != nil {
+ log.Fatal(err)
+ }
+ err = tpl.ExecuteTemplate(w, "entry", post)
+ if err != nil {
+ log.Fatal(err)
}
- w.WriteHeader(404)
})