diff --git a/app.js b/app.js new file mode 100644 index 000000000..8ed19a1e1 --- /dev/null +++ b/app.js @@ -0,0 +1,84 @@ +/* Simple MetacatUI app module. + +You'll need node.js and express.js to run this. + +- Install dependencies with `npm install`. +- load with `const main = require("./app");` + +*/ + +const express = require("express"); +const path = require("path"); +const port = process.env.PORT || 3000; +const app = express(); +const fs = require("fs"); + +// Subdirectory where index.html and the rest are +const src_dir = "src"; + +/** + * Get the directories for the given path + * @param path + * @returns {*} + */ +function getDirectories(path) { + return fs.readdirSync(path).filter(function (file) { + return fs.statSync(path + '/' + file).isDirectory(); + }); +} + +/** + * Initialize the MetacatUI App with the base dir to the main + * source directory + * + * @param metacatuiBaseDir [Optional] base metacatui repo directory + * @param configJs [Optional] override /config/config.js + */ +function initializeApp(metacatuiBaseDir = null, configJs = null, ) { + + // Determine where metacatui source directory is + if (metacatuiBaseDir === null || metacatuiBaseDir === undefined) { + metacatuiBaseDir = __dirname; + } + + app.use('/loader.js', express.static(path.resolve(metacatuiBaseDir, src_dir, "loader.js"))); + + // Is /config/config/js to be overridden? + var overrideConfigJs = configJs !== null; + + // Map metacatui source directories + let metacatDirs = getDirectories(metacatuiBaseDir + "/" + src_dir) + for (let i in metacatDirs) { + + var metacatCurrentDir = path.resolve(metacatuiBaseDir, src_dir, metacatDirs[i]) + // Determine if we override the configuration file (/config/config.js) + if (metacatDirs[i] !== 'config' || !overrideConfigJs) { + console.log('Setting /' + metacatDirs[i]+ " to " + metacatCurrentDir) + app.use('/' + metacatDirs[i], express.static(metacatCurrentDir)) + } else if (metacatDirs[i] === 'config' || overrideConfigJs) { + console.log("Setting " + '/' + metacatDirs[i] + "/config.js to " + configJs) + app.use('/' + metacatDirs[i] + "/config.js", express.static(configJs)); + } + } + + app.get("*", function (request, response) { + response.sendFile(path.resolve(metacatuiBaseDir, src_dir, "index.html")); + }); + + + return app; +} + +/** + * Expose the `initializeApp` function. + */ +exports.initializeApp = initializeApp + +/** + * Expose the prototypes. + */ + +exports.express = express; +exports.app = app; +exports.srcDir = src_dir; +exports.port = port; \ No newline at end of file diff --git a/server.js b/server.js index ee97eed60..23b26c248 100644 --- a/server.js +++ b/server.js @@ -8,19 +8,10 @@ You'll need node.js and express.js to run this. You'll also likely want to edit index.html and loader.js as needed. See README.md for more details. */ +'use strict'; -const express = require("express"); -const path = require("path"); -const port = process.env.PORT || 3000; -const app = express(); +const main = require("./app"); +const app = main.initializeApp() -// Subdirectory where index.html and the rest are -const src_dir = "src"; - -app.use(express.static(__dirname + "/" + src_dir)); -app.get("*", function(request, response) { - response.sendFile(path.resolve(__dirname, src_dir, "index.html")); -}); -app.listen(port); - -console.log("Now running at http://localhost:" + port); +app.listen(main.port) +console.log("Now running at http://localhost:" + main.port); \ No newline at end of file