quelea-web/server/server.js

119 lines
3.0 KiB
JavaScript

const express = require("express");
const compression = require('compression');
const expressSSE = require("express-sse");
const httpProxy = require("http-proxy");
const axios = require("axios");
const path = require("path");
const methodOverride = require("method-override");
const config = require("./config.js");
const rcUrl = config.rcApi + "/lyrics";
const lyricUrl = config.lyricsApi + "/lyrics";
const apiProxy = httpProxy.createProxyServer();
const sse = new expressSSE("");
let lowerLyrics = "";
let newLyrics = "";
let rcLyrics = "";
let stageLyrics = "";
const app = express();
const PORT = process.env.PORT || 3000;
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"}));
app.use(methodOverride("_method"));
// 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'));
});
// Reverse Proxy redirect to Remote Control API
app.all("/rcApi/*", (req, res) => {
console.log("redirecting to Remote Control API");
apiProxy.web(req, res, {target: config.rcApi});
});
// Reverse Proxy redirect to Lyrics API
app.all("/lyricsApi/*", (req, res) => {
console.log("redirecting to Lyrics API");
apiProxy.web(req, res, {target: config.lyricsApi});
});
// Update lyrics by polling every 250ms
setInterval( async() => {
// 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);
});
}, 250);
// SSE Connection to Frontend
app.get('/stream', sse.init);
// SSE Connection to the Frontend
app.get('/lowerthirdsserver', function (request, response) {
response.status(200).set({
"connection": "keep-alive",
"cache-control": "no-cache",
"content-type": "text/event-stream",
});
// const data = "Hello Earl!";
setInterval(() => {
response.write('data:' + lowerLyrics + '\n\n');
}, 250);
});
app.get('/stagesserver', function (request, response) {
response.status(200).set({
"connection": "keep-alive",
"cache-control": "no-cache",
"content-type": "text/event-stream",
});
// const data = "Hello Earl!";
setInterval(() => {
response.write('data:' + stageLyrics + '\n\n');
}, 250);
});
// Keep server running if connection to an API fails
process.on('uncaughtException', (err) => {
console.log(err);
});
// Run the server
app.listen(PORT, () => {
console.log("Server listening on: http://localhost:" + PORT);
});