<commit_before>'use strict';

let co = require('co');
let util = require('util');

// chassis

// events
let Events = require('../lib/transport/events/events').Events;
let Kafka = require('../lib/transport/events/kafka').Kafka;

let logger = {
  log: console.log,
};
let options = {
  logger: logger,
  kafka: {
    groupId: 'restore-chassis-example-email',
    clientId: 'restore-chassis-example-email',
    connectionString: 'localhost:9092',
    logger: {
      logFunction: logger.log,
    },
  },
  timeout: 10000,
};

function sendEmail(mail) {
  logger.log('debug', 'pretending to send email', mail);
}

function* init(options) {
  // Events
  // Create a Kafka provider
  let kafka = new Kafka(options.kafka);
  // Use Kafka provider for events
  let events = new Events(kafka);
  // subscribe to user events provided by kafka (topic:user)
  let userEvents = yield events.subscribe('user');
  // listen to user.created events (kafka messages)
  userEvents.on('created', function*(message){
    let name = message.name || message.id;
    let msg = util.format('Hello user %s Your account has beeen created.', name);
    let email = {
      to: message.email,
      body: msg,
    };
    sendEmail(email);
  });
  userEvents.on('activated', function*(message){
    let name = message.name || message.id;
    let msg = util.format('Hello user %s Your account has beeen activated.', name);
    let email = {
      to: message.email,
      body: msg,
    };
    sendEmail(email);
  });
  userEvents.on('deleted', function*(message){
    let name = message.name || message.id;
    let msg = util.format('Hello user %s Your account has beeen deleted.', name);
    let email = {
      to: message.email,
      body: msg,
    };
    sendEmail(email);
  });
  // start provider kafka (consumer, producer)
  yield kafka.start();

  process.on('SIGINT', function() {
    logger.log('signal', 'SIGINT');
    co(function*(){
      yield kafka.end();
      process.exit(0);
    }).catch(function(err){
      logger.log('error', err);
      process.exit(1);
    });
  });
}

co(function*(){
  let ms = yield init(options);
}).catch(function(err){
  logger.log('error', 'microservice', 'message', err);
  process.exit(1);
});
<commit_msg>Convert email example to new microservice Server chassis<commit_after>'use strict';

let co = require('co');
let util = require('util');
let Server = require('../lib/microservice').Server

function Service(userEvents, logger) {
  function* sendEmail(mail) {
    logger.log('debug', 'pretending to send email', mail);
  }
  userEvents.on('created', function*(message){
    let name = message.name || message.id;
    let msg = util.format('Hello user %s Your account has beeen created.', name);
    let email = {
      to: message.email,
      body: msg,
    };
    yield sendEmail(email);
  });
  userEvents.on('activated', function*(message){
    let name = message.name || message.id;
    let msg = util.format('Hello user %s Your account has beeen activated.', name);
    let email = {
      to: message.email,
      body: msg,
    };
    yield sendEmail(email);
  });
  userEvents.on('deleted', function*(message){
    let name = message.name || message.id;
    let msg = util.format('Hello user %s Your account has beeen deleted.', name);
    let email = {
      to: message.email,
      body: msg,
    };
    yield sendEmail(email);
  });
}

let config = {
  events: {
    provider: {
      name: 'kafka',
      config: { // config object pased to kafka provider
        groupId: 'restore-chassis-example-email',
        clientId: 'restore-chassis-example-email',
        connectionString: 'localhost:9092',
      },
    },
  }
};

co(function*(){
  // Create a new microservice Server
  let server = new Server(config);

  // Subscribe to events which the business logic requires
  let userEvents = yield server.events.subscribe('user');

  // Create the business logic
  let service = new Service(userEvents, server.logger);

  // Bind business logic to server
  yield server.bind(service);

  // Start server
  yield server.start();
}).catch(function(err) {
  console.log(err.stack);
  process.exit(1);
});

{'fraction_non_alphanumeric': 0.15226833976833976, 'fraction_numerical': 0.004102316602316602, 'mean_word_length': 3.1284860557768925, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6564265', 'n_tokens_mistral': 1292, 'n_tokens_neox': 1263, 'n_words': 352}