Add static file routing

Signed-off-by: William Brawner <me@wbrawner.com>
This commit is contained in:
William Brawner 2020-09-15 07:31:58 -07:00
parent b71cc57919
commit 7f14bc11e7
5 changed files with 55 additions and 21 deletions

9
package-lock.json generated
View file

@ -550,6 +550,15 @@
"resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz", "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz",
"integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=" "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac="
}, },
"cors": {
"version": "2.8.5",
"resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz",
"integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==",
"requires": {
"object-assign": "^4",
"vary": "^1"
}
},
"cross-spawn": { "cross-spawn": {
"version": "7.0.3", "version": "7.0.3",
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz", "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz",

View file

@ -12,6 +12,7 @@
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"bcrypt": "^5.0.0", "bcrypt": "^5.0.0",
"cors": "^2.8.5",
"express": "^4.17.1", "express": "^4.17.1",
"express-basic-auth": "^1.2.0", "express-basic-auth": "^1.2.0",
"mysql": "^2.18.1" "mysql": "^2.18.1"

View file

@ -6,13 +6,14 @@ const pool = require('./db.js');
const express = require('express'); const express = require('express');
const basicAuth = require('express-basic-auth'); const basicAuth = require('express-basic-auth');
const basicAuthConfig = require('./config.js') const basicAuthConfig = require('./config.js')
const cors = require('cors');
class Event { class Event {
static types = [ static types = [
'view', 'VIEW',
'click', 'CLICK',
'error', 'ERROR',
'crash', 'CRASH',
]; ];
id = randomId(32); id = randomId(32);
@ -167,16 +168,18 @@ router.get('/', basicAuth(basicAuthConfig), (req, res) => {
// events will be coming from all over the place, I don't think it makes // events will be coming from all over the place, I don't think it makes
// sense to try to put auth in front of this. Even some kind of client // sense to try to put auth in front of this. Even some kind of client
// "secret" would be trivial to deduce by examining the requests. // "secret" would be trivial to deduce by examining the requests.
router.post('/', (req, res) => { router.post('/', cors({origin: true, methods: ['POST']}), (req, res) => {
console.log(req.body);
if (typeof req.body.appId === "undefined") { if (typeof req.body.appId === "undefined") {
res.status(400).json({ message: 'Invalid appId' }); res.status(400).json({ message: 'Invalid appId' });
return; return;
} }
if (typeof req.body.sessionId === "undefined") { // Without Cookies, websites can't consistently send the same sessionId
res.status(400).json({ message: 'Invalid sessionId' }); // if (typeof req.body.sessionId === "undefined") {
return; // res.status(400).json({ message: 'Invalid sessionId' });
} // return;
// }
if (Event.types.indexOf(req.body.type) === -1) { if (Event.types.indexOf(req.body.type) === -1) {
res.status(400).json({ message: 'Invalid event type' }); res.status(400).json({ message: 'Invalid event type' });
@ -192,7 +195,7 @@ router.post('/', (req, res) => {
EventRepository.createEvent(new Event( EventRepository.createEvent(new Event(
req.body.appId, req.body.appId,
new Date(req.body.date), new Date(req.body.date),
req.body.userAgent, req.headers['User-Agent'],
req.body.platform, req.body.platform,
req.body.manufacturer, req.body.manufacturer,
req.body.model, req.body.model,

View file

@ -1,23 +1,21 @@
const express = require('express'); const express = require('express');
const eventRouter = require('./event.js').router; const eventRouter = require('./event.js').router;
const port = require('./config.js').port; const port = require('./config.js').port;
const randomId = require('./util.js').randomId;
const appRouter = require('./app.js').router; const appRouter = require('./app.js').router;
const cors = require('cors');
const app = express(); const app = express();
app.use(express.json()); app.use(express.json());
app.use(cors()); // TODO: Enable this for event creation only
app.get('/', (req, res) => { // app.get('/id', (req, res) => {
res.send('Hello, world!'); // const length = Number.parseInt(req.query['length']) || 32;
}); // res.send(require('./util.js').randomId(length));
// });
app.get('/id', (req, res) => { app.use(express.static('server/static'));
const length = Number.parseInt(req.query['length']) || 32; app.use('/api/apps', appRouter);
res.send(randomId(length)); app.use('/api/events', eventRouter);
});
app.use('/api/apps', appRouter)
app.use('/api/events', eventRouter)
app.listen(port, () => { app.listen(port, () => {
console.log(`Started Flayre server on port ${port}`); console.log(`Started Flayre server on port ${port}`);

23
server/static/flayre.js Normal file
View file

@ -0,0 +1,23 @@
(function () {
if (window.navigator.doNotTrack === '1') {
console.log('Flayre respects DNT');
return;
}
const flayreDomain = document.currentScript.src.split('/').slice(0, 3).join('/');
const app = document.currentScript.dataset.app;
fetch(`${flayreDomain}/api/events`, {
method: 'POST',
headers: {
'Content-type': 'application/json',
},
body: JSON.stringify({
appId: app,
date: new Date().toISOString(),
platform: window.navigator.platform,
locale: window.navigator.language,
data: window.location.pathname,
type: 'VIEW'
})
});
})();