mirror of
https://github.com/glenndehaan/unifi-voucher-site.git
synced 2026-04-05 08:53:53 -04:00
Implemented API endpoints. Update README.md. Implemented health endpoint. Updated dependencies
This commit is contained in:
248
server.js
248
server.js
@@ -1,6 +1,7 @@
|
||||
/**
|
||||
* Import base packages
|
||||
*/
|
||||
const os = require('os');
|
||||
const express = require('express');
|
||||
const multer = require('multer');
|
||||
const cookieParser = require('cookie-parser');
|
||||
@@ -30,6 +31,8 @@ const app = express();
|
||||
*/
|
||||
const random = (min, max) => Math.floor(Math.random() * (max - min)) + min;
|
||||
const voucherTypes = types(process.env.VOUCHER_TYPES || '480,0,,,;');
|
||||
const webService = (process.env.SERVICE_WEB === 'true') || true;
|
||||
const apiService = (process.env.SERVICE_API === 'true') || false;
|
||||
const authDisabled = (process.env.DISABLE_AUTH === 'true') || false;
|
||||
|
||||
/**
|
||||
@@ -37,6 +40,12 @@ const authDisabled = (process.env.DISABLE_AUTH === 'true') || false;
|
||||
*/
|
||||
logo();
|
||||
|
||||
/**
|
||||
* Log service status
|
||||
*/
|
||||
console.log(`[Service][Web] ${webService ? 'Enabled!' : 'Disabled!'}`);
|
||||
console.log(`[Service][Api] ${apiService ? 'Enabled!' : 'Disabled!'}`);
|
||||
|
||||
/**
|
||||
* Log voucher types
|
||||
*/
|
||||
@@ -48,7 +57,7 @@ voucherTypes.forEach((type, key) => {
|
||||
/**
|
||||
* Log auth status
|
||||
*/
|
||||
console.log(`[AUTH] ${authDisabled ? 'Disabled!' : 'Enabled!'}`);
|
||||
console.log(`[Auth] ${authDisabled ? 'Disabled!' : 'Enabled!'}`);
|
||||
|
||||
/**
|
||||
* Log controller
|
||||
@@ -66,6 +75,19 @@ app.enable('trust proxy');
|
||||
app.set('view engine', 'ejs');
|
||||
app.set('views', `${__dirname}/template`);
|
||||
|
||||
/**
|
||||
* GET /_health - Health check page
|
||||
*/
|
||||
app.get('/_health', (req, res) => {
|
||||
res.json({
|
||||
status: 'UP',
|
||||
host: os.hostname(),
|
||||
load: process.cpuUsage(),
|
||||
mem: process.memoryUsage(),
|
||||
uptime: process.uptime()
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* Enable multer
|
||||
*/
|
||||
@@ -108,89 +130,52 @@ app.use(express.static(`${__dirname}/public`));
|
||||
* Configure routers
|
||||
*/
|
||||
app.get('/', (req, res) => {
|
||||
res.redirect(302, '/voucher');
|
||||
});
|
||||
app.get('/login', (req, res) => {
|
||||
// Check if authentication is disabled
|
||||
if(authDisabled) {
|
||||
if(webService) {
|
||||
res.redirect(302, '/voucher');
|
||||
return;
|
||||
}
|
||||
|
||||
const hour = new Date().getHours();
|
||||
const timeHeader = hour < 12 ? 'Good Morning' : hour < 18 ? 'Good Afternoon' : 'Good Evening';
|
||||
|
||||
res.render('login', {
|
||||
error: req.flashMessage.type === 'error',
|
||||
error_text: req.flashMessage.message || '',
|
||||
banner_image: process.env.BANNER_IMAGE || `/images/bg-${random(1, 10)}.jpg`,
|
||||
app_header: timeHeader,
|
||||
sid: req.sid
|
||||
});
|
||||
});
|
||||
app.post('/login', async (req, res) => {
|
||||
if(typeof req.body === "undefined") {
|
||||
res.status(400).send();
|
||||
return;
|
||||
}
|
||||
|
||||
const passwordCheck = req.body.password === (process.env.SECURITY_CODE || "0000");
|
||||
|
||||
if(!passwordCheck) {
|
||||
res.cookie('flashMessage', JSON.stringify({type: 'error', message: 'Password Invalid!'}), {httpOnly: true, expires: new Date(Date.now() + 24 * 60 * 60 * 1000)}).redirect(302, '/login');
|
||||
return;
|
||||
}
|
||||
|
||||
res.cookie('authorization', req.body.password, {httpOnly: true, expires: new Date(Date.now() + 24 * 60 * 60 * 1000)}).redirect(302, '/voucher');
|
||||
});
|
||||
app.get('/voucher', [authorization], async (req, res) => {
|
||||
const hour = new Date().getHours();
|
||||
const timeHeader = hour < 12 ? 'Good Morning' : hour < 18 ? 'Good Afternoon' : 'Good Evening';
|
||||
|
||||
res.render('voucher', {
|
||||
info: req.flashMessage.type === 'info',
|
||||
info_text: req.flashMessage.message || '',
|
||||
error: req.flashMessage.type === 'error',
|
||||
error_text: req.flashMessage.message || '',
|
||||
banner_image: process.env.BANNER_IMAGE || `/images/bg-${random(1, 10)}.jpg`,
|
||||
app_header: timeHeader,
|
||||
sid: req.sid,
|
||||
timeConvert: time,
|
||||
voucher_types: voucherTypes,
|
||||
vouchers_popup: false
|
||||
});
|
||||
});
|
||||
app.post('/voucher', [authorization], async (req, res) => {
|
||||
if(typeof req.body === "undefined") {
|
||||
res.status(400).send();
|
||||
return;
|
||||
}
|
||||
|
||||
const typeCheck = (process.env.VOUCHER_TYPES || '480,0,,,;').split(';').includes(req.body['voucher-type']);
|
||||
|
||||
if(!typeCheck) {
|
||||
res.cookie('flashMessage', JSON.stringify({type: 'error', message: 'Unknown Type!'}), {httpOnly: true, expires: new Date(Date.now() + 24 * 60 * 60 * 1000)}).redirect(302, '/voucher');
|
||||
return;
|
||||
}
|
||||
|
||||
// Create voucher code
|
||||
const voucherCode = await unifi(types(req.body['voucher-type'], true)).catch((e) => {
|
||||
res.cookie('flashMessage', JSON.stringify({type: 'error', message: e}), {httpOnly: true, expires: new Date(Date.now() + 24 * 60 * 60 * 1000)}).redirect(302, '/voucher');
|
||||
});
|
||||
|
||||
if(voucherCode) {
|
||||
res.cookie('flashMessage', JSON.stringify({type: 'info', message: `Voucher Created: ${voucherCode}`}), {httpOnly: true, expires: new Date(Date.now() + 24 * 60 * 60 * 1000)}).redirect(302, '/voucher');
|
||||
} else {
|
||||
res.status(501).send();
|
||||
}
|
||||
});
|
||||
app.get('/vouchers', [authorization], async (req, res) => {
|
||||
const hour = new Date().getHours();
|
||||
const timeHeader = hour < 12 ? 'Good Morning' : hour < 18 ? 'Good Afternoon' : 'Good Evening';
|
||||
|
||||
const vouchers = await unifi('', false).catch((e) => {
|
||||
res.cookie('flashMessage', JSON.stringify({type: 'error', message: e}), {httpOnly: true, expires: new Date(Date.now() + 24 * 60 * 60 * 1000)}).redirect(302, '/voucher');
|
||||
// Check if web service is enabled
|
||||
if(webService) {
|
||||
app.get('/login', (req, res) => {
|
||||
// Check if authentication is disabled
|
||||
if (authDisabled) {
|
||||
res.redirect(302, '/voucher');
|
||||
return;
|
||||
}
|
||||
|
||||
const hour = new Date().getHours();
|
||||
const timeHeader = hour < 12 ? 'Good Morning' : hour < 18 ? 'Good Afternoon' : 'Good Evening';
|
||||
|
||||
res.render('login', {
|
||||
error: req.flashMessage.type === 'error',
|
||||
error_text: req.flashMessage.message || '',
|
||||
banner_image: process.env.BANNER_IMAGE || `/images/bg-${random(1, 10)}.jpg`,
|
||||
app_header: timeHeader,
|
||||
sid: req.sid
|
||||
});
|
||||
});
|
||||
app.post('/login', async (req, res) => {
|
||||
if (typeof req.body === "undefined") {
|
||||
res.status(400).send();
|
||||
return;
|
||||
}
|
||||
|
||||
const passwordCheck = req.body.password === (process.env.SECURITY_CODE || "0000");
|
||||
|
||||
if(!passwordCheck) {
|
||||
res.cookie('flashMessage', JSON.stringify({type: 'error', message: 'Password Invalid!'}), {httpOnly: true, expires: new Date(Date.now() + 24 * 60 * 60 * 1000)}).redirect(302, '/login');
|
||||
return;
|
||||
}
|
||||
|
||||
res.cookie('authorization', req.body.password, {httpOnly: true, expires: new Date(Date.now() + 24 * 60 * 60 * 1000)}).redirect(302, '/voucher');
|
||||
});
|
||||
app.get('/voucher', [authorization.web], async (req, res) => {
|
||||
const hour = new Date().getHours();
|
||||
const timeHeader = hour < 12 ? 'Good Morning' : hour < 18 ? 'Good Afternoon' : 'Good Evening';
|
||||
|
||||
if(vouchers) {
|
||||
res.render('voucher', {
|
||||
info: req.flashMessage.type === 'info',
|
||||
info_text: req.flashMessage.message || '',
|
||||
@@ -201,11 +186,110 @@ app.get('/vouchers', [authorization], async (req, res) => {
|
||||
sid: req.sid,
|
||||
timeConvert: time,
|
||||
voucher_types: voucherTypes,
|
||||
vouchers_popup: true,
|
||||
vouchers
|
||||
vouchers_popup: false
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
app.post('/voucher', [authorization.web], async (req, res) => {
|
||||
if (typeof req.body === "undefined") {
|
||||
res.status(400).send();
|
||||
return;
|
||||
}
|
||||
|
||||
const typeCheck = (process.env.VOUCHER_TYPES || '480,0,,,;').split(';').includes(req.body['voucher-type']);
|
||||
|
||||
if(!typeCheck) {
|
||||
res.cookie('flashMessage', JSON.stringify({type: 'error', message: 'Unknown Type!'}), {httpOnly: true, expires: new Date(Date.now() + 24 * 60 * 60 * 1000)}).redirect(302, '/voucher');
|
||||
return;
|
||||
}
|
||||
|
||||
// Create voucher code
|
||||
const voucherCode = await unifi(types(req.body['voucher-type'], true)).catch((e) => {
|
||||
res.cookie('flashMessage', JSON.stringify({type: 'error', message: e}), {httpOnly: true, expires: new Date(Date.now() + 24 * 60 * 60 * 1000)}).redirect(302, '/voucher');
|
||||
});
|
||||
|
||||
if(voucherCode) {
|
||||
res.cookie('flashMessage', JSON.stringify({type: 'info', message: `Voucher Created: ${voucherCode}`}), {httpOnly: true, expires: new Date(Date.now() + 24 * 60 * 60 * 1000)}).redirect(302, '/voucher');
|
||||
}
|
||||
});
|
||||
app.get('/vouchers', [authorization.web], async (req, res) => {
|
||||
const hour = new Date().getHours();
|
||||
const timeHeader = hour < 12 ? 'Good Morning' : hour < 18 ? 'Good Afternoon' : 'Good Evening';
|
||||
|
||||
const vouchers = await unifi('', false).catch((e) => {
|
||||
res.cookie('flashMessage', JSON.stringify({type: 'error', message: e}), {httpOnly: true, expires: new Date(Date.now() + 24 * 60 * 60 * 1000)}).redirect(302, '/voucher');
|
||||
});
|
||||
|
||||
if (vouchers) {
|
||||
res.render('voucher', {
|
||||
info: req.flashMessage.type === 'info',
|
||||
info_text: req.flashMessage.message || '',
|
||||
error: req.flashMessage.type === 'error',
|
||||
error_text: req.flashMessage.message || '',
|
||||
banner_image: process.env.BANNER_IMAGE || `/images/bg-${random(1, 10)}.jpg`,
|
||||
app_header: timeHeader,
|
||||
sid: req.sid,
|
||||
timeConvert: time,
|
||||
voucher_types: voucherTypes,
|
||||
vouchers_popup: true,
|
||||
vouchers
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if(apiService) {
|
||||
app.get('/api', (req, res) => {
|
||||
res.json({
|
||||
error: null,
|
||||
data: {
|
||||
message: 'OK',
|
||||
endpoints: [
|
||||
'/api',
|
||||
'/api/types',
|
||||
'/api/voucher/:type'
|
||||
]
|
||||
}
|
||||
});
|
||||
});
|
||||
app.get('/api/types', (req, res) => {
|
||||
res.json({
|
||||
error: null,
|
||||
data: {
|
||||
message: 'OK',
|
||||
types: voucherTypes
|
||||
}
|
||||
});
|
||||
});
|
||||
app.get('/api/voucher/:type', [authorization.api], async (req, res) => {
|
||||
const typeCheck = (process.env.VOUCHER_TYPES || '480,0,,,;').split(';').includes(req.params.type);
|
||||
|
||||
if(!typeCheck) {
|
||||
res.json({
|
||||
error: 'Unknown Type!',
|
||||
data: {}
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// Create voucher code
|
||||
const voucherCode = await unifi(types(req.params.type, true)).catch((e) => {
|
||||
res.json({
|
||||
error: e,
|
||||
data: {}
|
||||
});
|
||||
});
|
||||
|
||||
if(voucherCode) {
|
||||
res.json({
|
||||
error: null,
|
||||
data: {
|
||||
message: 'OK',
|
||||
voucher: voucherCode
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup default 404 message
|
||||
|
||||
Reference in New Issue
Block a user