git ssb

2+

ev / mvd



Tree: 76e471a1d7f948aaea0a1f022cc8842d66dff10e

Files: 76e471a1d7f948aaea0a1f022cc8842d66dff10e / bin.js

3235 bytesRaw
1var fs = require('fs')
2var path = require('path')
3var ssbKeys = require('ssb-keys')
4var stringify = require('pull-stringify')
5var open = require('opn')
6var home = require('os-homedir')()
7var nonPrivate = require('non-private-ip')
8var muxrpcli = require('muxrpcli')
9
10var SEC = 1e3
11var MIN = 60*SEC
12
13var network = 'ssb'
14//var network = 'decent'
15//var network = 'testnet'
16
17var config = require('./config/inject')(network)
18
19config.keys = ssbKeys.loadOrCreateSync(path.join(config.path, 'secret'))
20
21var mvdClient = fs.readFileSync(path.join('./build/index.html'))
22
23var manifestFile = path.join(config.path, 'manifest.json')
24
25var argv = process.argv.slice(2)
26var i = argv.indexOf('--')
27var conf = argv.slice(i+1)
28argv = ~i ? argv.slice(0, i) : argv
29
30if (argv[0] == 'server') {
31
32 var createSbot = require('scuttlebot')
33 .use(require('scuttlebot/plugins/master'))
34 .use(require('scuttlebot/plugins/gossip'))
35 .use(require('scuttlebot/plugins/replicate'))
36 .use(require('ssb-friends'))
37 .use(require('ssb-blobs'))
38 .use(require('ssb-backlinks'))
39 .use(require('./query'))
40 .use(require('ssb-links'))
41 .use(require('ssb-ebt'))
42 .use(require('ssb-search'))
43 .use(require('scuttlebot/plugins/invite'))
44 .use(require('scuttlebot/plugins/local'))
45 .use(require('decent-ws'))
46 .use({
47 name: 'serve',
48 version: '1.0.0',
49 init: function (sbot) {
50 sbot.ws.use(function (req, res, next) {
51 var send = config
52 delete send.keys // very important to keep this, as it removes the server keys from the config before broadcast
53 send.address = sbot.ws.getAddress()
54 sbot.invite.create({modern: true}, function (err, cb) {
55 send.invite = cb
56 })
57 if(req.url == '/')
58 res.end(mvdClient)
59 if(req.url == '/get-config')
60 res.end(JSON.stringify(send))
61 else next()
62 })
63 }
64 })
65
66 open('http://localhost:' + config.ws.port, {wait: false})
67
68 var server = createSbot(config)
69
70 fs.writeFileSync(manifestFile, JSON.stringify(server.getManifest(), null, 2))
71} else {
72
73 var manifest
74 try {
75 manifest = JSON.parse(fs.readFileSync(manifestFile))
76 } catch (err) {
77 throw explain(err,
78 'no manifest file'
79 + '- should be generated first time server is run'
80 )
81 }
82
83 // connect
84 require('ssb-client')(config.keys, {
85 manifest: manifest,
86 port: config.port,
87 host: config.host||'localhost',
88 caps: config.caps,
89 key: config.key || config.keys.id
90 }, function (err, rpc) {
91 if(err) {
92 if (/could not connect/.test(err.message)) {
93 console.log('Error: Could not connect to the scuttlebot server.')
94 console.log('Use the "server" command to start it.')
95 if(config.verbose) throw err
96 process.exit(1)
97 }
98 throw err
99 }
100
101 // add some extra commands
102 manifest.version = 'async'
103 manifest.config = 'sync'
104 rpc.version = function (cb) {
105 console.log(require('./package.json').version)
106 cb()
107 }
108 rpc.config = function (cb) {
109 console.log(JSON.stringify(config, null, 2))
110 cb()
111 }
112
113 // run commandline flow
114 muxrpcli(argv, manifest, rpc, config.verbose)
115 })
116}
117
118

Built with git-ssb-web