UNPKG

2.7 kBMarkdownView Raw
1__THIS LIBRARY IS IN THE FIRST STAGES OF DEVELOPMENT.
2DO NOT EXPECT IT TO WORK AT ALL YET.__
3
4## Waif
5
6Waif provides the smallest possible abstraction that would allow you to
7to write an express-based microservice that can be used as a local library
8or a remote instance.
9
10__Waif is developed by [Wayfinder](http://wayfinder.co) and is (almost) being
11used in production where we deploy our services using [Longshoreman](http://longshoreman.io).__
12
13### How it works
14
15Waif is a thin wrapper around express.js when we declare our
16services, and a thin wrapper around request when we call them.
17
18It splits the declaration and implementation of the services into separate files.
19
20Doing this allows you the greatest amount of flexibility and scaleability,
21since you can run the service in many environments.
22
23### Service Entry Point Example
24
25Each microservice can be used and re-used in many different contexts,
26therefor there is a separate step where you configure all the services
27you will use, and where they will be accessed.
28
29
30```javascript
31// file: production.js
32
33// get an instance of waif
34var waif = require('waif')();
35
36// wrap standard middleware
37var wrap = require('waif/wrap');
38
39// proxy to other addresses
40var pipe = require('waif/pipe');
41
42// return content verbatim
43var send = require('waif/send');
44
45waif('user')
46 .use(pipe, 'http://user.example.com/v3');
47 .listen();
48
49wait('my-service')
50 .use('/ping', send, 'pong')
51 .use(wrap, myLogMiddleware)
52 .use(require('./src/my-service'))
53 .listen(3000);
54```
55
56### Service Example
57
58```javascript
59// file: src/my-service.js
60
61// always return a function
62module.exports = function(config) {
63 // you can use anything that is connect-like.
64 var app = require('express')();
65
66 // services are passed these as context
67 var waif = this.waif;
68 var service = this.service;
69
70 // map the defined user service
71 var user = waif('user');
72
73 // set up any routes you want to use
74 app.get('/profile/:userId', function(req, res, next) {
75
76 // the services registered are used just like request.
77 user(req.param.userId, function(err, resp, body) {
78 if (err || resp.statusCode !== 200) { return next(resp.statusCode); }
79
80 res.render('profile', body);
81 });
82 });
83
84 // return your middleware instead of listening.
85 return app;
86};
87```
88
89### About Service Configuration
90
91Each repository may contain zero or more of
92these entry points, which may represent one
93or more environments.
94
95These may map services differently depending
96on how you need to access them.
97
98How the configuration gets into Waif is entirely
99up to the developer. It just provides the
100API to declare them, in a way that the services
101don't need to care about it.