Skip to content

Commit

Permalink
Reorganize API code
Browse files Browse the repository at this point in the history
  • Loading branch information
nickcoutsos committed Sep 13, 2021
1 parent b15b2f1 commit 25fd989
Show file tree
Hide file tree
Showing 13 changed files with 110 additions and 83 deletions.
14 changes: 14 additions & 0 deletions api/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const express = require('express')
const bodyParser = require('body-parser')

const applicationInit = require('./routes/application')
const keyboards = require('./routes/keyboards')

const app = express()

app.use(bodyParser.json())
applicationInit(app)
app.use(keyboards)

module.exports = app

39 changes: 39 additions & 0 deletions api/routes/application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const childProcess = require('child_process')
const path = require('path')
const express = require('express')
const expressWs = require('express-ws')

const appDir = path.join(__dirname, '..', '..', 'application')

function init (app) {
expressWs(app)

childProcess.execFile('npm', ['run', 'build-watch'], { cwd: appDir }, err => {
console.error(err)
console.error('Application serving failed')
process.exit(1)
})

app.get('/', (req, res) => res.redirect('/application'))
app.use('/application', express.static(path.join(appDir, 'dist')))

const subscribers = []
app.ws('/console', (ws, req) => {
const { remoteAddress } = req.connection
subscribers.push(ws)

console.info(`[${new Date()}] [${remoteAddress}] connected`)

ws.onerror = err => {
console.error(`[${new Date()}] [${remoteAddress}]`, err)
}

ws.onclose = () => {
console.info(`[${new Date()}] [${remoteAddress}] disconnected`)
const index = subscribers.indexOf(ws)
subscribers.splice(index, 1)
}
})
}

module.exports = init
49 changes: 49 additions & 0 deletions api/routes/keyboards.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const { Router } = require('express')
const zmk = require('../services/zmk/zmk')

const firmwares = { zmk }
const router = Router()

function addFirmwareLibrary(req, res, next) {

if (!('firmware' in req.query)) {
return res.status(400).json({
error: 'Must include "firmware" query parameter'
})
}

if (!firmwares[req.query.firmware]) {
return res.status(400).json({
error: `Unknown firmware "${req.query.firmware}"`
})
}

req.firmware = firmwares[req.query.firmware]
next()
}

router.get('/behaviors', addFirmwareLibrary, (req, res) => res.json(req.firmware.loadBehaviors()))
router.get('/keycodes', addFirmwareLibrary, (req, res) => res.json(req.firmware.loadKeycodes()))
router.get('/layout', addFirmwareLibrary, (req, res) => res.json(req.firmware.loadLayout()))
router.get('/keymap', addFirmwareLibrary, (req, res) => res.json(req.firmware.loadKeymap()))
router.post('/keymap', addFirmwareLibrary, (req, res) => {
const keymap = req.body
const layout = req.firmware.loadLayout()
const generatedKeymap = req.firmware.generateKeymap(layout, keymap)
const exportStdout = req.firmware.exportKeymap(generatedKeymap, 'flash' in req.query, err => {
if (err) {
res.status(500).send(err)
return
}

res.send()
})

// exportStdout.stdout.on('data', data => {
// for (let sub of subscribers) {
// sub.send(data)
// }
// })
})

module.exports = router
Empty file added api/services/index.js
Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Empty file added api/services/zmk/index.js
Empty file.
File renamed without changes.
6 changes: 3 additions & 3 deletions zmk.js → api/services/zmk/zmk.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ const fs = require('fs')
const path = require('path')
const { renderTable } = require('./layout')

const ZMK_PATH = 'zmk-config'
const ZMK_PATH = path.join(__dirname, '..', '..', '..', 'zmk-config')
const KEYBOARD = 'dactyl'

function loadBehaviors() {
return JSON.parse(fs.readFileSync('./data/zmk-behaviors.json'))
return JSON.parse(fs.readFileSync(path.join(__dirname, 'data', 'zmk-behaviors.json')))
}

function loadKeycodes() {
return JSON.parse(fs.readFileSync('./data/zmk-keycodes.json'))
return JSON.parse(fs.readFileSync(path.join(__dirname, 'data', 'zmk-keycodes.json')))
}

function loadLayout (layout = 'LAYOUT') {
Expand Down
82 changes: 2 additions & 80 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,84 +1,6 @@
const childProcess = require('child_process')
const process = require('process')
const express = require('express')
const expressWs = require('express-ws')
const bodyParser = require('body-parser')
const zmk = require('./zmk')

const app = express()
const subscribers = []
expressWs(app)
app.use(bodyParser.json())

childProcess.execFile('npm', ['run', 'build-watch'], { cwd: './application' }, err => {
console.error(err)
console.error('Application serving failed')
process.exit(1)
})

const firmwares = { zmk }

function addFirmwareLibrary(req, res, next) {

if (!('firmware' in req.query)) {
return res.status(400).json({
error: 'Must include "firmware" query parameter'
})
}

if (!firmwares[req.query.firmware]) {
return res.status(400).json({
error: `Unknown firmware "${req.query.firmware}"`
})
}

req.firmware = firmwares[req.query.firmware]
next()
}

app.get('/', (req, res) => res.redirect('/application'))
app.use('/application', express.static('application/dist'))
app.get('/behaviors', addFirmwareLibrary, (req, res) => res.json(req.firmware.loadBehaviors()))
app.get('/keycodes', addFirmwareLibrary, (req, res) => res.json(req.firmware.loadKeycodes()))
app.get('/layout', addFirmwareLibrary, (req, res) => res.json(req.firmware.loadLayout()))
app.get('/keymap', addFirmwareLibrary, (req, res) => res.json(req.firmware.loadKeymap()))
app.post('/keymap', addFirmwareLibrary, (req, res) => {
const keymap = req.body
const layout = req.firmware.loadLayout()
const generatedKeymap = req.firmware.generateKeymap(layout, keymap)
const exportStdout = req.firmware.exportKeymap(generatedKeymap, 'flash' in req.query, err => {
if (err) {
res.status(500).send(err)
return
}

res.send()
})

exportStdout.stdout.on('data', data => {
for (let sub of subscribers) {
sub.send(data)
}
})
})

app.ws('/console', (ws, req) => {
const { remoteAddress } = req.connection
subscribers.push(ws)

console.info(`[${new Date()}] [${remoteAddress}] connected`)

ws.onerror = err => {
console.error(`[${new Date()}] [${remoteAddress}]`, err)
}

ws.onclose = () => {
console.info(`[${new Date()}] [${remoteAddress}] disconnected`)
const index = subscribers.indexOf(ws)
subscribers.splice(index, 1)
}
})
const api = require('./api')

const PORT = process.env.PORT || 8080
app.listen(PORT)
api.listen(PORT)
console.log('listening on', PORT)
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
"description": "",
"main": "index.js",
"scripts": {
"postinstall": "cd application && npm install",
"start": "node index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
Expand Down

0 comments on commit 25fd989

Please sign in to comment.