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