This repository has been archived by the owner on Mar 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Config API-Calls and 405 Responses (#74)
* 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
Showing
6 changed files
with
252 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,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] | ||
}; | ||
}) | ||
); | ||
}; |
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