Skip to content

Instantly share code, notes, and snippets.

@IndianGuru
Created September 7, 2015 09:43
Show Gist options
  • Select an option

  • Save IndianGuru/8d7a8c4a3933bf22aca3 to your computer and use it in GitHub Desktop.

Select an option

Save IndianGuru/8d7a8c4a3933bf22aca3 to your computer and use it in GitHub Desktop.
The fetch function
func fetch(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
// If we omitted the .Ancestor from this query there would be
// a slight chance that Quote that had just been written would not
// show up in a query.
q := datastore.NewQuery("Quote").Ancestor(quoteKey(c)).Limit(10)
quotes := make([]Quote, 0, 10)
_, err := q.GetAll(c, &quotes)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err1 := fetchTemplate.Execute(w, quotes)
if err1 != nil {
http.Error(w, err1.Error(), http.StatusInternalServerError)
}
}
var fetchTemplate = template.Must(template.New("fetch").Parse(`
<html>
<head>
<title>All the Quotes</title>
</head>
<body>
<h2>All the Quotes</h2>
{{range .}}
{{with .Author}}
<p><b>{{.}}</b> wrote:</p>
{{else}}
<p>An anonymous person wrote:</p>
{{end}}
<pre>{{.Message}}</pre>
{{end}}
</body>
</html>
`))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment