Files: a8221d7410bb00bbd9d418fea1249dc77aa1f981 / bin.js
3272 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('ssb-query')) |
40 | .use(require('./mvd-indexes')) |
41 | .use(require('ssb-links')) |
42 | .use(require('ssb-ebt')) |
43 | .use(require('ssb-search')) |
44 | .use(require('scuttlebot/plugins/invite')) |
45 | .use(require('scuttlebot/plugins/local')) |
46 | .use(require('decent-ws')) |
47 | .use({ |
48 | name: 'serve', |
49 | version: '1.0.0', |
50 | init: function (sbot) { |
51 | sbot.ws.use(function (req, res, next) { |
52 | var send = config |
53 | delete send.keys // very important to keep this, as it removes the server keys from the config before broadcast |
54 | send.address = sbot.ws.getAddress() |
55 | sbot.invite.create({modern: true}, function (err, cb) { |
56 | send.invite = cb |
57 | }) |
58 | if(req.url == '/') |
59 | res.end(mvdClient) |
60 | if(req.url == '/get-config') |
61 | res.end(JSON.stringify(send)) |
62 | else next() |
63 | }) |
64 | } |
65 | }) |
66 | |
67 | open('http://localhost:' + config.ws.port, {wait: false}) |
68 | |
69 | var server = createSbot(config) |
70 | |
71 | fs.writeFileSync(manifestFile, JSON.stringify(server.getManifest(), null, 2)) |
72 | } else { |
73 | |
74 | var manifest |
75 | try { |
76 | manifest = JSON.parse(fs.readFileSync(manifestFile)) |
77 | } catch (err) { |
78 | throw explain(err, |
79 | 'no manifest file' |
80 | + '- should be generated first time server is run' |
81 | ) |
82 | } |
83 | |
84 | // connect |
85 | require('ssb-client')(config.keys, { |
86 | manifest: manifest, |
87 | port: config.port, |
88 | host: config.host||'localhost', |
89 | caps: config.caps, |
90 | key: config.key || config.keys.id |
91 | }, function (err, rpc) { |
92 | if(err) { |
93 | if (/could not connect/.test(err.message)) { |
94 | console.log('Error: Could not connect to the scuttlebot server.') |
95 | console.log('Use the "server" command to start it.') |
96 | if(config.verbose) throw err |
97 | process.exit(1) |
98 | } |
99 | throw err |
100 | } |
101 | |
102 | // add some extra commands |
103 | manifest.version = 'async' |
104 | manifest.config = 'sync' |
105 | rpc.version = function (cb) { |
106 | console.log(require('./package.json').version) |
107 | cb() |
108 | } |
109 | rpc.config = function (cb) { |
110 | console.log(JSON.stringify(config, null, 2)) |
111 | cb() |
112 | } |
113 | |
114 | // run commandline flow |
115 | muxrpcli(argv, manifest, rpc, config.verbose) |
116 | }) |
117 | } |
118 | |
119 |
Built with git-ssb-web