Commit efab93dd authored by Aral Balkan's avatar Aral Balkan

Add data file existence states to globals and use them in index.js

parent 06f07519
......@@ -3,6 +3,7 @@
const path = require('path')
const os = require('os')
const fs = require('fs-extra')
module.exports = function (app) {
const dataDirectoryPath = path.join(os.homedir(), '.indie', 'site')
......@@ -10,8 +11,23 @@ module.exports = function (app) {
const ownerKeysFilePath = path.join(dataDirectoryPath, 'owner-keys.json')
const ownerSettingsFilePath = path.join(dataDirectoryPath, 'owner-settings.json')
// Ensure that the data directory path exists as other parts of the application
// will rely on it being there.
app.set('dataDirectoryPath', dataDirectoryPath)
app.set('serverSecretFilePath', serverSecretFilePath)
app.set('ownerKeysFilePath', ownerKeysFilePath)
app.set('ownerSettingsFilePath', ownerSettingsFilePath)
// Flag whether the various settings files exist or not so that other parts of
// the application can easily query their state (and, if necessary, update it).
const serverSecretFileExists = fs.existsSync(serverSecretFilePath)
const ownerKeysFileExists = fs.existsSync(ownerKeysFilePath)
const ownerSettingsFileExists = fs.existsSync(ownerSettingsFilePath)
app.set('serverSecretFileExists', serverSecretFileExists)
app.set('ownerKeysFileExists', ownerKeysFileExists)
app.set('ownerSettingsFileExists', ownerSettingsFileExists)
/* eslint-disable no-console */
const logger = require('winston')
const app = require('./app')
const portFromConfiguration = app.get('port')
const fs = require('fs-extra')
const sodium = require('libsodium-wrappers')
const { argv } = require('yargs')
const app = require('./app')
// Set the port:
// 1. Via commandline override (--port) if present
// 2. Via environment variable if present
// 3. Via the Feathers configuration file (fallback)
const portFromConfiguration = app.get('port')
const port = argv.port || process.env.PORT || portFromConfiguration
// Start the Feathers (API; REST + sockets) server.
......@@ -26,20 +23,11 @@ function startFeathersServer () {
// File-based data (e.g., configuration, etc.) is saved at ~/.indie/site
const dataDirectoryPath = app.get('dataDirectoryPath')
const serverSecretFilePath = app.get('serverSecretFilePath')
// On first run of the server only, create a server secret. This
// server secret is used to sign the JSON Web Tokens that we use
// for stateless authentication.
async function createAndPersitServerSecretIfItDoesntAlreadyExist () {
// Check for existence of the file synchronously so as to avoid a possible (if
// highly inprobable) race condition. Since this happens on server start, it
// will not have an impact on performance.
const serverSecretFileExists = fs.existsSync(serverSecretFilePath)
if (!serverSecretFileExists) {
if (!app.get('serverSecretFileExists')) {
// Server secret does not exist. This should be the first time the server
// is being run. Create and save the server.
......@@ -56,8 +44,7 @@ async function createAndPersitServerSecretIfItDoesntAlreadyExist () {
// Write the keys file to a data file in a directory for this spike that
// we place in the home directory of the account that this spike is
// running under.
await fs.ensureDir(dataDirectoryPath)
await fs.writeJson(serverSecretFilePath, serverSecretObject)
await fs.writeJson(app.get('serverSecretFilePath'), serverSecretObject)
} catch (error) {
throw new Error('Could not persist the server secret.', error)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment