diff --git a/bin/lib/start.js b/bin/lib/start.js index 8967fe238..b39cf81db 100644 --- a/bin/lib/start.js +++ b/bin/lib/start.js @@ -2,7 +2,6 @@ const options = require('./options') const fs = require('fs') -const extend = require('extend') const { cyan, red, bold } = require('colorette') module.exports = function (program, server) { @@ -23,7 +22,7 @@ module.exports = function (program, server) { start.option('-v, --verbose', 'Print the logs to console') start.action((opts) => { - let argv = extend({}, opts, { version: program.version() }) + let argv = Object.assign({}, opts, { version: program.version() }) let configFile = argv['configFile'] || './config.json' fs.readFile(configFile, (err, file) => { diff --git a/config/defaults.js b/config/defaults.js index a2032929c..fdfdf6184 100644 --- a/config/defaults.js +++ b/config/defaults.js @@ -11,7 +11,9 @@ module.exports = { 'port': 8443, 'serverUri': 'https://localhost:8443', 'webid': true, - 'dataBrowserPath': 'default' + 'dataBrowserPath': 'default', + 'suffixAcl': '.acl', + 'suffixMeta': '.meta' // For use in Enterprises to configure a HTTP proxy for all outbound HTTP requests from the SOLID server (we use // https://www.npmjs.com/package/global-tunnel-ng). diff --git a/lib/create-app.js b/lib/create-app.js index 62d0cedf1..32ba95939 100644 --- a/lib/create-app.js +++ b/lib/create-app.js @@ -39,11 +39,14 @@ function createApp (argv = {}) { // Override default configs (defaults) with passed-in params (argv) argv = Object.assign({}, defaults, argv) - argv.host = SolidHost.from({ port: argv.port, serverUri: argv.serverUri }) + const hostConfig = config.hostConfigFor(argv) + argv.host = SolidHost.from(hostConfig) const configPath = config.initConfigDir(argv) argv.templates = config.initTemplateDirs(configPath) + config.printDebugInfo(argv) + const ldp = new LDP(argv) const app = express() diff --git a/lib/create-server.js b/lib/create-server.js index 31216c834..c3071a52d 100644 --- a/lib/create-server.js +++ b/lib/create-server.js @@ -13,7 +13,6 @@ function createServer (argv, app) { argv = argv || {} app = app || express() const ldpApp = createApp(argv) - const ldp = ldpApp.locals.ldp || {} let mount = argv.mount || '/' // Removing ending '/' if (mount.length > 1 && @@ -72,7 +71,7 @@ function createServer (argv, app) { cert: cert }, argv) - if (ldp.webid && ldp.auth === 'tls') { + if (argv.webid && argv.auth === 'tls') { credentials.requestCert = true } @@ -98,9 +97,9 @@ function createServer (argv, app) { } // Setup Express app - if (ldp.live) { + if (argv.live) { const solidWs = SolidWs(server, ldpApp) - ldpApp.locals.ldp.live = solidWs.publish.bind(solidWs) + ldpApp.locals.host.live = solidWs.publish.bind(solidWs) } return server diff --git a/lib/handlers/get.js b/lib/handlers/get.js index c92731792..0ea455963 100644 --- a/lib/handlers/get.js +++ b/lib/handlers/get.js @@ -19,7 +19,7 @@ const error = require('../http-error') const RDFs = require('../ldp').RDF_MIME_TYPES function handler (req, res, next) { - const ldp = req.app.locals.ldp + const { ldp, host: hostConfig } = req.app.locals const includeBody = req.method === 'GET' const negotiator = new Negotiator(req) const baseUri = utils.getFullUri(req) @@ -32,7 +32,7 @@ function handler (req, res, next) { res.header('MS-Author-Via', 'SPARQL') // Set live updates - if (ldp.live) { + if (hostConfig.live) { res.header('Updates-Via', utils.getBaseUri(req).replace(/^http/, 'ws')) } diff --git a/lib/ldp.js b/lib/ldp.js index cade52701..5fb0f006b 100644 --- a/lib/ldp.js +++ b/lib/ldp.js @@ -10,12 +10,12 @@ const utils = require('./utils') const error = require('./http-error') const stringToStream = require('./utils').stringToStream const serialize = require('./utils').serialize -const extend = require('extend') const rimraf = require('rimraf') const ldpContainer = require('./ldp-container') const parse = require('./utils').parse const fetch = require('node-fetch') const { promisify } = require('util') +const defaults = require('../config/defaults') const DEFAULT_CONTENT_TYPE = 'text/turtle' @@ -32,31 +32,51 @@ const RDF_MIME_TYPES = [ 'application/x-turtle' ] +/** + * @constructor + * @param [options={}] {Object} + * @param [options.serverUri] + * @param [options.dbPath] + * @param [options.configPath] + * @param [options.root] + * @param [options.suffixAcl] + * @param [options.suffixMeta] + * @param [options.errorPages] + * @param [options.fileBrowser] + * @param [options.suppressDataBrowser] + * @param [options.dataBrowserPath] + * @param [options.webid] + * @param [options.multiuser] + */ class LDP { - constructor (argv = {}) { - extend(this, argv) + constructor (options = {}) { + options = Object.assign({}, defaults, options) + this.serverUri = options.serverUri + this.dbPath = options.dbPath + this.configPath = options.configPath + this.suffixAcl = options.suffixAcl + this.suffixMeta = options.suffixMeta + this.multiuser = options.multiuser + this.webid = options.webid + this.strictOrigin = options.strictOrigin + this.errorPages = options.errorPages + this.noErrorPages = options.noErrorPages + this.errorHandler = options.errorHandler + this.dataBrowserPath = options.dataBrowserPath + this.suppressDataBrowser = options.suppressDataBrowser - // Setting root - if (!this.root) { - this.root = process.cwd() - } - if (!this.root.endsWith('/')) { - this.root += '/' - } - - // Suffixes - if (!this.suffixAcl) { - this.suffixAcl = '.acl' - } - if (!this.suffixMeta) { - this.suffixMeta = '.meta' - } this.turtleExtensions = [ '.ttl', this.suffixAcl, this.suffixMeta ] - // Error pages folder + this.root = options.root || process.cwd() + if (!this.root.endsWith('/')) { this.root += '/' } + + this.initErrorPages(options) + } + + initErrorPages (options) { this.errorPages = null if (!this.noErrorPages) { - this.errorPages = argv.errorPages + this.errorPages = options.errorPages if (!this.errorPages) { // TODO: For now disable error pages if errorPages parameter is not explicitly passed this.noErrorPages = true @@ -64,29 +84,6 @@ class LDP { this.errorPages += '/' } } - - if (this.skin !== false) { - this.skin = true - } - - if (this.corsProxy && this.corsProxy[ 0 ] !== '/') { - this.corsProxy = '/' + this.corsProxy - } - - debug.settings('Server URI: ' + this.serverUri) - debug.settings('Auth method: ' + this.auth) - debug.settings('Db path: ' + this.dbPath) - debug.settings('Config path: ' + this.configPath) - debug.settings('Suffix Acl: ' + this.suffixAcl) - debug.settings('Suffix Meta: ' + this.suffixMeta) - debug.settings('Filesystem Root: ' + this.root) - debug.settings('Allow WebID authentication: ' + !!this.webid) - debug.settings('Live-updates: ' + !!this.live) - debug.settings('Multi-user: ' + !!this.multiuser) - debug.settings('Suppress default data browser app: ' + this.suppressDataBrowser) - debug.settings('Default data browser app file path: ' + this.dataBrowserPath) - - return this } stat (file, callback) { diff --git a/lib/models/solid-host.js b/lib/models/solid-host.js index 663546f62..012a5cc75 100644 --- a/lib/models/solid-host.js +++ b/lib/models/solid-host.js @@ -24,6 +24,8 @@ class SolidHost { this.parsedUri = url.parse(this.serverUri) this.host = this.parsedUri.host this.hostname = this.parsedUri.hostname + + this.live = options.live } /** diff --git a/lib/server-config.js b/lib/server-config.js index a7dbba6fd..14220ee63 100644 --- a/lib/server-config.js +++ b/lib/server-config.js @@ -5,6 +5,30 @@ const fs = require('fs-extra') const path = require('path') +const debug = require('./debug') + +function printDebugInfo (options) { + debug.settings('Server URI: ' + options.serverUri) + debug.settings('Auth method: ' + options.auth) + debug.settings('Db path: ' + options.dbPath) + debug.settings('Config path: ' + options.configPath) + debug.settings('Suffix Acl: ' + options.suffixAcl) + debug.settings('Suffix Meta: ' + options.suffixMeta) + debug.settings('Filesystem Root: ' + options.root) + debug.settings('Allow WebID authentication: ' + !!options.webid) + debug.settings('Live-updates: ' + !!options.live) + debug.settings('Multi-user: ' + !!options.multiuser) + debug.settings('Suppress default data browser app: ' + options.suppressDataBrowser) + debug.settings('Default data browser app file path: ' + options.dataBrowserPath) +} + +function hostConfigFor (argv) { + return { + port: argv.port, + serverUri: argv.serverUri, + live: argv.live + } +} /** * Ensures that a directory has been copied / initialized. Used to ensure that @@ -130,7 +154,9 @@ function initTemplateDirs (configPath) { module.exports = { ensureDirCopyExists, ensureWelcomePage, + hostConfigFor, initConfigDir, initDefaultViews, - initTemplateDirs + initTemplateDirs, + printDebugInfo } diff --git a/package.json b/package.json index df29a64b8..b8eda2e49 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,6 @@ "express": "^4.16.3", "express-handlebars": "^3.0.0", "express-session": "^1.15.6", - "extend": "^3.0.0", "from2": "^2.1.0", "fs-extra": "^2.1.0", "glob": "^7.1.1",