Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSODE #3

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# FancyStore

FancyStore is something something. The admin email and password is in app.js.

## Install

```bash
npm install
```

```bash
npm start
```

or

```bash
npm run watch
```
181 changes: 181 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
const express = require('express');
const path = require('path');
const moment = require('moment');
const fs = require('fs');

const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');

const slots = require('./slots');

const app = express();

let config = {
port: 8080,
store: {
openingTime: moment('8:00:00 AM', 'h:mm:ss A'),
closingTime: moment('9:00:00 PM', 'h:mm:ss A'),
serveDuration: 30,
space: 100
},
admin: {
email: '[email protected]',
password: 'password'
}
};

if (!fs.existsSync('config.json')) {
fs.writeFileSync('config.json', JSON.stringify(config));
} else {
config = JSON.parse(fs.readFileSync('config.json'));
}

app.slots = slots.calculate(config);

// Set of all authTokens encountered.
app.authTokens = new Set();
app.serviceCodes = {};

app.use(bodyParser.urlencoded({ extended: true}));
app.use(cookieParser());
app.use('/', express.static(path.join(__dirname, '/public')));

app.get('/', function(req, res) {
res.sendFile(path.join(__dirname, '/views/index.html'));
});

app.get('/admin/login', function(req, res) {
// If already logged in we skip to /admin.
if (!app.authTokens.has(req.cookies['AuthToken'])) {
res.sendFile(path.join(__dirname, '/views/admin/login.html'));
} else {
res.redirect('/admin');
}
});

app.post('/admin/login', function(req, res) {
const { inputEmail, inputPassword } = req.body;

// Hardcode these bad bois, cause the login system is not the
// point of this app.
if (inputEmail == config.admin.email && inputPassword == config.admin.password) {
// Top tier security here.
let authToken = Math.random().toString();
do {
authToken = Math.random().toString();
} while (app.authTokens.has(authToken));

app.authTokens.add(authToken);
res.cookie('AuthToken', authToken);
res.redirect('/admin');
} else {
// Send them to the home page, cause ehh.
res.status(401);
res.redirect('/');
}
});

app.post('/admin/config', function(req, res) {
if (!app.authTokens.has(req.cookies['AuthToken'])) {
res.sendStatus(401);
return;
}

const { inputAddress, inputSpace, inputOpenHour, inputCloseHour } = req.body;
let openingTime = moment(inputOpenHour, 'HH:mm:ss.SSS')
let closingTime = moment(inputCloseHour, 'HH:mm:ss.SSS')

if (closingTime.valueOf() < openingTime.valueOf()) {
// TODO: ERROR
} else {
config.store.openingTime = openingTime;
config.store.closingTime = closingTime;
config.store.address = inputAddress;
config.store.space = inputSpace;

// TODO: Check if valid.
fs.writeFileSync('config.json', JSON.stringify(config));
}
});

app.post('/admin/srvcode', function(req, res) {
try {
const {inputServiceCode} = req.body;
let serviceCode = app.serviceCodes[inputServiceCode];
if (moment().valueOf() > moment(serviceCode.endTime).valueOf()) {
adminIo.emit('nook');
} else {
adminIo.emit('ok');
}
} catch (e) {
adminIo.emit('nook');
}

res.sendStatus(204);
});

app.get('/admin', function(req, res) {
if (!app.authTokens.has(req.cookies['AuthToken'])) {
res.redirect('/admin/login');
} else {
res.sendFile(path.join(__dirname, '/views/admin/index.html'));
}
});

const server = app.listen(config.port, () => console.log(`Listening on port ${config.port}`));
const io = require('socket.io').listen(server);

function generateServiceCode() {
let result = '';
for (let i = 0; i < 8; i++)
result += Math.floor(Math.random() * 10);
return result;
}

let adminIo = io.of('/admin');
adminIo.on('connection', function(socket) {
// something
});

io.on('connection', function(socket) {
socket.booked = false;

socket.on('book', data => {
try {
if (socket.booked === true) {
socket.emit('booked', {message: 'Already booked'});
return;
}

app.slots[data.id].count++;

let serviceCode = generateServiceCode();
do {
serviceCode = generateServiceCode();
} while (serviceCode in app.serviceCodes)

app.serviceCodes[serviceCode] = {
code: serviceCode,
startTime: app.slots[data.id].startTime,
endTime: app.slots[data.id].endTime
};

socket.booked = true;
socket.emit('code', {code: serviceCode});
io.emit('slots', app.slots);


// May the lord forgive us for this sync call.
//
// NOTE: This is mostly because of the uncertainty of if it can happen
// that we fire 2 write call at the same time and end up with file in use
// errors.
//fs.writeFileSync('slots.json', JSON.stringify(app.slots));
} catch (e) {
// Client is doing sketchy things.
console.log(e);
}
});

socket.emit('slots', app.slots);
});
15 changes: 15 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const moment = require('moment');

module.exports = {
port: 8080,
store: {
openingTime: moment('8:00:00 AM', 'h:mm:ss A'),
closingTime: moment('9:00:00 PM', 'h:mm:ss A'),
avgServeDuration: 30,
surfaceArea: 100
},
admin: {
email: '[email protected]',
password: 'password'
}
};
1 change: 1 addition & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"port":8080,"store":{"openingTime":"2020-04-01T04:00:00.000Z","closingTime":"2020-04-01T17:00:00.000Z","serveDuration":30,"space":100},"admin":{"email":"[email protected]","password":"password"}}
Loading