package main import ( "html/template" "log" "net/http" ) 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) if err != nil { log.Fatal(err) } blog.mutex.Lock() defer blog.mutex.Unlock() err = tpl.ExecuteTemplate(w, "index", blog) if err != nil { log.Fatal(err) } }) /* Handle one post display(/post/{post ID}) */ http.HandleFunc("GET /post/{id}", func(w http.ResponseWriter, r *http.Request) { id := r.PathValue("id") 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) } }) http.HandleFunc("GET /favicon.ico", func(w http.ResponseWriter, r *http.Request) { log.Println("favicon.ico") w.WriteHeader(200) }) http.ListenAndServe("0.0.0.0:8080", nil) }