menuserver/server.js
Martin Donnelly 38e9027d09 Update
2024-02-26 13:42:32 +00:00

86 lines
2.5 KiB
JavaScript

'use strict';
require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const session = require('express-session');
const path = require('path');
const helmet = require('helmet');
const cors = require('cors');
const cron = require('node-cron');
const db = require('./server/lib/loginmanager');
const doJob = require('./server/lib/job');
const checkAuth = require('./server/middle/checkAuth');
const app = express();
require('dotenv').config();
const serverPort = process.env.PORT || 3000;
const localHost = process.env.LOCAL || false;
const sitePath = 'dist';
app.use(cors());
app.use(helmet());
app.use(session({
'secret': 'rBLH5#Q89Z4',
'resave': true,
'saveUninitialized': true
}));
app.get('/', (request, response) => {
/* if (request.session.auth)
response.redirect('/menu');
else
if (!localHost)
response.sendFile(path.join(`${__dirname}/server/static/login.html`));
else {
// fix auth.
request.session.username = 'martin';
request.session.auth = 'jhgkjgkjhgkjhgjkhgjkhgfhghfjgfjhgf';
response.redirect('/menu');
}*/
request.session.username = 'martin';
request.session.auth = 'jhgkjgkjhgkjhgjkhgjkhgfhghfjgfjhgf';
response.redirect('/menu');
});
app.get('/menu', checkAuth, (req, res) => {
res.sendFile(path.join(`${__dirname}/dist/index.html`));
});
app.use(express.static(path.join(__dirname, sitePath)));
app.use(bodyParser.urlencoded({ 'extended': true }));
app.use(bodyParser.json());
app.post('/auth', (request, response) => {
const username = request.body.u;
const password = request.body.p;
if (username && password)
db.getOne(username, password)
.then((data) => {
if (!data)
response.redirect('/');
else {
request.session.username = username;
request.session.auth = 'jhgkjgkjhgkjhgjkhgjkhgfhghfjgfjhgf';
response.redirect('/menu');
}
})
.catch((err) => {
console.log(err);
response.status(500).send({
'message': err.message || 'Some error occurred while querying the database.'
});
});
else {
response.send('Please enter Username and Password!');
response.end();
}
});
require('./server/routes/recipe.routes')(app);
require('./server/routes/view.routes')(app);
app.listen(serverPort, () => {
console.log(`Server is listening on port ${serverPort}`);
});
((() => {
console.log('Menuserver 2.0 started');
})());
cron.schedule('0 8 * * 6', function () {
doJob();
return -1;
});