-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
129 lines (109 loc) · 3.42 KB
/
Copy pathapp.js
File metadata and controls
129 lines (109 loc) · 3.42 KB
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
// require('dotenv').configure()
const mongoose = require('mongoose')
const express = require('express')
const http = require('http')
const WebSocket = require('ws')
const app = express()
const MONGO_HOST = process.env.MONGO_HOST
const MONGO_USER = process.env.MONGO_USER
const MONGO_PASS = process.env.MONGO_PASS
const MONGO_PORT = process.env.MONGO_PORT
const COFFEE_DB = process.env.COFFEE_DB
if (!MONGO_HOST || !MONGO_USER || !MONGO_PORT || !MONGO_PASS|| !COFFEE_DB) {
console.log('Please populate both MONGO_HOST, MONGO_USER, MONGO_PASS, MONGO_PORT and COFFEE_DB environment variables')
process.exit()
}
const DB_URL = `mongodb://${MONGO_USER}:${MONGO_PASS}@${MONGO_HOST}:${MONGO_PORT}/${COFFEE_DB}`
const [ AVAILABLE, UNAVAILABLE ] = ['available', 'unavailable']
const Schema = mongoose.Schema
const coffeeStatusSchema = new Schema({
status: String
})
const getCurrentStatus = () => new Promise((resolve, error) => {
return CoffeeStatus.find().sort({ _id: 'desc' }).exec((err, docs) => {
if (err) return error(err)
else return resolve(docs[0])
}).catch(console.log)
}).catch(console.log)
const setStatus = status => new Promise((resolve, error) => {
return (new CoffeeStatus({ status })).
save((err, newStatus) => {
if (err) return error(err)
else return resolve(newStatus)
}).catch(console.log)
}).catch(console.log)
const CoffeeStatus = mongoose.model('CoffeeStatus', coffeeStatusSchema)
mongoose.connect(DB_URL)
app.get('/', (req, resp) => { resp.status(200); resp.send('hi') })
app.get('/coffee', (req, resp) => {
getCurrentStatus().
then(currentStatus => {
resp.status(200)
resp.send({ status: currentStatus.status })
})
})
app.post('/coffee', (req, resp) => {
setStatus(AVAILABLE).
then(statusAfterUpdate => {
resp.status(200)
resp.send({ status: statusAfterUpdate.status })
wss.broadcast(statusAfterUpdate.status)
})
})
app.delete('/coffee', (req, resp) => {
getCurrentStatus().
then(currentStatus => {
if (currentStatus.status == AVAILABLE) {
setStatus(UNAVAILABLE).
then(statusAfterUpdate => {
resp.status(200)
resp.send({ status: statusAfterUpdate.status })
wss.broadcast(statusAfterUpdate.status)
})
}
else {
resp.status(409)
resp.send({ message: 'coffee already unavailable' })
}
})
})
const server = http.createServer(app)
server.listen(process.env.PORT || 3000, () => {
getCurrentStatus().then(status => {
if (typeof status === 'undefined')
setStatus(UNAVAILABLE).
then(status => console.log('set default status to unavailable'))
console.log('listening')
})
}).on('error', console.log)
const wss = new WebSocket.Server({
server,
clientTracking: true,
perMessageDeflate: false
})
wss.on('connection', ws => {
console.log('client connected')
getCurrentStatus().
then(status => ws.send(status.status)).
catch(err => ws.send(err))
ws.on('message', message => {
console.log(message)
}).on('close', client => {
console.log('client disconnected')
}).on('error', client => {
console.log('client error')
})
})
setInterval(() => {
wss.clients.forEach((client) => {
client.ping()
});
}, 45 * 1000)
wss.broadcast = message => {
console.log('trying to broadcast: ' + message)
wss.clients.forEach(client => {
if (client.readyState === WebSocket.OPEN) {
client.send(message)
}
})
}