Skip to content

Instantly share code, notes, and snippets.

View catarak's full-sized avatar
🌈
it's a unix system, i know this

Cassie Tarakajian catarak

🌈
it's a unix system, i know this
View GitHub Profile
var seneca = require('seneca')();
seneca.use('./todo.js');
seneca.act({role:'todo', cmd:'create', name:'Buy Milk'}, function(err, todo) {
console.log(todo);
}
var seneca = require('seneca')();
seneca.use('./todo.js');
seneca.listen({ type: 'tcp', pin: 'role:todo' }); // default port is 10101
var seneca = require('seneca')();
// seneca.use('./todo.js');
seneca.client({ type: 'tcp', pin: 'role:todo' });
seneca.act({role:'todo', cmd:'add', name:'Buy Milk'}, function(err, todo) {
console.log(todo);
}
var SenecaWeb = require('seneca-web')
var express = require('express');
var Router = express.Router;
var context = new Router();
var senecaWebConfig = {
context: context,
adapter: require('seneca-web-adapter-express'),
options: { parseBody: false } // if you want to use body parser, put this in
};
@catarak
catarak / api.js
Last active April 25, 2017 02:44
module.exports = function api(options) {
var validActions = { add:'add' }
this.add('role:api,path:todo', function (msg, respond) {
var action = msg.args.params.action;
var todoText = msg.args.body.text;
this.act('role:todo', {
cmd: validActions[action], // do this to prevent injection attacks
text: text,
const io = require('socket.io-client');
/**
* Forwards updates from a subclass of {@link UpdateEmitter} to a websocket
* @global
* @param {string} name - Name string to be send with each {@link UpdateEmitter#status} event
* @param {number} port - Websocket port to which to connect and forward events
* @param {UpdateEmitter} updateEmitter - Concrete subclass of {@link UpdateEmitter}
*/
class WebsocketUpdater {
const EventEmitter = require('events');
/**
* Subclass to add `started`, `progress` and `completed` functions. Then, just implement
* the `status` getter that returns an object describing the status of your object. Long
* running objects like {@link TMSExporter} subclass this class to send updates as they run.
* @interface
* @global
*/
class UpdateEmitter extends EventEmitter {
// An example of one of our microservices - handles importing to Elasticsearch
class ESCollection extends UpdateEmitter {
get status() {
// should return a JSON object which is passed along with the websocket event
}
syncESWithCSV(csvName) {
this.started(); // emits 'started' event to any front-end listening for it
// do stuff to load CSV to Elasticsearch
this.progress(); // emits 'progress' event with the status
// Partial Express.js API for accessing microservices from front end dashboard
// ...
app.get('/api/tmstocsv/info', (req, res) => {
seneca.act('role:tmstocsv,cmd:info').then((info) => {
res.json(info);
});
});
app.get('/api/tmstocsv/run', (req, res) => {
seneca.act('role:tmstocsv,cmd:run').then(() => {
// CodeMirror, copyright (c) by Marijn Haverbeke and others
// Distributed under an MIT license: http://codemirror.net/LICENSE
// Define search commands. Depends on dialog.js or another
// implementation of the openDialog method.
// Replace works a little oddly -- it will do the replace on the next
// Ctrl-G (or whatever is bound to findNext) press. You prevent a
// replace by making sure the match is no longer selected when hitting
// Ctrl-G.