git ssb

1+

punkmonk.termux / mvd



forked from ev / mvd

Tree: af3e4febd3c1489a35bda14c98a8575ea624615b

Files: af3e4febd3c1489a35bda14c98a8575ea624615b / bin.js

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

Built with git-ssb-web