git ssb

1+

punkmonk.termux / mvd



forked from ev / mvd

Tree: 7ede3c7a935ddf6b575e9a503f49956005a87a3d

Files: 7ede3c7a935ddf6b575e9a503f49956005a87a3d / bin.js

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

Built with git-ssb-web