Giter Club home page Giter Club logo

hapi-hemera's Introduction

hapi-hemera

Build Status NPM Downloads

js-standard-style

  • Status: In development

hapi-hemera is a Hemera micro-services plugin for hapi. The plugin integrates the Hemera functionality into hapi and provide tools to map its actions to server methods and views for easy access.

Usage

Plugin Registration

const server = new Hapi.Server()
server.connection()
server.register({
  register: require('hapi-hemera'),
  options: {
    hemera:{
      name: 'test',
      load: {
        sampleInterval: 1000
      },
      logLevel: 'info'
    },
    nats: 'nats://localhost:4242',
    // If you want to add hemera plugins
    plugins: [
      require('hemera-joi'),
      require('hemera-stats')
    ],
    // In case you want to use hapi server methods
    methods: {
      add: {
        pattern: {
          topic: 'math',
          cmd: 'add',
          timeout$: 5000,
        }
        // Optional caching parameters 
        cache: {
          expiresIn: 60000,
          staleIn: 30000,
          staleTimeout: 10000,
          generateTimeout: 100
        }
      }
    }
  }
})

Use reply interface

server.route({
  method: 'POST',
  path: '/add',
  handler: function (request, reply) {

    return reply.act({ topic: 'math', cmd: 'add', a: 2, b: 2 })
  }
}

Use hemera instance

server.route({
  method: 'POST',
  path: '/add',
  handler: function (request, reply) {

    return reply.hemera.act({ topic: 'math', cmd: 'add', a: 2, b: 2 },
      function(err, result) {

        reply(err || result)
      })
  }
}

Use server methods

server.route({
  method: 'POST',
  path: '/add',
  handler: function (request, reply) {

    server.methods.add({ a: 1, b: 2 }, (err, resp) => {
      
      reply(err || result)
    }) 
  }
}

Use action mapping

// as string
server.action('generate', 'topic:generator,cmd:id')
// or as object
server.action('generate', {
  topic: 'generator',
  cmd: 'id'
})
// or with cache
server.action('generate', 'topic:generator,cmd:id', {
  cache: {
    expiresIn: 1000,
    generateTimeout: 3000
  }
})
// call
server.methods.generate((err, result) => {})

Use server handlers

Use body

server.route({
  method: 'POST',
  path: '/foo/{topic}/{cmd}',
  handler: {
    act: {
      pattern: {
        timeout$: 5000
      }
    }
  }
})
 curl -H "Content-Type: application/json" -X POST -d '{"a":2,"b":2}' http://localhost:3000/foo/math/add

use query parameters

server.route({
  method: 'POST',
  path: '/math/{cmd}',
  config: {
    validate: {
      query: {
        a: Joi.number().required(),
        b: Joi.number().required()
      }
    }
  },
  handler: {
    act: {
      pattern: {
        topic: 'math',
        timeout$: 5000
      }
    }
  }
})
 curl http://localhost:3000/foo/math/add?a=2&b=2

hapi-hemera's People

Contributors

aruss avatar imatefx avatar starptech avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.