create-server.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. const provision = async () => {
  14. await server.register({
  15. plugin: Vision,
  16. options: {
  17. engines: { html: require('handlebars') },
  18. path: __dirname + '/views/'
  19. }
  20. })
  21. const Handler = require(`./handlers/${process.env.NODE_HANDLER}`);
  22. // Makes routing simpler as tfb routes are all GET's
  23. // We also don't use the nifty route features that Hapi has
  24. // to offer such as attaching a validator
  25. const Route = (path, handler) =>
  26. server.route({ method: 'GET', path, handler });
  27. const JsonSerialization = (request, h) =>
  28. h.response({ message: 'Hello, World!' }).header('Server', 'hapi');
  29. const Plaintext = (request, h) =>
  30. h.response('Hello, World!')
  31. .header('Server', 'hapi')
  32. .header('Content-Type', 'text/plain');
  33. Route('/json', JsonSerialization);
  34. Route('/plaintext', Plaintext);
  35. Route('/db', Handler.SingleQuery);
  36. Route('/queries', Handler.MultipleQueries);
  37. Route('/fortunes', Handler.Fortunes);
  38. Route('/updates', Handler.Updates);
  39. await server.start();
  40. console.log('Hapi worker started and listening on ' + server.info.uri + " "
  41. + new Date().toISOString(" "));
  42. }
  43. provision()