mirror of
https://github.com/KevinMidboe/ISPDowntimeMonitor.git
synced 2025-10-29 09:40:12 +00:00
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:
42
server.js
42
server.js
@@ -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
53
src/server.js
Normal 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)
|
||||||
Reference in New Issue
Block a user