Files: e8b919558c93da12334cc6e605de9d1828ddb297 / index.js
1380 bytesRaw
1 | const http = require('http') |
2 | const defaultsDeep = require('lodash/defaultsDeep') |
3 | |
4 | const defaultVizConfig = require('./config') |
5 | const Api = require('./api') |
6 | |
7 | var _server |
8 | |
9 | module.exports = { |
10 | name: 'ssb-graphviz', |
11 | version: require('./package.json').version, |
12 | manifest: {}, |
13 | init: function (ssb, config, reconnect) { |
14 | // close existing server. when scuttlebot plugins get a deinit method, we |
15 | // will close it in that instead it |
16 | if (_server) _server.close() |
17 | |
18 | _server = Server(ssb, config, reconnect) |
19 | _server.listen() |
20 | |
21 | return {} |
22 | } |
23 | } |
24 | |
25 | function Server (ssb, config, reconnect) { |
26 | const vizConfig = defaultsDeep(config['ssb-graphviz'], defaultVizConfig) |
27 | const { host, port } = parseAddr(config.listenAddr, { |
28 | host: vizConfig.host, |
29 | port: vizConfig.port |
30 | }) |
31 | |
32 | var server = http.createServer(Api(ssb, config)) |
33 | |
34 | return { |
35 | listen, |
36 | close |
37 | } |
38 | |
39 | function listen () { |
40 | server.listen(port, host, function () { |
41 | var hostName = ~host.indexOf(':') ? '[' + host + ']' : host |
42 | console.log(`Listening on http://${hostName}:${port}/`) |
43 | }) |
44 | } |
45 | |
46 | function close () { |
47 | server.close() |
48 | } |
49 | } |
50 | |
51 | function parseAddr (str, def) { |
52 | if (!str) return def |
53 | var i = str.lastIndexOf(':') |
54 | if (~i) return {host: str.substr(0, i), port: str.substr(i + 1)} |
55 | if (isNaN(str)) return {host: str, port: def.port} |
56 | return {host: def.host, port: str} |
57 | } |
58 |
Built with git-ssb-web