git ssb

1+

punkmonk.termux / mvd



forked from ev / mvd

Tree: 653e48dd0083bbc962d029960824b4212f96840f

Files: 653e48dd0083bbc962d029960824b4212f96840f / bin.js

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

Built with git-ssb-web