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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. }
  12. const validators = {}
  13. const responseSchemas = {
  14. health: healthSchema.stats,
  15. error: errorSchema.single
  16. }
  17. module.exports = {
  18. method: 'GET',
  19. path: '/',
  20. options:{
  21. ...pluginConfig.docs,
  22. tags: ['api'],
  23. auth: false,
  24. cors: true,
  25. handler: async function (request, h) {
  26. const { healthService } = request.server.services()
  27. const stats = await healthService.getStats()
  28. try {
  29. return h.response(({
  30. ok:true,
  31. handler: pluginConfig.handlerType,
  32. data: stats
  33. })).code(200)
  34. } catch (err) {
  35. return h
  36. .response({
  37. ok: false,
  38. handler: pluginConfig.handlerType,
  39. data: {error: `${err}`}
  40. })
  41. .code(409)
  42. }
  43. },
  44. validate: {
  45. ...validators,
  46. failAction: 'log'
  47. },
  48. response: {
  49. status: {
  50. 200: apiSchema.single
  51. .append({
  52. data: responseSchemas.health,
  53. })
  54. .label('api_single_res'),
  55. 409: apiSchema.single
  56. .append({
  57. data: responseSchemas.error,
  58. })
  59. .label('error_single_res'),
  60. },
  61. },
  62. },
  63. }