create-server.js 1.5 KB

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