Skip to content

Instantly share code, notes, and snippets.

@will3942
Created December 4, 2013 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save will3942/7795951 to your computer and use it in GitHub Desktop.
Save will3942/7795951 to your computer and use it in GitHub Desktop.
Go: new request handler
func handlerequest(w http.ResponseWriter, r *http.Request) {
if (r.URL.Path[1:] == "") {
posts := getPosts()
t := template.New("index.html")
t, _ = t.ParseFiles("index.html")
t.Execute(w, posts)
} else {
f = "posts/" + r.URL.Path[1:] + ".md"
fileread, _ := ioutil.ReadFile(f)
lines := strings.Split(string(fileread), "\n")
title := string(lines[0])
date := string(lines[1])
summary := string(lines[2])
body := strings.Join(lines[3:len(lines)], "\n")
body = string(blackfriday.MarkdownCommon([]byte(body)))
post = Post{title, date, summary, body, r.URL.Path[1:]}
t := template.New("post.html")
t, _ = t.ParseFiles("post.html")
t.Execute(w, post)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment