diff --git a/server.js b/server.js deleted file mode 100644 index 6f71db1..0000000 --- a/server.js +++ /dev/null @@ -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 - -*/ diff --git a/src/server.js b/src/server.js new file mode 100644 index 0000000..1e43a7d --- /dev/null +++ b/src/server.js @@ -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)