create-server.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /**
  2. * Currently commenting out redis caching as there is no
  3. * working implementation for the benchmark suite.
  4. */
  5. const Hapi = require('hapi');
  6. const Vision = require('vision');
  7. const options = {
  8. connections: {
  9. compression: false
  10. }
  11. };
  12. const server = new Hapi.Server(options);
  13. server.connection({port: process.env.PORT || 8080, host: '0.0.0.0'});
  14. server.register(Vision, (err) => {
  15. if (err) {
  16. throw err;
  17. }
  18. server.views({
  19. engines: { html: require('handlebars') },
  20. path: __dirname + '/views/'
  21. });
  22. });
  23. const Handler = require(`./handlers/${process.env.NODE_HANDLER}`);
  24. // Makes routing simpler as tfb routes are all GET's
  25. // We also don't use the nifty route features that Hapi has
  26. // to offer such as attaching a validator
  27. const Route = (path, handler) =>
  28. server.route({ method: 'GET', path, handler });
  29. const JsonSerialization = (req, reply) =>
  30. reply({ message: 'Hello, World!' }).header('Server', 'hapi');
  31. const Plaintext = (req, reply) =>
  32. reply('Hello, World!')
  33. .header('Server', 'hapi')
  34. .header('Content-Type', 'text/plain');
  35. Route('/json', JsonSerialization);
  36. Route('/plaintext', Plaintext);
  37. Route('/db', Handler.SingleQuery);
  38. Route('/queries', Handler.MultipleQueries);
  39. Route('/fortunes', Handler.Fortunes);
  40. Route('/updates', Handler.Updates);
  41. server.start((err) =>
  42. console.log('Hapi worker started and listening on ' + server.info.uri + " "
  43. + new Date().toISOString(" ")));