UNPKG

2.58 kBMarkdownView Raw
1## Waif
2
3Waif provides the smallest possible abstraction that would allow you to
4to write an express-based microservice that can be used as a local library
5or a remote instance.
6
7
8### How it works
9
10Waif is a thin wrapper around express.js when we declare our
11services, and a thin wrapper around request when we call them.
12
13It splits the declaration and implementation of the services into separate files.
14
15Doing this allows you the greatest amount of flexibility and scaleability,
16since you can run the service in many environments.
17
18### Service Entry Point Example
19
20Each microservice can be used and re-used in many different contexts,
21therefor there is a separate step where you configure all the services
22you will use, and where they will be accessed.
23
24```javascript
25// file: production.js
26
27// get an instance of waif
28var waif = require('waif')();
29
30// wrap standard middleware
31var wrap = require('waif/wrap');
32
33// maps to external service, on a random local port.
34waif('user')
35 .pipe('/user/:id', 'http://user.example.com/v3/:id')
36 .listen(0);
37
38var service = require('./src/my-service');
39wait('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
52module.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
81Each repository may contain zero or more of
82these entry points, which may represent one
83or more environments.
84
85These may map services differently depending
86on how you need to access them.
87
88How the configuration gets into Waif is entirely
89up to the developer. It just provides the
90API to declare them, in a way that the services
91don'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).__