1 | import express from "express";
|
2 | import bodyParser from "body-parser";
|
3 | import Promise from "bluebird";
|
4 | import gatewayApi from "./gateway-api";
|
5 | import gatewayAppRouter from "./gateway-app-router";
|
6 | import { DEFAULT_GATEWAY_PORT } from "./const";
|
7 |
|
8 | let server;
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | const start = (apps, options = {}) => {
|
20 | return new Promise((resolve, reject) => {
|
21 | Promise.coroutine(function*() {
|
22 | const { manifest } = options;
|
23 | const middleware = express().use(bodyParser.json());
|
24 |
|
25 |
|
26 | let apiRoute;
|
27 | if (manifest) {
|
28 | if (!manifest.current) { yield manifest.get().catch(err => reject(err)); }
|
29 | apiRoute = manifest.current && manifest.current.api && manifest.current.api.route;
|
30 | }
|
31 |
|
32 |
|
33 | if (server) {
|
34 | reject(new Error("The gateway server is already running."));
|
35 | }
|
36 |
|
37 |
|
38 | if (apiRoute) { gatewayApi(apiRoute, apps, middleware, manifest); }
|
39 | gatewayAppRouter(apps, middleware);
|
40 |
|
41 |
|
42 | const port = options.port || DEFAULT_GATEWAY_PORT;
|
43 | server = middleware.listen(port, () => {
|
44 | resolve({ port });
|
45 | });
|
46 | })();
|
47 | });
|
48 | };
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | const stop = () => {
|
56 | if (server) {
|
57 | server.close();
|
58 | }
|
59 | server = undefined;
|
60 | };
|
61 |
|
62 |
|
63 |
|
64 | export default {
|
65 | start,
|
66 | stop,
|
67 | isRunning: () => server !== undefined
|
68 | };
|