Skip to content

Instantly share code, notes, and snippets.

@IndianGuru
Last active September 21, 2015 04:39
Show Gist options
  • Select an option

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

Select an option

Save IndianGuru/f2151129d7962b62e3d9 to your computer and use it in GitHub Desktop.
Completely modified quote.go
package quote
import (
"fmt"
"html/template"
"net/http"
// Package context defines the Context type
// Do not store Contexts inside a struct type; instead, pass a
// Context explicitly to each function that needs it. The Context
// should be the first parameter, typically named ctx
// https://godoc.org/golang.org/x/net/context
"golang.org/x/net/context"
"google.golang.org/appengine"
// Package datastore provides a client for App Engine's datastore service
// https://godoc.org/google.golang.org/appengine/datastore
"google.golang.org/appengine/datastore"
)
type Quote struct {
Author string
Message string
}
func init() {
http.HandleFunc("/", root)
http.HandleFunc("/quote", quote)
http.HandleFunc("/fetch", fetch)
}
// quoteKey returns the key used for all quote entries.
func quoteKey(c context.Context) *datastore.Key {
return datastore.NewKey(c, "Quote", "default_quote", 0, nil)
}
func root(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, rootForm)
}
const rootForm = `
<html>
<body>
<h1>Enter Author's Quote</h1>
<form action="/quote" method="post" accept-charset="utf-8">
<input type="text" name="aut" value="Author's Name: " id="aut">
<input type="text" name="quo" value="Type a string..." id="quo">
<input type="submit" value="Submit Quote">
</form>
</body>
</html>
`
var quoteTemplate = template.Must(template.New("quote").Parse(quoteTemplateHTML))
func quote(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
q1 := Quote{
Author: r.FormValue("aut"),
Message: r.FormValue("quo"),
}
// We set the same parent key on every Quote entity to ensure each Quote
// is in the same entity group. Queries across the single entity group
// will be consistent.
key, err := datastore.Put(c, datastore.NewIncompleteKey(c, "Quote", quoteKey(c)), &q1)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var q2 Quote
err1 := datastore.Get(c, key, &q2)
if err1 != nil {
http.Error(w, err1.Error(), http.StatusInternalServerError)
return
}
err2 := quoteTemplate.Execute(w, q2.Message)
if err2 != nil {
http.Error(w, err2.Error(), http.StatusInternalServerError)
}
}
const quoteTemplateHTML = `
<html>
<body>
<p>You wrote the quote:</p>
<pre>{{html .}}</pre>
</body>
</html>
`
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