-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
90 lines (71 loc) · 2.55 KB
/
index.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
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
const mongo = require('mongodb')
const MongoClient = mongo.MongoClient
const assert = require('assert')
const bodyParser = require('body-parser')
const express = require('express')
const session = require('express-session')
const cookieParser = require('cookie-parser')
const cors = require('cors')
const app = express()
const port = 8081
app.set('trust proxy', 1)
app.use(session({name: 'sessionID', secret: "secretsecretsecret", cookie: {maxAge: 30000}, resave: false, saveUninitialized: false}))
app.use(bodyParser.urlencoded({extended: true}))
app.use(cors({optionsSuccessStatus: 200}))
const basepath = '/roboticslog/'
app.use(express.json())
var router = express.Router()
app.use(basepath, router)
const dbname = 'roboticsfunding-log'
const dburl = 'mongodb://webapp:roboticsrulez@localhost:27017/'+dbname
const client = new MongoClient(dburl, { useNewUrlParser: true })
let db
client.connect(function(err) {
assert.equal(null, err)
console.log('Connected successfully to server')
db = client.db(dbname)
})
function addSell(date, seller, type, amount){
let sell = {date: date, seller: seller, type: type, amount: amount}
db.collection('sells').insertOne(sell, (err, result) => {
assert.equal(null, err)
console.log('inserted ' + JSON.stringify(sell))
})
}
router.get('/admin.js', (req, res) => {
if(req.session.granted) res.sendFile(__dirname + '/admin.js')
else res.sendStatus(401)
})
app.use(basepath, express.static(__dirname))
router.post('/verify', (req, res) => {
if(!req.session.granted){
res.sendStatus(401)
return
}
console.log(req.body)
db.collection('sells').updateOne({_id: new mongo.ObjectID(req.body._id)}, {$set: {verified: req.body.verified}}, (err, result) => {
assert.equal(null, err)
if(req.body.verified) console.log('verified ' + JSON.stringify(req.body._id))
else console.log('un-verified ' + JSON.stringify(req.body._id))
})
res.status(200).send(req.body.verified)
})
router.post('/add', (req, res) => {
//res.send(JSON.stringify(req.body))
addSell(req.body.date/*new Date().toISOString()*/, req.body.name, req.body.type, req.body.amount)
//res.send({"ok": "1"})
res.redirect(basepath)
})
router.get('/list', (req, res) => {
let callback = res.send.bind(res)
db.collection('sells').find({}).toArray(function(err, docs) {
assert.equal(err, null)
console.log("Found records on request.")
callback(docs)
})
})
router.get('/secretlogin', (req,res) => {
req.session.granted = true
res.redirect(basepath)
})
app.listen(port, () => console.log(`Example app listening on port ${port}!`))