You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

get.js 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. 'use strict'
  2. const apiSchema = require('../../schemas/api')
  3. const errorSchema = require('../../schemas/errors')
  4. const healthSchema = require('../../schemas/health')
  5. const pluginConfig = {
  6. handlerType: 'health',
  7. docs: {
  8. description: 'Get server stats',
  9. notes: 'Returns stats on server status',
  10. },
  11. opts: {
  12. tags: ['api'],
  13. auth: { strategy: 'default_jwt' },
  14. cors: true,
  15. },
  16. }
  17. const validators = {}
  18. const responseSchemas = {
  19. health: healthSchema.stats,
  20. error: errorSchema.single,
  21. }
  22. module.exports = {
  23. method: 'GET',
  24. path: '/',
  25. options: {
  26. ...pluginConfig.docs,
  27. ...pluginConfig.opts,
  28. handler: async function (request, h) {
  29. const { healthService } = request.server.services()
  30. const stats = await healthService.getStats()
  31. try {
  32. return h
  33. .response({
  34. ok: true,
  35. handler: pluginConfig.handlerType,
  36. data: stats,
  37. })
  38. .code(200)
  39. } catch (err) {
  40. return h
  41. .response({
  42. ok: false,
  43. handler: pluginConfig.handlerType,
  44. data: { error: `${err}` },
  45. })
  46. .code(409)
  47. }
  48. },
  49. validate: {
  50. ...validators,
  51. failAction: 'log',
  52. },
  53. response: {
  54. status: {
  55. 200: apiSchema.single
  56. .append({
  57. data: responseSchemas.health,
  58. })
  59. .label('api_single_res'),
  60. 409: apiSchema.single
  61. .append({
  62. data: responseSchemas.error,
  63. })
  64. .label('error_single_res'),
  65. },
  66. },
  67. },
  68. }