forked from nukeop/nuclear
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a simple express rest api to remotly control the player, th…
…e settings and the window
- Loading branch information
Charles Jacquin
committed
Mar 9, 2019
1 parent
7aa7d95
commit 671e2ef
Showing
19 changed files
with
390 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"extends": "../.eslintrc.js", | ||
"env": { | ||
"node": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import settings from '../../../app/constants/settings'; | ||
|
||
export const RESTRICTED_SETTINGS = []; | ||
export const READONLY_SETTINGS = []; | ||
|
||
export const getSettingsSchema = { | ||
params: { | ||
type: 'object', | ||
required: ['option'], | ||
properties: { | ||
option: { | ||
type: 'string', | ||
enum: settings | ||
.filter(({ name }) => !RESTRICTED_SETTINGS.includes(name)) | ||
.map(({ name }) => name) | ||
} | ||
} | ||
} | ||
}; | ||
|
||
export const updateSettingsSchema = { | ||
params: { | ||
type: 'object', | ||
required: ['option'], | ||
properties: { | ||
option: { | ||
type: 'string', | ||
enum: settings | ||
.filter(({ name }) => !READONLY_SETTINGS.includes(name)) | ||
.filter(({ name }) => !RESTRICTED_SETTINGS.includes(name)) | ||
.map(({ name }) => name) | ||
} | ||
} | ||
}, | ||
body: { | ||
type: 'object', | ||
required: ['value'], | ||
properties: { | ||
value: { | ||
type: ['string', 'boolean', 'number'] | ||
} | ||
} | ||
} | ||
}; | ||
|
||
export const volumeSchema = { | ||
body: { | ||
type: 'object', | ||
required: ['value'], | ||
properties: { | ||
value: { | ||
type: 'number', | ||
minimum: 0, | ||
maximum: 100 | ||
} | ||
} | ||
} | ||
}; | ||
|
||
export const seekSchema = { | ||
body: { | ||
type: 'object', | ||
required: ['value'], | ||
properties: { | ||
value: { | ||
type: 'number', | ||
minimum: 0 | ||
} | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './window'; | ||
export * from './settings'; | ||
export * from './player'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import express from 'express'; | ||
import { Validator } from 'express-json-validator-middleware'; | ||
|
||
import { | ||
onNext, | ||
onPrevious, | ||
onPause, | ||
onPlayPause, | ||
onStop, | ||
onPlay, | ||
onVolume, | ||
onSeek | ||
} from '../../mpris'; | ||
import { volumeSchema, seekSchema } from './_schema'; | ||
|
||
const { validate } = new Validator({ allErrors: true }); | ||
|
||
export function playerRouter() { | ||
|
||
const router = express.Router(); | ||
|
||
router.post('/next', (req, res) => { | ||
onNext(); | ||
res.send(); | ||
}); | ||
|
||
router.post('/previous', (req, res) => { | ||
onPrevious(); | ||
res.send(); | ||
}); | ||
|
||
router.post('/pause', (req, res) => { | ||
onPause(); | ||
res.send(); | ||
}); | ||
|
||
router.post('/play-pause', (req, res) => { | ||
onPlayPause(); | ||
res.send(); | ||
}); | ||
|
||
router.post('/stop', (req, res) => { | ||
onStop(); | ||
res.send(); | ||
}); | ||
|
||
router.post('/play', (req, res) => { | ||
onPlay(); | ||
res.send(); | ||
}); | ||
|
||
router.post('/volume', validate(volumeSchema), (req, res) => { | ||
onVolume(req.body.value); | ||
res.send(); | ||
}); | ||
|
||
router.post('/seek', validate(seekSchema), (req, res) => { | ||
onSeek(req.body.value); | ||
res.send(); | ||
}); | ||
|
||
return router; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import express from 'express'; | ||
import { Validator } from 'express-json-validator-middleware'; | ||
|
||
import { onSettings } from '../../mpris'; | ||
import { getOption, store } from '../../store'; | ||
import { getSettingsSchema, updateSettingsSchema, RESTRICTED_SETTINGS } from './_schema'; | ||
import settingsParams from '../../../app/constants/settings'; | ||
|
||
const { validate } = new Validator({ allErrors: true }); | ||
|
||
export function settingsRouter() { | ||
|
||
const router = express.Router(); | ||
|
||
router.get('/', (req, res) => { | ||
const settings = store.get('settings'); | ||
const filteredSettings = settingsParams | ||
.filter(({ name }) => !RESTRICTED_SETTINGS.includes(name)) | ||
.reduce((acc, item) => ({ | ||
...acc, | ||
[item.name]: settings[item.name] || item.default | ||
}), {}); | ||
|
||
|
||
res.json(filteredSettings); | ||
}); | ||
|
||
router | ||
.route('/:option') | ||
.get( | ||
validate(getSettingsSchema), | ||
(req, res) => { | ||
res.send(getOption(req.params.option)); | ||
} | ||
) | ||
.post( | ||
validate(updateSettingsSchema), | ||
(req, res) => { | ||
onSettings({ [req.params.option]: req.body.value }); | ||
res.send(); | ||
} | ||
); | ||
|
||
return router; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import express from 'express'; | ||
const { ipcMain } = require('electron'); | ||
|
||
export function windowRouter() { | ||
|
||
const router = express.Router(); | ||
|
||
router.post('/quit', (req, res) => { | ||
ipcMain.emit('close'); | ||
res.send(); | ||
}); | ||
|
||
router.post('/maximize', (req, res) => { | ||
ipcMain.emit('maximize'); | ||
res.send(); | ||
}); | ||
|
||
router.post('/minimize', (req, res) => { | ||
ipcMain.emit('minimize'); | ||
res.send(); | ||
}); | ||
|
||
return router; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { ValidationError } from 'express-json-validator-middleware'; | ||
|
||
const getValidationMessage = ({ validationErrors }) => { | ||
if (validationErrors.params) { | ||
const err = validationErrors.params.shift(); | ||
return `${err.dataPath} ${err.message} ${err.params.allowedValues.toString()}`; | ||
} else { | ||
return `request body ${validationErrors.body.shift().message}`; | ||
} | ||
}; | ||
|
||
export function errorMiddleware(logger) { | ||
return (err, req, res, next) => { | ||
if (err instanceof ValidationError) { | ||
const message = getValidationMessage(err); | ||
|
||
res.status(400).send(message); | ||
next(); | ||
} else { | ||
logger.error(err); | ||
res.status(500).send('Internal Server Error'); | ||
} | ||
}; | ||
} | ||
|
||
export function notFoundMiddleware() { | ||
return (req, res) => { | ||
res.status(404).send('Not Found'); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import Logger from 'electron-timber'; | ||
import express from 'express'; | ||
import bodyParser from 'body-parser'; | ||
|
||
import { windowRouter, playerRouter, settingsRouter } from './api'; | ||
import { errorMiddleware, notFoundMiddleware } from './middlewares'; | ||
|
||
function runHttpServer({ | ||
log, | ||
port = 3000, | ||
host = '0.0.0.0', | ||
prefix = '/nuclear' | ||
}) { | ||
const app = express(); | ||
const logger = log | ||
? Logger.create({ name: 'http api' }) | ||
: { log: () => {}, error: () => {} }; | ||
|
||
return app | ||
.use(bodyParser.urlencoded({ extended: false })) | ||
.use(bodyParser.json()) | ||
.use(`${prefix}/window`, windowRouter()) | ||
.use(`${prefix}/player`, playerRouter()) | ||
.use(`${prefix}/settings`, settingsRouter()) | ||
.use(notFoundMiddleware()) | ||
.use(errorMiddleware(logger)) | ||
.listen(port, host, err => { | ||
if (err) { | ||
logger.error(err); | ||
} else { | ||
logger.log(`nuclear api available on port ${port}`); | ||
} | ||
}); | ||
} | ||
|
||
module.exports = runHttpServer; |
Oops, something went wrong.