-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
61 lines (55 loc) · 1.49 KB
/
server.js
File metadata and controls
61 lines (55 loc) · 1.49 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
const express = require("express")
const app = express()
const Oreki = require("./index").Oreki
const bodyParser = require("body-parser")
module.exports = function(configPath) {
const oreki = new Oreki(configPath)
app.use(bodyParser.urlencoded({
extended: true
}))
app.use(bodyParser.json())
let paymentBuffer = []
app.post("/", function(req, res, next) {
const password = req.body.password
if (password !== oreki.config.password) {
res.status(403).json({ msg: "forbidden" })
return
}
res.json({ payments: paymentBuffer })
paymentBuffer = []
})
app.post("/payment", function(req, res, next) {
const userId = req.body.user_id
const endpointId = req.body.endpoint
const point = req.body.point
const price = req.body.price
const password = req.body.password
if (password !== oreki.config.password) {
res.status(403).json({ msg: "forbidden" })
return
}
oreki.addPayment(userId, endpointId, point, price).then(function(payment) {
res.json({
payment: payment
})
}).catch(function(err) {
res.status(500).json(err)
})
})
app.listen(3000, function() {
console.log("oreki started")
});
(async() => {
const initialized = await oreki.init()
if (!initialized) {
console.error("couldn't initialized")
return
}
oreki.on("paid", function(payment) {
console.log("PAID")
console.log(payment)
paymentBuffer.push(payment)
})
oreki.start()
})()
}