git ssb

2+

ev / mvd



Tree: 33e3d7cfd2ea4ebdc8336656309afe5c2be5d035

Files: 33e3d7cfd2ea4ebdc8336656309afe5c2be5d035 / bin.js

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

Built with git-ssb-web