Skip to content
This repository has been archived by the owner on Mar 1, 2022. It is now read-only.

Commit

Permalink
Config API-Calls and 405 Responses (#74)
Browse files Browse the repository at this point in the history
* added config get api calls

* /config with no query returns the full config
* /config with the key query returns the specified key

* added config set api call

* config documentation

* 405 api response

* re-read config instead of using the loaded version
  • Loading branch information
Andreas Heimann authored and torch2424 committed Oct 8, 2018
1 parent da494f5 commit 33753a3
Show file tree
Hide file tree
Showing 6 changed files with 252 additions and 2 deletions.
73 changes: 71 additions & 2 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 @@ -33,7 +33,9 @@
"chalk": "^2.4.1",
"chalkline": "0.0.5",
"cli-progress": "^2.1.0",
"edit-json-file": "^1.1.0",
"fastify": "^1.12.1",
"fastify-formbody": "^2.0.1",
"find": "^0.2.9",
"fluent-ffmpeg": "^2.1.2",
"fs-extra": "^7.0.0",
Expand Down
80 changes: 80 additions & 0 deletions src/api/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
const authService = require('./auth');
const upath = require('upath');
const fs = require('fs-extra');
const editJsonFile = require('edit-json-file');

const getFullConfig = async (path) => {
// Get current config
let config = editJsonFile(upath.join(path, 'config.json'));

// Return Config
return [200, config.toObject()];
};

const getConfigByKey = async (path, key) => {
// Get current config
let config = editJsonFile(upath.join(path, 'config.json'));

// Return Config by a specific key
let configValue = config.get(key);

// Return 200 if it has a value and 404 if it does not have a value
if (configValue) {
return [200, configValue];
} else {
return [404, null];
}
};

const changeConfig = async (path, config, key, newValue) => {
// Make string safe
newValue = toSafeString(newValue);
let currentValue = key.split('.').reduce(index, config);

// Change config
let configFile = editJsonFile(upath.join(path, 'config.json'));

configFile.set(key, JSON.parse(newValue));
configFile.save();

return [200, { key: key, oldValue: currentValue, newValue: JSON.parse(newValue) }];
};

// Helper function to make a string safe for ffmpeg & json
const toSafeString = function(string) {
return string;
};

module.exports = (fastify, path, stream, config) => {
fastify.get(
'/config',
authService.secureRouteHandler(config, async (request, reply) => {
// Returns full config is "key" is not set, otherwise only return the requested key
let response;
if (request.query.key) {
response = await getConfigByKey(path, request.query.key);
} else {
response = await getFullConfig(path);
}

reply.type('application/json').code(response[0]);
return {
key: request.query.key,
value: response[1]
};
})
);

// Change a setting
fastify.post(
'/config',
authService.secureRouteHandler(config, async (request, reply) => {
let response = await changeConfig(path, config, request.body.key, request.body.value);

reply.type('application/json').code(response[0]);
return {
response: response[1]
};
})
);
};
57 changes: 57 additions & 0 deletions src/api/endpoints.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,60 @@ Only showing one item from the generated example content. And used the `include_
}
```

## GET /config

### Description

Returns a param of the config.json. If no key is set, all config params will be returned

### Query Params

* `key` - The config key in dot-notation (example: 'api.host', 'interlude.overlay.enabled')

### Example Response:
**HTTP 200** - Successful
```
{
"key": "interlude.enabled",
"value": true
}
```
**HTTP 404** - Key not existant
```
{
"key": "interlude.awesomeness_level",
"value": null
}
```

## POST /config

### Description

Changes a config param in the config.json. If they key has no value, it will be added

### Body Params

* `key` - The config key in dot-notation (example: 'api.host', 'interlude.overlay.enabled')
* `value` - The new value for the config in json (example: (string) "test", (number) 5, (boolean) true)

### Example Response:
**HTTP 200** - Successful
```
{
"response": {
"key": "interlude.frequency",
"oldValue": "0.5",
"newValue": "0.2"
}
}
```
**HTTP 200** - Key not existant, new key will be saved
```
{
"response": {
"key": "interlude.awesomeness_level",
"newValue": "over 9000"
}
}
```
5 changes: 5 additions & 0 deletions src/api/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
const chalk = require('chalk');
const fastify = require('fastify')({});

// www-form-urlencoded parser for fastify
fastify.register(require('fastify-formbody'));

// Get our routes
const addStreamRoutes = require('./stream.js');
const addConfigRoutes = require('./config.js');
const addLibraryRoutes = require('./library.js');

let currentStream;
Expand All @@ -23,6 +27,7 @@ module.exports = {

// Implement our other routes
addStreamRoutes(fastify, path, currentStream, currentConfig);
addConfigRoutes(fastify, path, currentStream, currentConfig);
addLibraryRoutes(fastify, path, currentStream, currentConfig);

await new Promise((resolve, reject) => {
Expand Down
37 changes: 37 additions & 0 deletions src/api/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,41 @@ module.exports = (fastify, path, stream, config) => {
};
})
);

// Returns 405
fastify.post(
'/stream',
authService.secureRouteHandler(config, async (request, reply) => {
reply.type('application/json').code(405);
return {};
})
);
fastify.get(
'/stream/start',
authService.secureRouteHandler(config, async (request, reply) => {
reply.type('application/json').code(405);
return {};
})
);
fastify.get(
'/stream/stop',
authService.secureRouteHandler(config, async (request, reply) => {
reply.type('application/json').code(405);
return {};
})
);
fastify.get(
'/stream/restart',
authService.secureRouteHandler(config, async (request, reply) => {
reply.type('application/json').code(405);
return {};
})
);
fastify.post(
'/stream/history',
authService.secureRouteHandler(config, async (request, reply) => {
reply.type('application/json').code(405);
return {};
})
);
};

0 comments on commit 33753a3

Please sign in to comment.