forked from GoesToEleven/GolangTraining
-
Notifications
You must be signed in to change notification settings - Fork 1
/
words.go
executable file
·46 lines (38 loc) · 905 Bytes
/
words.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main
import (
"fmt"
"net/http"
"google.golang.org/appengine"
"google.golang.org/appengine/datastore"
)
func init() {
http.HandleFunc("/", handleIndex)
}
type Word struct {
Term string
Definition string
}
func handleIndex(res http.ResponseWriter, req *http.Request) {
if req.Method == "POST" {
term := req.FormValue("term")
definition := req.FormValue("definition")
ctx := appengine.NewContext(req)
key := datastore.NewIncompleteKey(ctx, "Word", nil)
entity := Word{
Term: term,
Definition: definition,
}
_, err := datastore.Put(ctx, key, &entity)
if err != nil {
http.Error(res, err.Error(), 500)
return
}
}
res.Header().Set("Content-Type", "text/html")
fmt.Fprintln(res, `
<form method="POST" action="/words/">
<input type="text" name="term">
<textarea name="definition"></textarea>
<input type="submit">
</form>`)
}