quelea-web/server/server.js

119 lines
3.0 KiB
JavaScript
Raw Normal View History

2021-09-27 23:00:43 -04:00
const express = require("express");
2021-09-28 21:39:21 -04:00
const compression = require('compression');
const expressSSE = require("express-sse");
2021-09-27 23:00:43 -04:00
const httpProxy = require("http-proxy");
const axios = require("axios");
const path = require("path");
const methodOverride = require("method-override");
2021-09-28 21:39:21 -04:00
const config = require("./config.js");
2021-09-27 23:00:43 -04:00
const rcUrl = config.rcApi + "/lyrics";
const lyricUrl = config.lyricsApi + "/lyrics";
2021-09-28 21:39:21 -04:00
const apiProxy = httpProxy.createProxyServer();
const sse = new expressSSE("");
2021-09-27 23:00:43 -04:00
let lowerLyrics = "";
let newLyrics = "";
let rcLyrics = "";
let stageLyrics = "";
const app = express();
const PORT = process.env.PORT || 3000;
2021-09-28 21:39:21 -04:00
app.use(compression());
app.use(express.json());
app.use(express.urlencoded({extended: true}));
app.use(express.text());
app.use(express.json({type:"application/vnd.api+json"}));
2021-09-27 23:00:43 -04:00
app.use(methodOverride("_method"));
2021-09-28 21:39:21 -04:00
// Proxy all non-api requests to the built app
app.use(express.static(path.join(__dirname, '../app/dist')));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, '../app/dist/index.html'));
});
2021-09-27 23:00:43 -04:00
// Reverse Proxy redirect to Remote Control API
app.all("/rcApi/*", (req, res) => {
2021-09-28 21:39:21 -04:00
console.log("redirecting to Remote Control API");
apiProxy.web(req, res, {target: config.rcApi});
2021-09-27 23:00:43 -04:00
});
// Reverse Proxy redirect to Lyrics API
app.all("/lyricsApi/*", (req, res) => {
2021-09-28 21:39:21 -04:00
console.log("redirecting to Lyrics API");
apiProxy.web(req, res, {target: config.lyricsApi});
2021-09-27 23:00:43 -04:00
});
2021-09-28 21:39:21 -04:00
// Update lyrics by polling every 250ms
2021-09-27 23:00:43 -04:00
setInterval( async() => {
2021-09-28 21:39:21 -04:00
// Quelea Lyrics API GET using Axios
await axios.get(lyricUrl).then(res => {
newLyrics = res.data;
if (!(newLyrics === lowerLyrics)) {
lowerLyrics = newLyrics;
sse.updateInit(newLyrics);
sse.send(newLyrics);
}
})
.catch(error => {
console.log(error);
});
// Quelea Lyrics API GET using Axios
await axios.get(rcUrl).then(res => {
// console.log("Here be the Lyrics " + res.data);
rcLyrics = res.data;
rcLyrics = rcLyrics.replace(/(\r\n|\n|\r)/gm," ");
// console.log(newLyrics);
if (!(rcLyrics === stageLyrics)) {
stageLyrics = rcLyrics;
}
})
.catch(error => {
console.log(error);
});
2021-09-27 23:00:43 -04:00
}, 250);
2021-09-28 21:39:21 -04:00
// SSE Connection to Frontend
app.get('/stream', sse.init);
// SSE Connection to the Frontend
2021-09-27 23:00:43 -04:00
app.get('/lowerthirdsserver', function (request, response) {
2021-09-28 21:39:21 -04:00
response.status(200).set({
"connection": "keep-alive",
"cache-control": "no-cache",
"content-type": "text/event-stream",
});
// const data = "Hello Earl!";
2021-09-27 23:00:43 -04:00
2021-09-28 21:39:21 -04:00
setInterval(() => {
response.write('data:' + lowerLyrics + '\n\n');
}, 250);
2021-09-27 23:00:43 -04:00
});
app.get('/stagesserver', function (request, response) {
2021-09-28 21:39:21 -04:00
response.status(200).set({
"connection": "keep-alive",
"cache-control": "no-cache",
"content-type": "text/event-stream",
});
// const data = "Hello Earl!";
2021-09-27 23:00:43 -04:00
2021-09-28 21:39:21 -04:00
setInterval(() => {
response.write('data:' + stageLyrics + '\n\n');
}, 250);
2021-09-27 23:00:43 -04:00
});
// Keep server running if connection to an API fails
process.on('uncaughtException', (err) => {
2021-09-28 21:39:21 -04:00
console.log(err);
2021-09-27 23:00:43 -04:00
});
// Run the server
app.listen(PORT, () => {
2021-09-28 21:39:21 -04:00
console.log("Server listening on: http://localhost:" + PORT);
2021-09-27 23:00:43 -04:00
});