git ssb

2+

dinoworm ๐Ÿ› / ssb-graphviz



Tree: ca718f616a051e93adeb22faa3fc06646f667973

Files: ca718f616a051e93adeb22faa3fc06646f667973 / index.js

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

Built with git-ssb-web