New endpoints and move server.js to src/.

New endpoints include:
 - /logs : all events logged in the database
 - /logs/alternating : events where the same consecutive status is
 filtered out.
 - /logs/:id : get a log message by mongo id.
 - /uptime : get all events, but w/ selected on date & isOk.
 - /pdf/:id : get the pdf file by id.

TODO: Error handling. This is all best case. Should be properly logged.
This commit is contained in:
2020-06-07 23:42:27 +02:00
committed by KevinMidboe
parent 3af4c99eb3
commit 24384527b2
2 changed files with 53 additions and 42 deletions

View File

@@ -1,42 +0,0 @@
const express = require('express')
const app = express()
const path = require('path')
const indexPage = path.join(__dirname + '/templates/index.html')
app.get('/', (req, res) => res.sendFile(indexPage));
app.listen(3000)
/*
GET
/address
Scrape the address that the url is linked to.
- check if we have it in db first.
/history
Column data: Datetime | Reason | Duration
/history/:date
Get information for given date.
Then also get pdf file to frontend.
/uptime
Data graph for w/ red and green interval bars
PAGES
/index.html
What address is being monitored
Bar graph of time intervals w/ green and red indication of downtime.
(date below)
Activity log
/incident/:date
*/

53
src/server.js Normal file
View File

@@ -0,0 +1,53 @@
const express = require('express')
const fs = require('fs');
const app = express()
const config = require('../config')
const { getAllEvents, getEventById, getAlternatingEventStatuses, getEventStatus } = require('./db.js');
const PORT = 3000;
// Website and assets
app.use(express.static(__dirname + '/site'))
app.get('/', (req, res) => res.sendFile('index.html'));
// Data api endpoints
app.get('/logs', (req, res) =>
getAllEvents()
.then(allEvents => res.send(allEvents))
)
app.get('/logs/alternating', (req, res) =>
getAlternatingEventStatuses()
.then(allEvents => res.send(allEvents))
)
app.get('/logs/:id', (req, res) =>
getEventById(req.params.id)
.then(event => res.send(event))
)
app.get('/uptime', (req, res) =>
getEventStatus()
.then(eventStatuses => res.send(eventStatuses))
)
app.get('/pdf/:id', (req, res) =>
getEventById(req.params.id)
.then(event => {
const path = __dirname + '/../pdfExports/' + event.pdfFilename;
try {
const file = fs.readFileSync(path);
res.setHeader('Content-Type', 'application/pdf');
return res.status(200).send(file)
} catch (err) {
console.error('Error when reading file:', err);
return res.status(500).send({ message: 'Something went wrong reading file.' })
}
})
)
app.listen(PORT)