Skip to content

Instantly share code, notes, and snippets.

@MartinMuzatko
Last active June 21, 2018 12:23
Show Gist options
  • Save MartinMuzatko/0a4d10eac0f1a1097ca29bef6745c3f5 to your computer and use it in GitHub Desktop.
Save MartinMuzatko/0a4d10eac0f1a1097ca29bef6745c3f5 to your computer and use it in GitHub Desktop.
/**
* This module takes care about installing, listing, disabling/enabling and managing apps
* Apps are folders run as static server within server/frontend (they are driven by staticservers.hjson)
*/
// read config files
const Config = require('./config');
const constants = require('./constants');
const staticservers = new Config(constants.STATICSERVERSCONFIGPATH);
const serverconfig = require('./serverconfig')
/**
* Returns the staticservers.hjson as javascript object
*
* @returns {Promise<Object>}
*/
async function getConfig() {
let x = await staticservers.get(true)
return x
}
/**
* Retrieve the list of apps and add attributes like URL
*
* @returns
*/
async function list() {
const servers = await getConfig()
const url = await serverconfig.getURL()
return servers.sites.map(app => {
app.url = `${url}/${app.pathname}`
return app
})
}
async function findByPathname(pathname) {
let apps = await list()
return apps.find(app => app.pathname == pathname)
}
async function validateApp(app) {
if (!(
app
&& app.hasOwnProperty('pathname')
&& app.hasOwnProperty('path')
&& app.hasOwnProperty('name')
)) throw new Error('Missing property in app object. Provide pathname, name and path')
if (await findByPathname(app.pathname)) throw new Error('App already exists. Pick a unique pathname')
return true
}
function mergeAppWithDefaults(app) {
const defaultOptions = {
listed: true,
enabled: true,
}
return Object.assign({}, defaultOptions, app, { system: false })
}
async function create(app) {
await validateApp(app)
app = mergeAppWithDefaults(app)
let apps = await getConfig()
apps.sites.push(app)
await staticservers.set(apps)
return app
}
async function update(pathname, app) {
let apps = await getConfig()
let index = apps.sites.findIndex(app => app.pathname == pathname && !app.system)
if (!~index) throw new Error('App not found to update. Pathname does not exist or app is system app')
apps.sites[index] = mergeAppWithDefaults(app)
await staticservers.set(apps)
return apps.sites[index]
}
/**
* Set default app by pathname attribute
*
* @throws when app does not exist
* @param {String} pathname
* @returns {Promise<Object>}
*/
async function setDefault(pathname) {
let apps = await getConfig()
let appExists = apps.sites.find(app => app.pathname == pathname)
if (!appExists) throw new Error('The new default app does not exist')
apps.default = pathname
return staticservers.set(apps)
}
/**
* Get default app as app object
*
* @returns
*/
async function getDefault() {
let apps = await getConfig()
return apps.sites.find(app => app.pathname == apps.default)
}
module.exports = {
list,
create,
update,
setDefault,
getDefault,
getConfig,
}
{
"default": "cockpit",
"sites": [
{
"name": "Cockpit",
"pathname": "cockpit",
"path": "./frontend/cockpitV3/dist"
},
{
"name": "Setup",
"pathname": "setup",
"path": "./frontend/setup/www"
},
{
"name": "Config",
"pathname": "config",
"path": "./frontend/config/www"
},
{
"name": "Rest API Documentation",
"pathname": "apidocs",
"path": "./mods/apidocs"
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment