git ssb

2+

ev / mvd



Tree: e03e2bdee5572f15fe3434f5b1e0d456f6bd0f76

Files: e03e2bdee5572f15fe3434f5b1e0d456f6bd0f76 / bin.js

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

Built with git-ssb-web