-
Notifications
You must be signed in to change notification settings - Fork 0
/
populate.go
53 lines (40 loc) · 1.12 KB
/
populate.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
47
48
49
50
51
52
53
package quote
import (
//"encoding/json"
"net/http"
"encoding/json"
"google.golang.org/appengine"
"google.golang.org/appengine/datastore"
)
type Response struct {
Message string `json:"message"`
}
func init() {
http.HandleFunc("/", handler)
}
func handler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
ctx := appengine.NewContext(r)
var quotes []*Quote
inspirationalQuotes := GetInspirationalQuotes()
movieQuotes := GetMovieQuotes()
programmingQuotes := GetProgrammingQuotes()
quotes = append(quotes, inspirationalQuotes...)
quotes = append(quotes, movieQuotes...)
quotes = append(quotes, programmingQuotes...)
keys := make([]*datastore.Key, len(quotes))
for i := range quotes {
keys[i] = datastore.NewIncompleteKey(ctx, "Quote", nil)
}
if _, err := datastore.PutMulti(ctx, keys, quotes); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
successRes := Response{"Success"}
response, err := json.Marshal(successRes)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(response)
}