-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
59 lines (55 loc) · 1.6 KB
/
server.js
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
'use strict'
const http = require('http')
const r = require('rethinkdb')
require('rethinkdb-init')(r)
const j = (obj) => JSON.stringify(obj, null, ' ')
const port = parseInt(process.env.PORT || 80, 10)
http.createServer(function (req, res) {
let opts = {
IS_MIRRORED_DOCKERFILE: process.env.IS_MIRRORED_DOCKERFILE,
RUNNABLE_CONTAINER_ID: process.env.RUNNABLE_CONTAINER_ID,
HOSTNAME: process.env.HOSTNAME,
RETHINKDB: process.env.RETHINKDB
}
if (process.env.NAME) {
opts.NAME = process.env.NAME
}
res.writeHead(200, {'Content-Type': 'text/plain'})
if (!process.env.RETHINKDB) {
console.log('No `RETHINKDB` ENV vars set')
res.end(j({
message: 'Hello: No RethinkDB Variables set',
opts: opts
}))
}
console.log('Connecting to Rethinkdb...')
r.init({
host: process.env.RETHINKDB,
db: process.env.DB_NAME || 'hello_node_rethinkdb'
}, [
'hello_world',
process.env.TABLE_NAME || 'master'
])
.then(function (conn) {
console.log('Getting db list...')
return Promise.all([r.dbList().run(conn), r.tableList().run(conn)])
})
.spread(function (dbList, tableList) {
console.log('DB list...', dbList)
res.end(j({
message: 'Hello: Succesfully connected to DB',
opts: opts,
dbList: dbList,
tableList
}))
})
.catch(function (err) {
console.log('Error: ', err)
res.end(j({
message: `Hello: Error connecting to DB ${process.env.RETHINKDB}`,
opts: opts,
err: err
}))
})
}).listen(port)
console.log(`Server running at http://127.0.0.1:${port}/`)