forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PetStoreBook.go
204 lines (160 loc) · 5.68 KB
/
PetStoreBook.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
Copyright 2014 The Kubernetes Authors All rights reserved.
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 (
"encoding/json"
"fmt"
"net/http"
"os"
"strings"
"github.com/codegangsta/negroni"
"github.com/gorilla/mux"
"github.com/xyproto/simpleredis"
)
//return the path to static assets (i.e. index.html)
func pathToStaticContents() string {
var static_content = os.Getenv("STATIC_FILES")
// Take a wild guess. This will work in dev environment.
if static_content == "" {
println("*********** WARNING: DIDNT FIND ENV VAR 'STATIC_FILES', guessing your running in dev.")
static_content = "../../static/"
} else {
println("=========== Read ENV 'STATIC_FILES', path to assets : " + static_content)
}
//Die if no the static files are missing.
_, err := os.Stat(static_content)
if err != nil {
println("*********** os.Stat failed on " + static_content + " This means no static files are available. Dying...")
os.Exit(2)
}
return static_content
}
func main() {
var connection = os.Getenv("REDISMASTER_SERVICE_HOST") + ":" + os.Getenv("REDISMASTER_SERVICE_PORT")
if connection == ":" {
print("WARNING ::: If in kube, this is a failure: Missing env variable REDISMASTER_SERVICE_HOST")
print("WARNING ::: Attempting to connect redis localhost.")
connection = "127.0.0.1:6379"
} else {
print("Found redis master host " + os.Getenv("REDISMASTER_SERVICE_PORT"))
connection = os.Getenv("REDISMASTER_SERVICE_HOST") + ":" + os.Getenv("REDISMASTER_SERVICE_PORT")
}
println("Now connecting to : " + connection)
/**
* Create a connection pool. ?The pool pointer will otherwise
* not be of any use.?https://gist.github.com/jayunit100/1d00e6d343056401ef00
*/
pool = simpleredis.NewConnectionPoolHost(connection)
println("Connection pool established : " + connection)
defer pool.Close()
r := mux.NewRouter()
println("Router created ")
/**
* Define a REST path.
* - The parameters (key) can be accessed via mux.Vars.
* - The Methods (GET) will be bound to a handler function.
*/
r.Path("/info").Methods("GET").HandlerFunc(InfoHandler)
r.Path("/lrange/{key}").Methods("GET").HandlerFunc(ListRangeHandler)
r.Path("/rpush/{key}/{value}").Methods("GET").HandlerFunc(ListPushHandler)
r.Path("/llen").Methods("GET").HandlerFunc(LLENHandler)
//for dev environment, the site is one level up...
r.PathPrefix("/").Handler(http.FileServer(http.Dir(pathToStaticContents())))
r.Path("/env").Methods("GET").HandlerFunc(EnvHandler)
list := simpleredis.NewList(pool, "k8petstore")
HandleError(nil, list.Add("jayunit100"))
HandleError(nil, list.Add("tstclaire"))
HandleError(nil, list.Add("rsquared"))
// Verify that this is 3 on startup.
infoL := HandleError(pool.Get(0).Do("LLEN", "k8petstore")).(int64)
fmt.Printf("\n=========== Starting DB has %d elements \n", infoL)
if infoL < 3 {
print("Not enough entries in DB. something is wrong w/ redis querying")
print(infoL)
panic("Failed ... ")
}
println("=========== Now launching negroni...this might take a second...")
n := negroni.Classic()
n.UseHandler(r)
n.Run(":3000")
println("Done ! Web app is now running.")
}
/**
* the Pool will be populated on startup,
* it will be an instance of a connection pool.
* Hence, we reference its address rather than copying.
*/
var pool *simpleredis.ConnectionPool
/**
* REST
* input: key
*
* Writes all members to JSON.
*/
func ListRangeHandler(rw http.ResponseWriter, req *http.Request) {
println("ListRangeHandler")
key := mux.Vars(req)["key"]
list := simpleredis.NewList(pool, key)
//members := HandleError(list.GetAll()).([]string)
members := HandleError(list.GetLastN(4)).([]string)
print(members)
membersJSON := HandleError(json.MarshalIndent(members, "", " ")).([]byte)
print("RETURN MEMBERS = " + string(membersJSON))
rw.Write(membersJSON)
}
func LLENHandler(rw http.ResponseWriter, req *http.Request) {
println("=========== LLEN HANDLER")
infoL := HandleError(pool.Get(0).Do("LLEN", "k8petstore")).(int64)
fmt.Printf("=========== LLEN is %d ", infoL)
lengthJSON := HandleError(json.MarshalIndent(infoL, "", " ")).([]byte)
fmt.Printf("================ LLEN json is %s", infoL)
print("RETURN LEN = " + string(lengthJSON))
rw.Write(lengthJSON)
}
func ListPushHandler(rw http.ResponseWriter, req *http.Request) {
println("ListPushHandler")
/**
* Expect a key and value as input.
*
*/
key := mux.Vars(req)["key"]
value := mux.Vars(req)["value"]
println("New list " + key + " " + value)
list := simpleredis.NewList(pool, key)
HandleError(nil, list.Add(value))
ListRangeHandler(rw, req)
}
func InfoHandler(rw http.ResponseWriter, req *http.Request) {
println("InfoHandler")
info := HandleError(pool.Get(0).Do("INFO")).([]byte)
rw.Write(info)
}
func EnvHandler(rw http.ResponseWriter, req *http.Request) {
println("EnvHandler")
environment := make(map[string]string)
for _, item := range os.Environ() {
splits := strings.Split(item, "=")
key := splits[0]
val := strings.Join(splits[1:], "=")
environment[key] = val
}
envJSON := HandleError(json.MarshalIndent(environment, "", " ")).([]byte)
rw.Write(envJSON)
}
func HandleError(result interface{}, err error) (r interface{}) {
if err != nil {
print("ERROR : " + err.Error())
//panic(err)
}
return result
}