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