Skip to content

Commit

Permalink
feat: add new endpoints for crypto onboarding
Browse files Browse the repository at this point in the history
  • Loading branch information
Cafe137 committed Mar 15, 2022
1 parent f81ede7 commit 80e1b3d
Show file tree
Hide file tree
Showing 13 changed files with 247 additions and 188 deletions.
11 changes: 5 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
const { shell } = require('electron')
const { runApiServer } = require('./src/api-server')
const { runElectronTray } = require('./src/electron')
const { runLauncher } = require('./src/launcher')
const { runStaticServer } = require('./src/static-server')
const { port, runServer } = require('./src/server')
const { getStatus } = require('./src/status')

if (require('./src/api-server').getStatus().status === 2) {
if (getStatus().status === 2) {
runLauncher()
} else {
shell.openExternal('http://localhost:5002')
shell.openExternal(`http://localhost:${port}/installer/`)
}
runElectronTray()
runStaticServer()
runApiServer()
runServer()
265 changes: 148 additions & 117 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
"license": "ISC",
"dependencies": {
"@koa/router": "^10.1.1",
"js-yaml": "^4.1.0",
"koa": "^2.13.4",
"koa-bodyparser": "^4.3.0",
"node-fetch": "^2.6.7",
"serve-handler": "^6.1.3"
"koa-static": "^5.0.0",
"node-fetch": "^2.6.7"
},
"devDependencies": {
"electron": "^17.1.0",
Expand Down
28 changes: 28 additions & 0 deletions src/config-yaml.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const { load, dump } = require('js-yaml')
const { readFileSync, writeFileSync } = require('fs')
const { FAILSAFE_SCHEMA } = require('js-yaml')

function getPath() {
return 'config.yaml'
}

function readConfigYaml() {
const raw = readFileSync(getPath(), 'utf-8')
const data = load(raw, {
schema: FAILSAFE_SCHEMA
})
return data
}

function writeConfigYaml(newValues) {
const data = readConfigYaml()
for (const [key, value] of Object.entries(newValues)) {
data[key] = value
}
writeFileSync(getPath(), dump(data))
}

module.exports = {
readConfigYaml,
writeConfigYaml
}
8 changes: 5 additions & 3 deletions src/electron.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
const { app, Tray, Menu, shell } = require('electron')
const { runLauncher } = require('./launcher')
const { BeeManager } = require('./lifecycle')
const { port } = require('./server')
const { getStatus } = require('./status')

let tray

function rebuildElectronTray() {
if (!tray) {
return
}
if (require('./api-server').getStatus().status !== 2) {
if (getStatus().status !== 2) {
const contextMenu = Menu.buildFromTemplate([
{ label: 'Open Installer', click: () => shell.openExternal('http://localhost:5002') },
{ label: 'Open Installer', click: () => shell.openExternal(`http://localhost:${port}/installer/`) },
{ type: 'separator' },
{
label: 'Exit',
Expand All @@ -34,7 +36,7 @@ function rebuildElectronTray() {
}
},
{ type: 'separator' },
{ label: 'Open Web UI', click: () => shell.openExternal('http://localhost:5000') },
{ label: 'Open Web UI', click: () => shell.openExternal(`http://localhost:${port}/dashboard/#/`) },
{ type: 'separator' },
{
label: 'Exit',
Expand Down
51 changes: 22 additions & 29 deletions src/api-server.js → src/server.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,19 @@
const Router = require('@koa/router')
const Koa = require('koa')
const koaBodyparser = require('koa-bodyparser')
const context = require('koa/lib/context')
const { existsSync, readFileSync } = require('fs')
const { createConfigFileAndAddress, createInitialTransaction } = require('./launcher')
const { rebuildElectronTray } = require('./electron')
const serve = require('koa-static')
const { writeConfigYaml, readConfigYaml } = require('./config-yaml')
const { createInitialTransaction, createConfigFileAndAddress, runLauncher } = require('./launcher')
const { BeeManager } = require('./lifecycle')
const { getStatus } = require('./status')

function getStatus() {
if (!existsSync('config.yaml') || !existsSync('data-dir')) {
return {
status: 0
}
}
const config = readFileSync('config.yaml', 'utf-8')
if (!config.includes('block-hash')) {
const { address } = JSON.parse(readFileSync('data-dir/keys/swarm.key'))
return {
address,
status: 1
}
}
return {
status: 2
}
}
const port = 5000

function main() {
function runServer() {
const app = new Koa()
app.use(serve('static'))
app.use(async (context, next) => {
context.set('Access-Control-Allow-Origin', 'http://localhost:5002')
context.set('Access-Control-Allow-Origin', `http://localhost:${port}`)
context.set('Access-Control-Allow-Credentials', 'true')
context.set(
'Access-Control-Allow-Headers',
Expand All @@ -48,15 +33,23 @@ function main() {
})
router.post('/setup/transaction', async context => {
await createInitialTransaction()
const { rebuildElectronTray } = require('./electron')
rebuildElectronTray()
context.body = getStatus()
})
router.patch('/config', context => {
writeConfigYaml(context.request.body)
context.body = readConfigYaml()
})
router.post('/restart', async context => {
BeeManager.stop()
await BeeManager.waitForSigtermToFinish()
runLauncher()
context.body = { success: true }
})
app.use(router.routes())
app.use(router.allowedMethods())
app.listen(5001)
app.listen(port)
}

module.exports = {
getStatus,
runApiServer: main
}
module.exports = { runServer, port }
20 changes: 0 additions & 20 deletions src/static-server.js

This file was deleted.

25 changes: 25 additions & 0 deletions src/status.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const { readFileSync, existsSync } = require('fs')
const { load } = require('js-yaml')
const { readConfigYaml } = require('./config-yaml')

function getStatus() {
const statusObject = {
status: 0,
address: null,
config: null
}
if (!existsSync('config.yaml') || !existsSync('data-dir')) {
return statusObject
}
statusObject.config = readConfigYaml()
const { address } = JSON.parse(readFileSync('data-dir/keys/swarm.key'))
statusObject.address = address
if (!statusObject.config['block-hash']) {
statusObject.status = 1
return statusObject
}
statusObject.status = 2
return statusObject
}

module.exports = { getStatus }
12 changes: 6 additions & 6 deletions static/installer/asset-manifest.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"files": {
"main.css": "/static/css/main.3814743f.css",
"main.js": "/static/js/main.b4ff9f8c.js",
"index.html": "/index.html",
"main.3814743f.css.map": "/static/css/main.3814743f.css.map",
"main.b4ff9f8c.js.map": "/static/js/main.b4ff9f8c.js.map"
"main.css": "/installer/static/css/main.3814743f.css",
"main.js": "/installer/static/js/main.59899838.js",
"index.html": "/installer/index.html",
"main.3814743f.css.map": "/installer/static/css/main.3814743f.css.map",
"main.59899838.js.map": "/installer/static/js/main.59899838.js.map"
},
"entrypoints": [
"static/css/main.3814743f.css",
"static/js/main.b4ff9f8c.js"
"static/js/main.59899838.js"
]
}
2 changes: 1 addition & 1 deletion static/installer/index.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="/favicon.ico"/><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="theme-color" content="#000000"/><meta name="description" content="Web site created using create-react-app"/><link rel="apple-touch-icon" href="/logo192.png"/><link rel="manifest" href="/manifest.json"/><title>React App</title><script defer="defer" src="/static/js/main.b4ff9f8c.js"></script><link href="/static/css/main.3814743f.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div></body></html>
<!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="/installer/favicon.ico"/><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="theme-color" content="#000000"/><meta name="description" content="Web site created using create-react-app"/><link rel="apple-touch-icon" href="/installer/logo192.png"/><link rel="manifest" href="/installer/manifest.json"/><title>React App</title><script defer="defer" src="/installer/static/js/main.59899838.js"></script><link href="/installer/static/css/main.3814743f.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div></body></html>

Large diffs are not rendered by default.

Large diffs are not rendered by default.

0 comments on commit 80e1b3d

Please sign in to comment.