git ssb

1+

mixmix / scuttle-shell



Commit 4a030f061750b955cc3028f04d0cf62248113251

enhancement: support for custom plugins

andre alves garzia authored on 7/13/2018, 2:47:55 PM
Henry committed on 9/4/2018, 4:46:02 PM
Parent: 69094b14d8a0f96b5da1d4ce1008b7ba2ceb94d3

Files changed

examples/launch_sbot_custom_plugin.jsadded
examples/service-discovery.jsadded
server.jschanged
examples/launch_sbot_custom_plugin.jsView
@@ -1,0 +1,7 @@
1 +let scuttleshell = require("../server")
2 +
3 +console.log("Starting sbot, quitting after 30 seconds")
4 +console.log("open http://localhost:8989/get-address")
5 +scuttleshell.start(false, ["./examples/service-discovery"])
6 +
7 +setTimeout(scuttleshell.stop, 30000)
examples/service-discovery.jsView
@@ -1,0 +1,14 @@
1 +module.exports = {
2 + name: 'server-discovery',
3 + version: '1.0.0',
4 + init: function (sbot) {
5 + sbot.ws.use(function (req, res, next) {
6 + res.setHeader('Access-Control-Allow-Origin', '*')
7 + if (req.url === '/get-address') {
8 + res.end(sbot.ws.getAddress())
9 + } else {
10 + next()
11 + }
12 + })
13 + }
14 +}
server.jsView
@@ -7,9 +7,9 @@
77 const notifier = require('node-notifier')
88 const SysTray = require('systray').default
99 let tray = {}
1010
11-function start(appname) {
11 +function start(appname, customPluginPaths) {
1212
1313 let argv = process.argv.slice(2)
1414 let i = argv.indexOf('--')
1515 let conf = argv.slice(i + 1)
@@ -46,8 +46,24 @@
4646 .use(require('ssb-search'))
4747 .use(require('ssb-query'))
4848 .use(require('ssb-ws'))
4949
50 +
51 + // Custom plugins from json
52 + let appManifestFile = path.resolve('scuttleshell.json')
53 + if (fs.existsSync(appManifestFile)) {
54 + let manifest = JSON.parse(fs.readFileSync(manifestPath))
55 + if (manifest.hasOwnProperty('plugins') && Array.isArray(manifest.plugins)) {
56 + console.log("loading custom plugins: ", manifest.plugins.join(", "))
57 + manifest.plugins.forEach(plugin => createSbot.use(require(plugin)))
58 + }
59 + }
60 +
61 + if (Array.isArray(customPluginPaths)) {
62 + console.log("loading custom plugins: ", customPluginPaths.join(", "))
63 + customPluginPaths.forEach(plugin => createSbot.use(require(plugin)))
64 + }
65 +
5066 // start server
5167
5268 config.keys = keys
5369 const server = createSbot(config)
@@ -102,9 +118,21 @@
102118 function stop() {
103119 tray.kill()
104120 }
105121
106-module.exports = { start, stop }
122 +const getConfig = () => {
123 + try {
124 + let secret = fs.readFileSync(pathToSecret, "utf8")
125 + let keys = JSON.parse(secret.replace(/#[^\n]*/g, ''))
126 + let manifest = JSON.parse(fs.readFileSync(path.join(config.path, 'manifest.json')))
127 + let remote = "ws://localhost:8989~shs:" + keys.id.substring(1, keys.id.indexOf('.'))
128 + return { type: 'config', keys: keys, manifest: manifest, remote: remote, secret: secret }
129 + } catch (n) {
130 + return { type: 'error', msg: n.message }
131 + }
132 +}
107133
134 +module.exports = { start, stop, getConfig }
135 +
108136 if (require.main === module) {
109137 var errorLevel = start()
110138 }

Built with git-ssb-web