This commit is contained in:
janis
2023-11-09 16:12:05 +01:00
parent b37c6c3635
commit 5037663fd5
11 changed files with 1266 additions and 29 deletions

View File

@@ -4,7 +4,6 @@ const path = require( 'path' );
const expressSession = require( 'express-session' );
const fs = require( 'fs' );
const bodyParser = require( 'body-parser' );
const cookieParser = require( 'cookie-parser' )
const favicon = require( 'serve-favicon' );
const static = require( 'express-static' );
@@ -17,27 +16,67 @@ app.use( expressSession ( {
} ) );
app.use( bodyParser.urlencoded( { extended: false } ) );
app.use( bodyParser.json() );
app.use( cookieParser() );
app.use( favicon( path.join( __dirname + '' ) ) );
app.use( static() );
// app.use( favicon( path.join( __dirname + '' ) ) );
let connectedClients = {};
let currentDetails = {
'songQueue': [],
'playingSong': {},
'pos': 0,
'isPlaying': false,
'queuePos': 0,
};
app.get( '/', ( request, response ) => {
response.sendFile( path.join( __dirname + '/ui/index.html' ) );
} );
app.get( '/showcase.js', ( request, response ) => {
response.sendFile( path.join( __dirname + '/ui/showcase.js' ) );
} );
app.get( '/showcase.css', ( request, response ) => {
response.sendFile( path.join( __dirname + '/ui/showcase.css' ) );
} );
app.post( '/connect', ( request, response ) => {
if ( request.session.authorized ) {
response.send( 'Already authenticated' );
if ( request.body.authKey === authKey ) {
request.session.authorized = true;
response.send( 'Handshake OK' );
} else {
if ( request.body.authKey === authKey ) {
request.session.authorized = true;
response.send( 'Handshake OK' );
} else {
response.status( 403 ).send( 'AuthKey wrong' );
}
response.status( 403 ).send( 'AuthKey wrong' );
}
} );
app.post( '/statusUpdate/:update', ( req, res ) => {
if ( req.session.authorized ) {
res.send( 'thanks' );
app.get( '/clientDisplayNotifier', ( req, res ) => {
res.writeHead( 200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
} );
res.status( 200 );
res.flushHeaders();
let det = { 'type': 'basics', 'data': currentDetails };
res.write( `data: ${ JSON.stringify( det ) }\n\n` );
connectedClients[ req.session.id ] = res;
} );
const sendUpdate = ( update ) => {
for ( let client in connectedClients ) {
connectedClients[ client ].write( 'data: ' + JSON.stringify( { 'type': update, 'data': currentDetails[ update ] } ) + '\n\n' );
}
};
const allowedTypes = [ 'playingSong', 'isPlaying', 'songQueue', 'pos', 'queuePos' ];
app.post( '/statusUpdate', ( req, res ) => {
if ( req.body.authKey === authKey ) {
if ( allowedTypes.includes( req.body.type ) ) {
currentDetails[ req.body.type ] = req.body.data
res.send( 'thanks' );
sendUpdate( req.body.type );
} else {
res.status( 400 ).send( 'incorrect request' );
}
} else {
res.status( 403 ).send( 'Unauthorized' );
}
@@ -47,5 +86,5 @@ app.use( ( request, response, next ) => {
response.sendFile( path.join( __dirname + '' ) )
} );
const PORT = process.env.PORT || 8080;
http.createServer( app ).listen( PORT );
const PORT = process.env.PORT || 3000;
app.listen( PORT );