This repository has been archived by the owner on Jun 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.go
137 lines (109 loc) · 3.38 KB
/
main.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
Copyright 2014 The Kubernetes Authors.
Copyright 2017 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"context"
"encoding/json"
"net/http"
"os"
"time"
"cloud.google.com/go/datastore"
"cloud.google.com/go/errors"
"github.com/codegangsta/negroni"
"github.com/gorilla/mux"
)
type Entry struct {
Value string `datastore:"value"`
Created time.Time `datastore:"created"`
}
var (
datastoreClient *datastore.Client
projectID string
)
func ListRangeHandler(rw http.ResponseWriter, req *http.Request) {
key := mux.Vars(req)["key"]
var entries []*Entry
query := datastore.NewQuery(key).Order("created")
_, err := datastoreClient.GetAll(context.Background(), query, &entries)
if err != nil {
panic(err)
}
membersJSON := PanicOnError(json.MarshalIndent(entries, "", " ")).([]byte)
PanicOnError(rw.Write(membersJSON))
}
func ListPushHandler(rw http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
// Bug:
key := vars["k"]
value := vars["v"]
// Functional code:
// key := vars["key"]
// value := vars["value"]
if key == "" || value == "" {
panic("error: can not store empty values")
}
newEntry := &Entry{
Value: value,
Created: time.Now(),
}
datastoreKey := datastore.IncompleteKey(key, nil)
_, err := datastoreClient.Put(context.Background(), datastoreKey, newEntry)
if err != nil {
panic(err)
}
ListRangeHandler(rw, req)
}
func InfoHandler(_ http.ResponseWriter, _ *http.Request) {
}
func PanicOnError(result interface{}, err error) (r interface{}) {
if err != nil {
panic(err)
}
return result
}
var errorsClient *errors.Client
type ErrorsMiddleware struct{}
func (l *ErrorsMiddleware) ServeHTTP(w http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
defer errorsClient.Catch(context.Background(), errors.WithRequest(req))
if next != nil {
next(w, req)
}
}
func main() {
projectID = os.Getenv("GOOGLE_PROJECT")
if projectID == "" {
panic("Please set the env variable GOOGLE_PROJECT in manifest.yml to a project with Google Cloud Datastore and Stackdriver Error Reporting enabled")
}
ctx := context.Background()
errorsClient = PanicOnError(errors.NewClient(ctx, projectID, "cf-stackdriver-example", "0.0.1", true)).(*errors.Client)
var err error
datastoreClient, err = datastore.NewClient(context.Background(), projectID)
if err != nil {
panic("error connecting to the Google Cloud Datastore. Does this project have an App Engine App? see: https://cloud.google.com/datastore/docs/activate")
}
r := mux.NewRouter()
r.Path("/lrange/{key}").Methods("GET").HandlerFunc(ListRangeHandler)
r.Path("/rpush/{key}/{value}").Methods("GET").HandlerFunc(ListPushHandler)
r.Path("/info").Methods("GET").HandlerFunc(InfoHandler)
n := negroni.Classic()
n.Use(&ErrorsMiddleware{})
n.UseHandler(r)
port := os.Getenv("PORT")
if port == "" {
port = "3000"
}
if err := http.ListenAndServe(":"+port, n); err != nil {
panic(err)
}
}