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