1 | __THIS LIBRARY IS IN THE FIRST STAGES OF DEVELOPMENT.
|
2 | DO NOT EXPECT IT TO WORK AT ALL YET.__
|
3 |
|
4 | ## Waif
|
5 |
|
6 | Waif provides the smallest possible abstraction that would allow you to
|
7 | to write an express-based microservice that can be used as a local library
|
8 | or a remote instance.
|
9 |
|
10 | __Waif is developed by [Wayfinder](http://wayfinder.co) and is (almost) being
|
11 | used in production where we deploy our services using [Longshoreman](http://longshoreman.io).__
|
12 |
|
13 | ### How it works
|
14 |
|
15 | Waif is a thin wrapper around express.js when we declare our
|
16 | services, and a thin wrapper around request when we call them.
|
17 |
|
18 | It splits the declaration and implementation of the services into separate files.
|
19 |
|
20 | Doing this allows you the greatest amount of flexibility and scaleability,
|
21 | since you can run the service in many environments.
|
22 |
|
23 | ### Service Entry Point Example
|
24 |
|
25 | Each microservice can be used and re-used in many different contexts,
|
26 | therefor there is a separate step where you configure all the services
|
27 | you will use, and where they will be accessed.
|
28 |
|
29 |
|
30 | ```javascript
|
31 | // file: production.js
|
32 |
|
33 | // get an instance of waif
|
34 | var waif = require('waif')();
|
35 |
|
36 | // wrap standard middleware
|
37 | var wrap = require('waif/wrap');
|
38 |
|
39 | // proxy to other addresses
|
40 | var pipe = require('waif/pipe');
|
41 |
|
42 | // return content verbatim
|
43 | var send = require('waif/send');
|
44 |
|
45 | waif('user')
|
46 | .use(pipe, 'http://user.example.com/v3');
|
47 | .listen();
|
48 |
|
49 | wait('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
|
62 | module.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 |
|
91 | Each repository may contain zero or more of
|
92 | these entry points, which may represent one
|
93 | or more environments.
|
94 |
|
95 | These may map services differently depending
|
96 | on how you need to access them.
|
97 |
|
98 | How the configuration gets into Waif is entirely
|
99 | up to the developer. It just provides the
|
100 | API to declare them, in a way that the services
|
101 | don't need to care about it.
|