Created
September 7, 2015 09:43
-
-
Save IndianGuru/8d7a8c4a3933bf22aca3 to your computer and use it in GitHub Desktop.
The fetch function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, "es) | |
| 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