Skip to content

Commit

Permalink
[wip] server enhancement
Browse files Browse the repository at this point in the history
  • Loading branch information
cheton committed Nov 26, 2019
1 parent 6cf4668 commit 0b8ed1f
Show file tree
Hide file tree
Showing 22 changed files with 201 additions and 207 deletions.
32 changes: 16 additions & 16 deletions src/server/api/api.commands.js
@@ -1,6 +1,6 @@
import find from 'lodash/find';
import castArray from 'lodash/castArray';
import isPlainObject from 'lodash/isPlainObject';
import ensureArray from 'ensure-array';
import _find from 'lodash/find';
import _isPlainObject from 'lodash/isPlainObject';
import uuid from 'uuid';
import settings from '../config/settings';
import { ensureFiniteNumber } from '../lib/ensure-type';
Expand All @@ -13,19 +13,19 @@ import {
ERR_INTERNAL_SERVER_ERROR
} from '../constants';

const config = serviceContainer.resolve('config');
const task = serviceContainer.resolve('task');
const shellCommand = serviceContainer.resolve('shellCommand');
const userStore = serviceContainer.resolve('userStore');

const log = logger('api:commands');

const CONFIG_KEY = 'commands';

const getSanitizedRecords = () => {
const records = castArray(config.get(CONFIG_KEY, []));
const records = ensureArray(userStore.get(CONFIG_KEY));

let shouldUpdate = false;
for (let i = 0; i < records.length; ++i) {
if (!isPlainObject(records[i])) {
if (!_isPlainObject(records[i])) {
records[i] = {};
}

Expand All @@ -52,7 +52,7 @@ const getSanitizedRecords = () => {
log.debug(`update sanitized records: ${JSON.stringify(records)}`);

// Pass `{ silent changes }` will suppress the change event
config.set(CONFIG_KEY, records, { silent: true });
userStore.set(CONFIG_KEY, records, { silent: true });
}

return records;
Expand Down Expand Up @@ -121,7 +121,7 @@ export const create = (req, res) => {
};

records.push(record);
config.set(CONFIG_KEY, records);
userStore.set(CONFIG_KEY, records);

res.send({ id: record.id, mtime: record.mtime });
} catch (err) {
Expand All @@ -134,7 +134,7 @@ export const create = (req, res) => {
export const read = (req, res) => {
const id = req.params.id;
const records = getSanitizedRecords();
const record = find(records, { id: id });
const record = _find(records, { id: id });

if (!record) {
res.status(ERR_NOT_FOUND).send({
Expand All @@ -150,7 +150,7 @@ export const read = (req, res) => {
export const update = (req, res) => {
const id = req.params.id;
const records = getSanitizedRecords();
const record = find(records, { id: id });
const record = _find(records, { id: id });

if (!record) {
res.status(ERR_NOT_FOUND).send({
Expand Down Expand Up @@ -178,7 +178,7 @@ export const update = (req, res) => {
delete record.command;
}

config.set(CONFIG_KEY, records);
userStore.set(CONFIG_KEY, records);

res.send({ id: record.id, mtime: record.mtime });
} catch (err) {
Expand All @@ -191,7 +191,7 @@ export const update = (req, res) => {
export const __delete = (req, res) => {
const id = req.params.id;
const records = getSanitizedRecords();
const record = find(records, { id: id });
const record = _find(records, { id: id });

if (!record) {
res.status(ERR_NOT_FOUND).send({
Expand All @@ -204,7 +204,7 @@ export const __delete = (req, res) => {
const filteredRecords = records.filter(record => {
return record.id !== id;
});
config.set(CONFIG_KEY, filteredRecords);
userStore.set(CONFIG_KEY, filteredRecords);

res.send({ id: record.id });
} catch (err) {
Expand All @@ -217,7 +217,7 @@ export const __delete = (req, res) => {
export const run = (req, res) => {
const id = req.params.id;
const records = getSanitizedRecords();
const record = find(records, { id: id });
const record = _find(records, { id: id });

if (!record) {
res.status(ERR_NOT_FOUND).send({
Expand All @@ -231,7 +231,7 @@ export const run = (req, res) => {

log.info(`run: title="${title}", commands="${commands}"`);

const taskId = task.run(commands, title);
const taskId = shellCommand.spawn(commands);

res.send({ taskId: taskId });
};
26 changes: 13 additions & 13 deletions src/server/api/api.events.js
@@ -1,6 +1,6 @@
import find from 'lodash/find';
import castArray from 'lodash/castArray';
import isPlainObject from 'lodash/isPlainObject';
import ensureArray from 'ensure-array';
import _find from 'lodash/find';
import _isPlainObject from 'lodash/isPlainObject';
import uuid from 'uuid';
import settings from '../config/settings';
import { ensureFiniteNumber } from '../lib/ensure-type';
Expand All @@ -13,18 +13,18 @@ import {
ERR_INTERNAL_SERVER_ERROR
} from '../constants';

const config = serviceContainer.resolve('config');
const userStore = serviceContainer.resolve('userStore');

const log = logger('api:events');

const CONFIG_KEY = 'events';

const getSanitizedRecords = () => {
const records = castArray(config.get(CONFIG_KEY, []));
const records = ensureArray(userStore.get(CONFIG_KEY));

let shouldUpdate = false;
for (let i = 0; i < records.length; ++i) {
if (!isPlainObject(records[i])) {
if (!_isPlainObject(records[i])) {
records[i] = {};
}

Expand All @@ -51,7 +51,7 @@ const getSanitizedRecords = () => {
log.debug(`update sanitized records: ${JSON.stringify(records)}`);

// Pass `{ silent changes }` will suppress the change event
config.set(CONFIG_KEY, records, { silent: true });
userStore.set(CONFIG_KEY, records, { silent: true });
}

return records;
Expand Down Expand Up @@ -129,7 +129,7 @@ export const create = (req, res) => {
};

records.push(record);
config.set(CONFIG_KEY, records);
userStore.set(CONFIG_KEY, records);

res.send({ id: record.id, mtime: record.mtime });
} catch (err) {
Expand All @@ -142,7 +142,7 @@ export const create = (req, res) => {
export const read = (req, res) => {
const id = req.params.id;
const records = getSanitizedRecords();
const record = find(records, { id: id });
const record = _find(records, { id: id });

if (!record) {
res.status(ERR_NOT_FOUND).send({
Expand All @@ -158,7 +158,7 @@ export const read = (req, res) => {
export const update = (req, res) => {
const id = req.params.id;
const records = getSanitizedRecords();
const record = find(records, { id: id });
const record = _find(records, { id: id });

if (!record) {
res.status(ERR_NOT_FOUND).send({
Expand Down Expand Up @@ -188,7 +188,7 @@ export const update = (req, res) => {
delete record.command;
}

config.set(CONFIG_KEY, records);
userStore.set(CONFIG_KEY, records);

res.send({ id: record.id, mtime: record.mtime });
} catch (err) {
Expand All @@ -201,7 +201,7 @@ export const update = (req, res) => {
export const __delete = (req, res) => {
const id = req.params.id;
const records = getSanitizedRecords();
const record = find(records, { id: id });
const record = _find(records, { id: id });

if (!record) {
res.status(ERR_NOT_FOUND).send({
Expand All @@ -214,7 +214,7 @@ export const __delete = (req, res) => {
const filteredRecords = records.filter(record => {
return record.id !== id;
});
config.set(CONFIG_KEY, filteredRecords);
userStore.set(CONFIG_KEY, filteredRecords);

res.send({ id: record.id });
} catch (err) {
Expand Down
4 changes: 2 additions & 2 deletions src/server/api/api.gcode.js
@@ -1,4 +1,4 @@
import get from 'lodash/get';
import _get from 'lodash/get';
import controllers from '../store/controllers';
import {
ERR_BAD_REQUEST,
Expand Down Expand Up @@ -72,7 +72,7 @@ export const fetch = (req, res) => {
};

export const download = (req, res) => {
const ident = get(req, 'query.ident') || get(req, 'body.ident');
const ident = _get(req, 'query.ident') || _get(req, 'body.ident');

if (!ident) {
res.status(ERR_BAD_REQUEST).send({
Expand Down
14 changes: 7 additions & 7 deletions src/server/api/api.machines.js
@@ -1,7 +1,7 @@
import ensureArray from 'ensure-array';
import _get from 'lodash/get';
import _set from 'lodash/set';
import _find from 'lodash/find';
import _castArray from 'lodash/castArray';
import _isPlainObject from 'lodash/isPlainObject';
import uuid from 'uuid';
import settings from '../config/settings';
Expand All @@ -15,14 +15,14 @@ import {
ERR_INTERNAL_SERVER_ERROR
} from '../constants';

const config = serviceContainer.resolve('config');
const userStore = serviceContainer.resolve('userStore');

const log = logger('api:machines');

const CONFIG_KEY = 'machines';

const getSanitizedRecords = () => {
const records = _castArray(config.get(CONFIG_KEY, []));
const records = ensureArray(userStore.get(CONFIG_KEY));

let shouldUpdate = false;
for (let i = 0; i < records.length; ++i) {
Expand All @@ -42,7 +42,7 @@ const getSanitizedRecords = () => {
log.debug(`update sanitized records: ${JSON.stringify(records)}`);

// Pass `{ silent changes }` will suppress the change event
config.set(CONFIG_KEY, records, { silent: true });
userStore.set(CONFIG_KEY, records, { silent: true });
}

return records;
Expand Down Expand Up @@ -104,7 +104,7 @@ export const create = (req, res) => {
try {
const records = getSanitizedRecords();
records.push(ensureMachineProfile(record));
config.set(CONFIG_KEY, records);
userStore.set(CONFIG_KEY, records);

res.send({ id: record.id });
} catch (err) {
Expand Down Expand Up @@ -160,7 +160,7 @@ export const update = (req, res) => {
_set(record, key, (typeof ensureType === 'function') ? ensureType(value) : value);
});

config.set(CONFIG_KEY, records);
userStore.set(CONFIG_KEY, records);

res.send({ id: record.id });
} catch (err) {
Expand All @@ -186,7 +186,7 @@ export const __delete = (req, res) => {
const filteredRecords = records.filter(record => {
return record.id !== id;
});
config.set(CONFIG_KEY, filteredRecords);
userStore.set(CONFIG_KEY, filteredRecords);

res.send({ id: record.id });
} catch (err) {
Expand Down
26 changes: 13 additions & 13 deletions src/server/api/api.macros.js
@@ -1,6 +1,6 @@
import find from 'lodash/find';
import castArray from 'lodash/castArray';
import isPlainObject from 'lodash/isPlainObject';
import ensureArray from 'ensure-array';
import _find from 'lodash/find';
import _isPlainObject from 'lodash/isPlainObject';
import uuid from 'uuid';
import settings from '../config/settings';
import { ensureFiniteNumber } from '../lib/ensure-type';
Expand All @@ -13,18 +13,18 @@ import {
ERR_INTERNAL_SERVER_ERROR
} from '../constants';

const config = serviceContainer.resolve('config');
const userStore = serviceContainer.resolve('userStore');

const log = logger('api:macros');

const CONFIG_KEY = 'macros';

const getSanitizedRecords = () => {
const records = castArray(config.get(CONFIG_KEY, []));
const records = ensureArray(userStore.get(CONFIG_KEY));

let shouldUpdate = false;
for (let i = 0; i < records.length; ++i) {
if (!isPlainObject(records[i])) {
if (!_isPlainObject(records[i])) {
records[i] = {};
}

Expand All @@ -40,7 +40,7 @@ const getSanitizedRecords = () => {
log.debug(`update sanitized records: ${JSON.stringify(records)}`);

// Pass `{ silent changes }` will suppress the change event
config.set(CONFIG_KEY, records, { silent: true });
userStore.set(CONFIG_KEY, records, { silent: true });
}

return records;
Expand Down Expand Up @@ -104,7 +104,7 @@ export const create = (req, res) => {
};

records.push(record);
config.set(CONFIG_KEY, records);
userStore.set(CONFIG_KEY, records);

res.send({ err: null });
} catch (err) {
Expand All @@ -117,7 +117,7 @@ export const create = (req, res) => {
export const read = (req, res) => {
const id = req.params.id;
const records = getSanitizedRecords();
const record = find(records, { id: id });
const record = _find(records, { id: id });

if (!record) {
res.status(ERR_NOT_FOUND).send({
Expand All @@ -133,7 +133,7 @@ export const read = (req, res) => {
export const update = (req, res) => {
const id = req.params.id;
const records = getSanitizedRecords();
const record = find(records, { id: id });
const record = _find(records, { id: id });

if (!record) {
res.status(ERR_NOT_FOUND).send({
Expand Down Expand Up @@ -168,7 +168,7 @@ export const update = (req, res) => {
record.name = String(name || '');
record.content = String(content || '');

config.set(CONFIG_KEY, records);
userStore.set(CONFIG_KEY, records);

res.send({ err: null });
} catch (err) {
Expand All @@ -181,7 +181,7 @@ export const update = (req, res) => {
export const __delete = (req, res) => {
const id = req.params.id;
const records = getSanitizedRecords();
const record = find(records, { id: id });
const record = _find(records, { id: id });

if (!record) {
res.status(ERR_NOT_FOUND).send({
Expand All @@ -194,7 +194,7 @@ export const __delete = (req, res) => {
const filteredRecords = records.filter(record => {
return record.id !== id;
});
config.set(CONFIG_KEY, filteredRecords);
userStore.set(CONFIG_KEY, filteredRecords);

res.send({ err: null });
} catch (err) {
Expand Down

0 comments on commit 0b8ed1f

Please sign in to comment.