forked from couchbase/query-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui_server.go
366 lines (295 loc) · 10.4 KB
/
ui_server.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package main
import (
"flag"
"fmt"
"net/http"
"net/http/httputil"
"net/url"
"os"
"strings"
"io/ioutil"
"github.com/couchbase/go-couchbase"
json "github.com/dustin/gojson"
)
const TEST_URL = "http://localhost:8091/"
var DATASTORE = flag.String("datastore", "", "Datastore URL (e.g., http://localhost:8091)")
var USER = flag.String("user", "", "Authorized user login for Couchbase")
var PASS = flag.String("pass", "", "Authorized user password for Couchbase")
var QUERYENGINE = flag.String("queryEngine", "", "Query URL (e.g., http://localhost:8093)")
var QUERY_PREFIX = flag.String("queryPrefix", "/query", "prefix for URL of query service, e.g. '/query' or '/analytics'")
var LOCALPORT = flag.String("localPort", ":8095", "Port on which the UI runs. E.g., :8095")
var WEBCONTENT = flag.String("webcontent", "./", "Location of static folder containing web content. E.g., ./static")
var VERBOSE = flag.Bool("verbose", false, "Produce verbose output about program operation.")
func main() {
flag.Parse()
launchWebService()
}
//
// Given a CB Server URL, ask it what ports KV and Query are running on
//
func getClusterQueryKVsAndMgmt(server, user, pass string) ([]string, []string, []string) {
n1ql := make([]string, 0, 0)
kvs := make([]string, 0, 0)
mgmt := make([]string, 0, 0)
var client couchbase.Client
var err error
if len(user) == 0 {
client, err = couchbase.Connect(server)
} else {
fmt.Printf("Connecting with user: %v pass: %v\n", user, pass)
client, err = couchbase.ConnectWithAuthCreds(server, user, pass)
}
if err != nil {
fmt.Printf("Error with connection to %v: %v\n", server, err)
return n1ql, kvs, mgmt
}
// ...then get the bucket-independent services...
services, err := client.GetPoolServices("default")
if err != nil {
fmt.Printf("Error with pool services: %v\n", err)
return n1ql, kvs, mgmt
}
// ...which then get us to the node-independent services
for _, nodeService := range services.NodesExt {
// the host name is either specified, or the boolean flag "ThisNode"
// indicates that it is the same as the node we connected to
host_name := nodeService.Hostname
if nodeService.ThisNode {
host_name = client.BaseURL.String()
}
// remove any port number from the host name
portIdx := strings.LastIndex(host_name, ":")
if portIdx > 0 {
host_name = host_name[0:portIdx]
}
// remove any http:// or https://
if strings.HasPrefix(host_name, "http://") {
host_name = host_name[7:]
}
if strings.HasPrefix(host_name, "https://") {
host_name = host_name[8:]
}
log(fmt.Sprintf("\nHost name is: %v", host_name))
// now iterate through theh services looking for "kv" and "n1ql"
for k, v := range nodeService.Services {
log(fmt.Sprintf(" Got service %v as %v", k, v))
if strings.EqualFold(k, "n1ql") {
n1ql = append(n1ql, fmt.Sprintf("%s:%d", host_name, v))
}
if strings.EqualFold(k, "mgmt") {
mgmt = append(mgmt, fmt.Sprintf("%s:%d", host_name, v))
}
if strings.EqualFold(k, "kv") {
kvs = append(kvs, fmt.Sprintf("%s:%d", host_name, v))
}
}
}
return n1ql, kvs, mgmt
}
//
// our web service has two jobs: it needs to serve up static web content for the
// query UI, using a normal file server.
//
// it *also* needs to proxy the queries from the UI, intercepting 'describe'
// statements and running them here.
//
var serverHost string
var queryHost string
var serverUrl, serverUser, serverPass string
var kvs []string
func launchWebService() {
fmt.Printf("Launching query web service.\n")
// the datastore allows us to get /pools, find out where every service is
if len(*DATASTORE) > 0 {
fmt.Printf(" Using CB Server at: %s\n", *DATASTORE)
serverUrl = *DATASTORE
serverUser = *USER
serverPass = *PASS
if strings.HasPrefix(strings.ToLower(*DATASTORE), "http://") {
serverHost = serverUrl[7:]
} else {
serverHost = serverUrl
}
//
// get a query and kv service that we'll need
//
var n1ql []string
var mgmt []string
n1ql, kvs, mgmt = getClusterQueryKVsAndMgmt(*DATASTORE, *USER, *PASS)
if len(n1ql) == 0 {
fmt.Printf("Unable to find a N1QL query service on: %s\n", *DATASTORE)
return
} else {
fmt.Printf(" Using N1QL query service on: %s\n", n1ql[0])
//queryURL = fmt.Sprintf("http://%s/query/service", n1ql[0])
queryHost = n1ql[0]
}
if len(mgmt) == 0 {
fmt.Printf("Unable to find the mgmt query service on: %s\n", *DATASTORE)
return
} else {
fmt.Printf(" Using mgmt query service on: %s\n", serverHost)
}
} else if len(*QUERYENGINE) > 0 { // if no DATASTORE, there must be a QUERYENGINE
queryHost = *QUERYENGINE
if strings.HasPrefix(queryHost, "http://") {
queryHost = queryHost[7:]
}
fmt.Printf(" Using query service on: %s\n", queryHost)
} else { // let the user know that they need either DATASTORE or QUERYENGINE
fmt.Printf("Error: you must specify either -datastore=<couchbase URL> or -queryEngine=<query engine URL>\n")
os.Exit(1)
}
//
// make a set of proxies for query engine, management server, and image content
//
staticPath := strings.Join([]string{*WEBCONTENT, "query-ui"}, "")
fmt.Printf(" Using -webcontent=%s, web content path at: %s\n", *WEBCONTENT, staticPath)
_, err := os.Stat(staticPath)
if err != nil {
if os.IsNotExist(err) {
fmt.Printf(" Can't find static path: %v\n", err)
} else {
fmt.Printf(" Error with static path: %v\n", err)
}
os.Exit(1)
}
// create proxies to query and mgmt ports
queryProxy := httputil.NewSingleHostReverseProxy(&url.URL{Scheme: "http", Host: queryHost})
queryProxy.Director = toQuery
imageProxy := httputil.NewSingleHostReverseProxy(&url.URL{Scheme: "http"})
imageProxy.Director = toImage
if len(serverHost) > 0 {
mgmtProxy := httputil.NewSingleHostReverseProxy(&url.URL{Scheme: "http", Host: serverHost})
mgmtProxy.Director = toMgmt
http.Handle("/pools", mgmtProxy)
http.Handle("/pools/", mgmtProxy)
}
// handle queries at the service prefix
http.Handle("../_p/query/", queryProxy)
http.Handle("/_p/query/", queryProxy)
http.Handle("/analytics/service/", queryProxy)
http.Handle("../_p/ui/query/", imageProxy)
http.Handle("/_p/ui/query/", imageProxy)
// Handle static endpoint for serving the web content
http.Handle("/", http.FileServer(http.Dir(staticPath)))
fmt.Printf("Launching UI server, to use, point browser at http://localhost%s\n", *LOCALPORT)
listenAndServe(*LOCALPORT)
}
type Credential struct {
User string `json:"user"`
Pass string `json:"pass"`
}
type QueryParams struct {
Statement string `json:"statement"`
ClientContextId string `json:"client_context_id"`
Creds []Credential `json:"creds"`
}
func toQuery(r *http.Request) {
r.Host = queryHost
r.URL.Host = r.Host
r.URL.Scheme = "http"
// rewrite the URL to the approriate proxied value
//fmt.Printf("Got query path: %s %s\n form: %v\n", r.Host,r.URL.Path, r.Form)
if strings.HasPrefix(strings.ToLower(r.URL.Path), "/_p/query/query") {
r.URL.Path = *QUERY_PREFIX + r.URL.Path[15:]
//fmt.Printf(" new host %v path: %s\n", r.Host,r.URL.Path)
}
if strings.HasPrefix(strings.ToLower(r.URL.Path), "../_p/query/query") {
r.URL.Path = *QUERY_PREFIX + r.URL.Path[17:]
//fmt.Printf(" new host %v path: %s\n", r.Host,r.URL.Path)
}
// if we were given a USER/PASS, we need to insert them into the creds array
// to give queries those privileges
if USER != nil && PASS != nil {
r.SetBasicAuth(*USER, *PASS)
// create a credentials array with USER and PASS
var credentials []Credential = make([]Credential,0,0)
newCred := new(Credential)
newCred.User = *USER
newCred.Pass = *PASS
credentials = append(credentials,*newCred)
queryParams := new(QueryParams)
err := json.NewDecoder(r.Body).Decode(&queryParams)
if err != nil {
log(fmt.Sprintf("Error decoding body: %v\n",err))
return
} else {
log(fmt.Sprintf("Got statement: %s\n",queryParams.Statement))
log(fmt.Sprintf("Got client context: %s\n",queryParams.ClientContextId))
log(fmt.Sprintf("Got creds: %v\n",queryParams.Creds))
}
queryParams.Creds = append(queryParams.Creds,credentials...)
newParamBytes,cerr := json.Marshal(queryParams)
log(fmt.Sprintf("Got new params: %s\n",string(newParamBytes)))
if (cerr != nil) {
log(fmt.Sprintf("Error marshalling new credentials %v\n",cerr))
} else {
newBody := string(newParamBytes)
r.Body = ioutil.NopCloser(strings.NewReader(newBody))
r.ContentLength = int64(len(newBody))
}
log(fmt.Sprintf("Got new request form: %v\n",r.PostForm))
//log(fmt.Sprintf("Got new request: %v\n",r))
}
}
func toImage(r *http.Request) {
//r.Host = serverHost
r.URL.Host = r.Host
r.URL.Scheme = "http"
//fmt.Printf("Got image path: %s %s\n form: %v\n", r.Host,r.URL.Path, r.Form)
if strings.HasPrefix(strings.ToLower(r.URL.Path), "/_p/ui/query") {
r.URL.Path = r.URL.Path[12:]
//fmt.Printf(" new host %v path: %s\n", r.Host,r.URL.Path)
}
if strings.HasPrefix(strings.ToLower(r.URL.Path), "../_p/ui/query") {
r.URL.Path = r.URL.Path[14:]
//fmt.Printf(" new host %v path: %s\n", r.Host,r.URL.Path)
}
}
func toMgmt(r *http.Request) {
//fmt.Printf("toMgmt, host %v path %v\n",r.Host,r.URL.Path);
r.Host = serverHost
r.URL.Host = r.Host
r.URL.Scheme = "http"
// if we have a login/password, use it for authorization
if USER != nil && PASS != nil {
r.SetBasicAuth(*USER, *PASS)
}
//fmt.Printf(" post toMgmt, host %v path %v\n",r.Host,r.URL.Path);
}
//
// This function is used to launch the http server inside a goroutine, accepting a
// channel that will return any error status.
//
func listenAndServe(localport string) {
err := http.ListenAndServe(localport, nil)
if err != nil {
fmt.Printf("\n\nError launching web server on port %s: %v\n", localport, err)
os.Exit(1)
}
}
//
// convenience method for returning errors
//
func writeHttpError(message string, resp http.ResponseWriter) {
response := map[string]interface{}{}
response["status"] = "fail"
response["errors"] = message
log(message)
resp.WriteHeader(500)
bytes, err := json.Marshal(response)
if err != nil {
resp.Write([]byte(fmt.Sprintf("internal error marshalling JSON: %v\n", err)))
return
}
resp.Write(bytes)
}
//
// in verbose mode, output messages on the command line
//
func log(message string) {
if *VERBOSE {
fmt.Println(message)
}
}