package main import ( "html/template" "log" "net/http" ) func StartServer(blog *Blog) { http.HandleFunc("GET /{$}", func(w http.ResponseWriter, r *http.Request) { tpl, err := template.New("index").Parse(indexTemplate) if err != nil { log.Fatal(err) } err = tpl.ExecuteTemplate(w, "index", blog) if err != nil { log.Fatal(err) } }) http.HandleFunc("GET /post/{id}", func(w http.ResponseWriter, r *http.Request) { id := r.PathValue("id") log.Println("showing post:", id) for _, p := range blog.Posts { log.Println("Examining:", p.Id) if p.Id == id { tpl, err := template.New("entry").Parse(entryTemplate) if err != nil { log.Fatal(err) } var post Blog post.Title = blog.Title post.Lang = blog.Lang post.Posts = make([]Post, 1) post.Posts[0] = p err = tpl.ExecuteTemplate(w, "entry", post) if err != nil { log.Fatal(err) } return } } w.WriteHeader(404) }) 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) }