UNPKG

2.42 kBJavaScriptView Raw
1/**
2* Example service entry point.
3*
4* The purpose of these entry points is to map
5* and configure all the services that the service
6* will need to connect to.
7*
8* You give the services names, and waif keeps track
9* of what the connection details you give it.
10*
11* Each service may have zero or more of these
12* configuration files, and may point to remote
13* services or load up it's own copies.
14*
15* This service declares :
16*
17* * Example service itself, listening on port 3000
18* * Gallery service, mounted at /gallery on the example service.
19* * Event service, hosted on a remote HTTP server.
20* * MongoDB store service, hosted on a local socket.
21*
22* For configuration :
23*
24* It uses variables that are populated by env vars that
25* default to hardcoded strings.
26*/
27
28// Return a new instance of Waif.
29var waif = require('waif')();
30var redirect = require('waif/redirect');
31
32// Configuration.
33var PORT = process.env.PORT || 3000;
34var SOCKET = process.env.SOCKET || '/tmp/sockets/test-data.sock';
35var MONGO_URL = process.env.MONGO_URL || "mongodb://localhost:27017/test";
36var EVENT_URL = process.env.EVENT_URL || 'http://event.example.com';
37
38var mongoStore = require('waif-mongo-store');
39var mongoConfig = { url: MONGO_URL };
40
41// Declaring internally used service.
42waif('store') // data access service.
43 .use(mongoStore(mongoConfig)) // uses mongo as a data store.
44 .listen(); // random socket file.
45
46// Declaring an external REST api.
47waif('event') // event logging service.
48 .use(redirect(EVENT_URL)); // all requests directed here.
49
50// Declaring a service to be mounted on another one.
51// From a relative submodule, with it's own views.
52// Note: the absence of forward() or listen()
53waif('gallery') // photo gallery service.
54 .use(require('./gallery')); // relative submodule.
55
56
57// Example application service
58// Note how we .use() the waif('sandbox') service.
59waif('example') // the example service itself.
60 .use('/gallery', waif('sandbox')) // mount the sandbox service.
61 .use(require('./src/example')) // mount the service' own app.
62 .listen(PORT); // listen on a specific port.
63
64// Start up the servers.
65// Has a companion stop method which closes them all.
66waif.start();
67