Files: fd958f9e5ebdd7619db1724eadaabc5ff27aa4b4 / bin.js
3235 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('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