Skip to content

Instantly share code, notes, and snippets.

@cjus
Last active December 15, 2016 15:25
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cjus/f42fb97e0027a15efb51e4db152f0221 to your computer and use it in GitHub Desktop.
Save cjus/f42fb97e0027a15efb51e4db152f0221 to your computer and use it in GitHub Desktop.
Hydra workshop files

Hydra Workshop Files

Hydra is an open source project which provides a set of tools that faciliate building distributed applications such as microservices.

These source files are part of the Hydra Workshop, an introduction to Hydra presented at Node and JavaScript meetups. The presentation slides can be found at: https://speakerdeck.com/cjus/building-microservices-using-hydra

Find out more about Hydra at: https://github.com/flywheelsports

You can share this gist using: https://goo.gl/AVW0lG

Note when using config.json below make sure to use redis.url and red.port for your own redis server.

{
"hydra": {
"serviceName": "hello",
"serviceIP": "",
"servicePort": 3000,
"serviceType": "",
"serviceDescription": "",
"redis": {
"url": "172.16.0.2",
"port": 6379,
"db": 15
}
}
}
var express = require('express')
var app = express()
app.get('/', function (req, res) {
res.send('Hello World!')
})
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})
var hydraExpress = require('fwsp-hydra-express');
var hydra = hydraExpress.getHydra();
var config = require('./config.json');
function onRegisterRoutes() {
var express = hydraExpress.getExpress();
var api = express.Router();
api.get('/', function(req, res) {
res.send({
msg: `hello from ${hydra.getServiceName()} - ${hydra.getInstanceID()}`
});
});
hydraExpress.registerRoutes({
'': api
});
}
hydraExpress.init(config, onRegisterRoutes);
var hydraExpress = require('fwsp-hydra-express');
var hydra = hydraExpress.getHydra();
var config = require('./config.json');
function onRegisterRoutes() {
var express = hydraExpress.getExpress();
var api = express.Router();
api.get('/hello', function(req, res) {
res.send({
msg: `hello from ${hydra.getServiceName()} - ${hydra.getInstanceID()}`
});
});
hydraExpress.registerRoutes({
'/v1': api
});
}
hydraExpress.init(config, onRegisterRoutes);
var hydraExpress = require('fwsp-hydra-express');
var config = require('./config.json');
function onRegisterRoutes() {
var express = hydraExpress.getExpress();
var api = express.Router();
api.get('/', function(req, res) {
res.send('Hello World!');
});
hydraExpress.registerRoutes({
'': api
});
}
hydraExpress.init(config, onRegisterRoutes);
/**
* @name Red
* @summary Red Hydra Express service entry point
* @description
*/
'use strict';
const version = require('./package.json').version;
const hydraExpress = require('fwsp-hydra-express');
let config = require('fwsp-config');
/**
* Load configuration file and initialize hydraExpress app.
*/
config.init('./config/config.json')
.then(() => {
config.version = version;
hydraExpress.init(config.getObject(), version, () => {
hydraExpress.registerRoutes({
'/v1/red': require('./routes/red-v1-routes')
});
})
.then((serviceInfo) => {
console.log('serviceInfo', serviceInfo);
let hydra = hydraExpress.getHydra();
let message = hydra.createUMFMessage({
to: 'blue-service:[get]/v1/blue/',
from: 'red-service:/',
body: {}
});
hydra.makeAPIRequest(message)
.then((response) => {
console.log('response', response);
});
return 0;
})
.catch(err => console.log('err', err));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment