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