Skip to content

Commit

Permalink
initial work on standalone sbot and custom ports / data directory
Browse files Browse the repository at this point in the history
  • Loading branch information
mmckegg committed Aug 14, 2017
1 parent 81ab2bf commit 8737709
Show file tree
Hide file tree
Showing 8 changed files with 5,033 additions and 248 deletions.
31 changes: 31 additions & 0 deletions assets/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!DOCTYPE html>
<html>
<head></head>
<body>
<script>
// redirect console to main process
var electron = require('electron')
var localLog = console.log
var localError = console.error
var remoteLog = electron.remote.getGlobal('console').log
var remoteError = electron.remote.getGlobal('console').error

console.log = function (...args) {
localLog.apply(console, args)
remoteLog(...args)
}

console.error = function (...args) {
localError.apply(console, args)
remoteError(...args)
}

process.exit = electron.remote.app.quit
// redirect errors to stderr
window.addEventListener('error', function (e) {
e.preventDefault()
console.error(e.error.stack || 'Uncaught ' + e.error)
})
</script>
</body>
</html>
28 changes: 28 additions & 0 deletions background-process.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var fs = require('fs')
var Path = require('path')
var electron = require('electron')

var createSbot = require('scuttlebot')
.use(require('scuttlebot/plugins/master'))
.use(require('scuttlebot/plugins/gossip'))
.use(require('scuttlebot/plugins/replicate'))
.use(require('ssb-friends'))
.use(require('ssb-blobs'))
.use(require('ssb-backlinks'))
.use(require('ssb-private'))
.use(require('scuttlebot/plugins/invite'))
.use(require('scuttlebot/plugins/local'))
.use(require('scuttlebot/plugins/logging'))
.use(require('ssb-query'))
.use(require('ssb-about'))
.use(require('ssb-contacts'))
// .use(require('ssb-ebt'))
.use(require('ssb-ws'))

// pull config options out of depject
var config = require('./config').create().config.sync.load()

var sbot = createSbot(config)
var manifest = sbot.getManifest()
fs.writeFileSync(Path.join(config.path, 'manifest.json'), JSON.stringify(manifest))
electron.ipcRenderer.send('server-started')
13 changes: 10 additions & 3 deletions blob/sync/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@ const nest = require('depnest')

exports.gives = nest('blob.sync.url')

exports.needs = nest({
'config.sync.load': 'first'
})

exports.create = function (api) {
return nest('blob.sync.url', function (id) {
// return id

return 'http://localhost:8989/blobs/get/' + id
var config = api.config.sync.load()
var prefix = config.blobsPrefix != null ? config.blobsPrefix : `http://localhost:${config.ws.port}/blobs/get`
if (id && typeof id.link === 'string') {
id = id.link
}
return `${prefix}/${encodeURIComponent(id)}`
})
}
28 changes: 28 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const Config = require('ssb-config/inject')
const nest = require('depnest')
const ssbKeys = require('ssb-keys')
const Path = require('path')

const appName = 'ticktack-ssb'
const opts = {
port: 43750,
blobsPort: 43751,
ws: {
port: 43751
}
}

exports.gives = nest('config.sync.load')
exports.create = (api) => {
var config
return nest('config.sync.load', () => {
if (!config) {
config = Config(process.env.ssb_appname || appName, opts)
config.keys = ssbKeys.loadOrCreateSync(Path.join(config.path, 'secret'))

// HACK: fix offline on windows by specifying 127.0.0.1 instead of localhost (default)
config.remote = `net:127.0.0.1:${config.port}~shs:${config.keys.id.slice(1).replace('.ed25519', '')}`
}
return config
})
}
135 changes: 135 additions & 0 deletions electron.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
var defaultMenu = require('electron-default-menu')
var WindowState = require('electron-window-state')
var electron = require('electron')
var Menu = electron.Menu
var Path = require('path')

var windows = {}
var quitting = false

electron.app.on('ready', () => {
var menu = defaultMenu(electron.app, electron.shell)
var view = menu.find(x => x.label === 'View')
view.submenu = [
{ role: 'reload' },
{ role: 'toggledevtools' },
{ type: 'separator' },
{ role: 'resetzoom' },
{ role: 'zoomin' },
{ role: 'zoomout' },
{ type: 'separator' },
{ role: 'togglefullscreen' }
]
if (process.platform === 'darwin') {
var win = menu.find(x => x.label === 'Window')
win.submenu = [
{ role: 'minimize' },
{ role: 'zoom' },
{ role: 'close', label: 'Close' },
{ type: 'separator' },
{ role: 'front' }
]
}

Menu.setApplicationMenu(Menu.buildFromTemplate(menu))

startBackgroundProcess()

// wait until server has started before opening main window
electron.ipcMain.once('server-started', function (ev, config) {
openMainWindow()
})

electron.app.on('before-quit', function () {
quitting = true
})

// allow inspecting of background process
electron.ipcMain.on('open-background-devtools', function (ev, config) {
if (windows.background) {
windows.background.webContents.openDevTools({detach: true})
}
})
})

function startBackgroundProcess () {
if (!windows.background) {
windows.background = openWindow(Path.join(__dirname, 'background-process.js'), {
connect: false,
center: true,
fullscreen: false,
fullscreenable: false,
height: 150,
maximizable: false,
minimizable: false,
resizable: false,
show: false,
skipTaskbar: true,
title: 'ticktack-server',
useContentSize: true,
width: 150
})
}
}

function openMainWindow () {
if (!windows.main) {
var windowState = WindowState({
defaultWidth: 1024,
defaultHeight: 768
})
windows.main = openWindow(Path.join(__dirname, 'main.js'), {
minWidth: 800,
x: windowState.x,
y: windowState.y,
width: windowState.width,
height: windowState.height,
autoHideMenuBar: true,
title: 'Ticktack',
show: true,
backgroundColor: '#EEE',
icon: './assets/icon.png'
})
windowState.manage(windows.main)
windows.main.setSheetOffset(40)
windows.main.on('close', function (e) {
if (!quitting && process.platform === 'darwin') {
e.preventDefault()
windows.main.hide()
}
})
windows.main.on('closed', function () {
windows.main = null
if (process.platform !== 'darwin') electron.app.quit()
})
}
}

function openWindow (path, opts) {
var window = new electron.BrowserWindow(opts)
window.webContents.on('dom-ready', function () {
window.webContents.executeJavaScript(`
var electron = require('electron')
var h = require('mutant/h')
electron.webFrame.setZoomLevelLimits(1, 1)
var title = ${JSON.stringify(opts.title || 'Ticktack')}
document.documentElement.querySelector('head').appendChild(
h('title', title)
)
require(${JSON.stringify(path)})
`)
})

window.webContents.on('will-navigate', function (e, url) {
e.preventDefault()
electron.shell.openExternal(url)
})

window.webContents.on('new-window', function (e, url) {
e.preventDefault()
electron.shell.openExternal(url)
})

window.loadURL('file://' + Path.join(__dirname, 'assets', 'base.html'))
return window
}
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const ticktack = {
app: require('./app'),
blob: require('./blob'),
config: require('./config'),
router: require('./router'),
styles: require('./styles')
}
Expand Down
Loading

0 comments on commit 8737709

Please sign in to comment.