SHARE
TWEET

Untitled

a guest Nov 19th, 2019 94 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const express = require('express');
  2. const app = express();
  3.  
  4. const fs = require('fs');
  5. const hbs = require('hbs');
  6. const path = require('path')
  7. const bodyParser = require('body-parser');
  8. const morganBody = require('morgan-body');
  9.  
  10.  
  11. app.use(bodyParser.json());
  12. app.set('view engine', 'html');
  13. app.set('views', path.join(__dirname, 'views'));
  14. app.use('/static', express.static(path.join(__dirname, 'static')));
  15. app.engine('html', require('hbs').__express);
  16. morganBody(app);
  17.  
  18. const session = require('express-session');
  19. const randomize = require('randomatic');
  20.  
  21. app.use(session({
  22.     name: 'session',
  23.     secret: randomize('aA0', 69),
  24.     resave: false,
  25.     saveUninitialized: false,
  26.     cookie: {secure: true}
  27. }));
  28.  
  29. app.use(function(req, res, next) {
  30.     if (req.session.archive === undefined) {
  31.         req.session.archive = {
  32.             'dumb': 'yes',
  33.           // formula: '[0.20 * a + 0.25 * e + 0.25 * p]', // depends on grade
  34.           // assignment: '',
  35.           // exam: '',
  36.           // paper: '',
  37.         };
  38.     }
  39.     next();
  40. });
  41.  
  42. app.get('/debug', (req, res) => {
  43.     res.header('Content-Type', 'text/plain');
  44.     return res.end(fs.readFileSync(__filename).toString());
  45. });
  46.  
  47. app.get('/log', (req, res) => {
  48.     res.header('Content-Type', 'text/plain');
  49.     return res.end(fs.readFileSync(path.join(__dirname, '.log')).toString());
  50. });
  51.  
  52. app.get('/', (req, res) => {
  53.     return res.render('index');
  54. });
  55.  
  56. function deepFreeze(obj) {
  57.     var propNames = Object.getOwnPropertyNames(obj);
  58.     for (let name of propNames) {
  59.         let value = obj[name];
  60.         obj[name] = value && typeof value === 'object' ? deepFreeze(value) : value;
  61.     }
  62.     return Object.freeze(obj);
  63. }
  64.  
  65. function isObject(obj) {
  66.     return obj !== null && typeof obj === 'object';
  67. }
  68.  
  69. function merge(target, source) {
  70.     for (let key in source) {
  71.         if (isObject(target[key]) && isObject(source[key])) {
  72.             merge(target[key], source[key]);
  73.         } else {
  74.             target[key] = source[key];
  75.         }
  76.     }
  77.     return target;
  78. }
  79.  
  80. function clone(target) {
  81.     return merge({}, target);
  82. }
  83.  
  84. const evaluate = require('static-eval');
  85. const parse = require('esprima').parse;
  86.  
  87. app.post('/api/v2/', (req, res) => {
  88. //  var archive = req.session.archive
  89.     deepFreeze(Object);
  90.     var student = clone(req.body);
  91.  
  92.     if (student.name.includes('Baker') || student.name.includes('Purvis')) {
  93.         return res.json({
  94.             'pass': 'n' + randomize('?', 10, {chars: 'o0'}) + 'pe'
  95.         });
  96.     }
  97.  
  98.     if (student.paper !== undefined) {
  99.         let ast = parse(student.formula).body[0].expression;
  100.         let weight = evaluate(ast, {
  101.             a: student.assignment,
  102.             e: student.exam,
  103.             p: student.paper
  104.         });
  105.  
  106.         return res.json({
  107.             'pass': parseFloat(10.5) <= parseFloat(weight) ? 'yes' : 'no'
  108.         });
  109.     }
  110.     return res.end();
  111. });
  112. app.listen(1337);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top