2019-02-12 16:55:46 +01:00
|
|
|
process.env['NODE_ENV'] = 'production';
|
2019-02-16 17:55:35 +01:00
|
|
|
//'AIzaSyA21Odw9UdfiOJnxvA_eDfOW0OxuUfHKig' = 'AIzaSyA21Odw9UdfiOJnxvA_eDfOW0OxuUfHKig';
|
2018-11-08 18:36:12 +01:00
|
|
|
const express = require('express');
|
2019-01-14 19:23:29 +01:00
|
|
|
const compression = require('compression')
|
2018-12-01 03:02:13 +01:00
|
|
|
const bodyParser = require('body-parser');
|
|
|
|
|
const low = require('lowdb');
|
2018-12-02 21:02:12 +01:00
|
|
|
const lodashId = require('lodash-id');
|
2018-12-01 03:02:13 +01:00
|
|
|
const FileAsync = require('lowdb/adapters/FileAsync');
|
|
|
|
|
const app = express(); // app is an instance of express
|
2018-11-08 18:36:12 +01:00
|
|
|
|
2019-01-14 19:23:29 +01:00
|
|
|
app.use(compression()); // enables gzip compression
|
2018-12-01 03:02:13 +01:00
|
|
|
app.use(bodyParser.urlencoded({ extended: false })) // parse application/x-www-form-urlencoded
|
|
|
|
|
app.use(bodyParser.json()) // parse application/json
|
2018-12-02 21:02:12 +01:00
|
|
|
|
2019-01-14 19:23:29 +01:00
|
|
|
|
2019-02-14 17:01:45 +01:00
|
|
|
app.get('/a', (req, res) => {
|
|
|
|
|
res.send(process.env);
|
2019-01-14 19:23:29 +01:00
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
2018-12-01 03:02:13 +01:00
|
|
|
// Create database instance and start server
|
|
|
|
|
// const adapter = new FileAsync(__dirname + '/db.json');
|
2018-12-02 21:02:12 +01:00
|
|
|
low(new FileAsync(__dirname + '/db.json')) // production
|
2018-12-01 03:02:13 +01:00
|
|
|
.then(db => {
|
2018-12-02 21:02:12 +01:00
|
|
|
db._.mixin(lodashId);
|
2018-12-01 03:02:13 +01:00
|
|
|
// DATABASE ROUTES
|
2018-11-08 18:36:12 +01:00
|
|
|
|
2019-02-14 17:01:45 +01:00
|
|
|
// ==============
|
2018-12-02 21:02:12 +01:00
|
|
|
|
2019-02-14 17:01:45 +01:00
|
|
|
app.post('/test', (req, res) => {
|
|
|
|
|
// "reason": {
|
|
|
|
|
// "fitali": req.body.reason.toString() === "fvitali" ? 1:0,
|
|
|
|
|
// "": req.body.reason.toString() === "" ? 1:0,
|
|
|
|
|
// "": req.body.reason.toString() === "" ? 1:0,
|
|
|
|
|
// }
|
2018-12-03 01:22:48 +01:00
|
|
|
// try {
|
|
|
|
|
// let test = table.updateById(req.body.id, { })
|
|
|
|
|
// }
|
|
|
|
|
// catch(e) {
|
|
|
|
|
// }
|
|
|
|
|
res.send(req.body);
|
|
|
|
|
});
|
|
|
|
|
|
2019-02-14 17:01:45 +01:00
|
|
|
// ==============
|
2018-12-03 01:22:48 +01:00
|
|
|
|
2019-02-14 17:01:45 +01:00
|
|
|
// GET /globpop
|
2019-02-12 16:55:46 +01:00
|
|
|
app.get('/globpop', (req, res) => {
|
2019-02-14 17:01:45 +01:00
|
|
|
req.query.id ? res.send(req.query.id) : res.status(400).send('Bad Request');
|
2019-02-12 16:55:46 +01:00
|
|
|
});
|
|
|
|
|
|
2019-02-14 17:01:45 +01:00
|
|
|
// OPTIONS /videotrack
|
|
|
|
|
app.options('/videotrack', (req, res) => {
|
|
|
|
|
res.set({
|
|
|
|
|
'Access-Control-Allow-Origin': '*',
|
|
|
|
|
'Access-Control-Allow-Methods': 'POST, GET, OPTIONS',
|
|
|
|
|
'Access-Control-Allow-Headers': 'Content-Type',
|
|
|
|
|
'Access-Control-Max-Age': 600
|
|
|
|
|
})
|
|
|
|
|
res.status(200).end();
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// GET /videotrack
|
|
|
|
|
app.get('/videotrack', (req, res) => {
|
2018-12-03 01:22:48 +01:00
|
|
|
const videos = db.get('videos');
|
2019-02-14 17:01:45 +01:00
|
|
|
req.query.id ? res.send(videos.getById(req.query.id).value()) : res.send(videos.value())
|
2018-12-03 01:22:48 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// POST /videotrack
|
|
|
|
|
app.post('/videotrack', (req, res) => {
|
2019-02-18 03:58:51 +01:00
|
|
|
res.set({
|
|
|
|
|
'Access-Control-Allow-Origin': '*'
|
|
|
|
|
});
|
2019-02-14 17:01:45 +01:00
|
|
|
const videos = db.get('videos');
|
|
|
|
|
const row = videos.getById(req.body.id);
|
|
|
|
|
if (row.value()) {
|
2018-12-02 21:02:12 +01:00
|
|
|
row
|
2019-02-14 17:01:45 +01:00
|
|
|
.update('timesWatched', (n) => ++n)
|
|
|
|
|
.update('lastWatched', () => new Date().toISOString())
|
|
|
|
|
.write()
|
|
|
|
|
.then(output => res.send(output))
|
2018-12-02 21:02:12 +01:00
|
|
|
} else {
|
2018-12-03 01:22:48 +01:00
|
|
|
let newRow = {
|
2019-02-14 17:01:45 +01:00
|
|
|
"id": req.body.id.toString(),
|
2018-12-02 21:02:12 +01:00
|
|
|
"timesWatched": 1,
|
|
|
|
|
"lastWatched": new Date().toISOString(),
|
|
|
|
|
}
|
2019-02-18 03:58:51 +01:00
|
|
|
videos.push(newRow).last().write().then(output => res.send(200,output))
|
2018-12-02 21:02:12 +01:00
|
|
|
}
|
2018-12-03 01:22:48 +01:00
|
|
|
});
|
2019-02-14 17:01:45 +01:00
|
|
|
|
2018-12-01 03:02:13 +01:00
|
|
|
// Set db default values
|
2018-12-03 01:22:48 +01:00
|
|
|
return db.defaults({ videos: [] }).write();
|
2018-12-01 03:02:13 +01:00
|
|
|
})
|
|
|
|
|
.then(() => {
|
2019-02-12 16:55:46 +01:00
|
|
|
app.use(express.static(__dirname + '/build')); // serve static build site
|
2018-12-01 03:02:13 +01:00
|
|
|
app.listen(8000, () => console.log('listen on 8000')); // bind to port 8000 as required from specs
|
|
|
|
|
});
|