From 2f4fa6d9700d23bd2f4d0cae73fb27270b11bfbe Mon Sep 17 00:00:00 2001 From: bill bittner Date: Tue, 18 Jul 2017 09:52:18 -0700 Subject: [PATCH 1/4] added download directory retrieval to startup process --- controllers/serveController.js | 8 +++++--- helpers/libraries/lbryApi.js | 20 ++++++++++++++++++++ routes/api-routes.js | 7 ++----- server.js | 34 +++++++++++++++++----------------- 4 files changed, 44 insertions(+), 25 deletions(-) diff --git a/controllers/serveController.js b/controllers/serveController.js index a390a32c..2a5c34a5 100644 --- a/controllers/serveController.js +++ b/controllers/serveController.js @@ -192,8 +192,10 @@ module.exports = { getClaimByClaimId (name, claimId) { const deferred = new Promise((resolve, reject) => { let uri; - validateClaimId(name, claimId) // 1. validate the claim id & retrieve the full claim id if needed - .then(validClaimId => { // 2. check locally for the claim + // 1. validate the claim id & retrieve the full claim id if needed + validateClaimId(name, claimId) + .then(validClaimId => { + // 2. check locally for the claim logger.debug('valid claim id:', validClaimId); uri = `${name}#${validClaimId}`; return db.File.findOne({ where: { name, claimId: validClaimId } }); @@ -206,7 +208,7 @@ module.exports = { resolve(result.dataValues); // update the file, as needed updateFileIfNeeded(uri, name, claimId, result.dataValues.outpoint, result.dataValues.outpoint); - // 3. if a match was not found use the daemon to retrieve the claim & return the db data once it is created + // 3. if a match was not found locally, use the daemon to retrieve the claim & return the db data once it is created } else { logger.debug('No result found in File table'); lbryApi diff --git a/helpers/libraries/lbryApi.js b/helpers/libraries/lbryApi.js index cff3c346..c46d56e2 100644 --- a/helpers/libraries/lbryApi.js +++ b/helpers/libraries/lbryApi.js @@ -103,4 +103,24 @@ module.exports = { }); return deferred; }, + getDownloadDirectory () { + logger.debug('Retrieving the download directory path from lbry daemon...'); + const deferred = new Promise((resolve, reject) => { + axios + .post('http://localhost:5279/lbryapi', { + method: 'settings_get', + }) + .then(({ data }) => { + if (data.result) { + resolve(data.result.download_directory); + } else { + reject(new Error('Successfully connected to lbry daemon, but unable to retrieve the download directory.')); + } + }) + .catch((error) => { + reject(error); + }); + }); + return deferred; + }, }; diff --git a/routes/api-routes.js b/routes/api-routes.js index 5fb910dc..ad5ec602 100644 --- a/routes/api-routes.js +++ b/routes/api-routes.js @@ -7,13 +7,10 @@ const { createPublishParams, validateFile } = require('../helpers/libraries/publ const errorHandlers = require('../helpers/libraries/errorHandlers.js'); const { postToStats, sendGoogleAnalytics } = require('../controllers/statsController.js'); -const config = require('config'); -const hostedContentPath = config.get('Database.DownloadDirectory'); - -module.exports = app => { +module.exports = (app, hostedContentPath) => { // route to return a file directly app.get('/api/streamFile/:name', ({ params }, res) => { - const filePath = `${hostedContentPath}${params.name}`; + const filePath = `${hostedContentPath}/${params.name}`; res.status(200).sendFile(filePath); }); // route to run a claim_list request on the daemon diff --git a/server.js b/server.js index 0a8e2adf..0b0beead 100644 --- a/server.js +++ b/server.js @@ -6,8 +6,7 @@ const expressHandlebars = require('express-handlebars'); const Handlebars = require('handlebars'); const config = require('config'); const logger = require('winston'); - -const hostedContentPath = config.get('Database.DownloadDirectory'); +const { getDownloadDirectory } = require('./helpers/libraries/lbryApi'); // configure logging const logLevel = config.get('Logging.LogLevel'); @@ -82,25 +81,26 @@ const hbs = expressHandlebars.create({ app.engine('handlebars', hbs.engine); app.set('view engine', 'handlebars'); -// require express routes -require('./routes/api-routes.js')(app); -require('./routes/show-routes.js')(app); -require('./routes/serve-routes.js')(app); -require('./routes/home-routes.js')(app); - -// require socket.io routes -const server = require('./routes/sockets-routes.js')(app, siofu, hostedContentPath); - -// sync sequelize -// wrap the server in socket.io to intercept incoming sockets requests -// start server -db.sequelize.sync() - .then(() => { +// start the server +db.sequelize + .sync() // sync sequelize + .then(() => { // get the download directory from the daemon + logger.info('Retrieving daemon download directory'); + return getDownloadDirectory(); + }) + .then(hostedContentPath => { // require routes & wrap in socket.io + require('./routes/api-routes.js')(app, hostedContentPath); + require('./routes/show-routes.js')(app); + require('./routes/serve-routes.js')(app); + require('./routes/home-routes.js')(app); + return require('./routes/sockets-routes.js')(app, siofu, hostedContentPath); + }) + .then(server => { // start the server server.listen(PORT, () => { logger.info('Trusting proxy?', app.get('trust proxy')); logger.info(`Server is listening on PORT ${PORT}`); }); }) .catch((error) => { - logger.log('Error syncing sequelize db:', error); + logger.error('Startup Error >>', error); }); From baa580ac87abfe70dda62bb47b09d1cff4c292eb Mon Sep 17 00:00:00 2001 From: bill bittner Date: Tue, 18 Jul 2017 09:54:24 -0700 Subject: [PATCH 2/4] removed DownloadDirectory from config vars --- config/default.json | 3 +-- config/development.json | 3 +-- config/production.json | 3 +-- config/test.json | 3 +-- 4 files changed, 4 insertions(+), 8 deletions(-) diff --git a/config/default.json b/config/default.json index 11140fef..d3bc0e0d 100644 --- a/config/default.json +++ b/config/default.json @@ -6,8 +6,7 @@ "GoogleId": "none" }, "Database": { - "MySqlConnectionUri": "none", - "DownloadDirectory": "none" + "MySqlConnectionUri": "none" }, "Logging": { "LogLevel": "none" diff --git a/config/development.json b/config/development.json index d3472a68..e21d54ea 100644 --- a/config/development.json +++ b/config/development.json @@ -6,8 +6,7 @@ "GoogleId": "UA-100747990-1" }, "Database": { - "MySqlConnectionUri": "none", - "DownloadDirectory": "/home/ubuntu/Downloads/" + "MySqlConnectionUri": "none" }, "Logging": { "LogLevel": "silly" diff --git a/config/production.json b/config/production.json index d5f9e903..34d9bfca 100644 --- a/config/production.json +++ b/config/production.json @@ -6,8 +6,7 @@ "GoogleId": "UA-60403362-3" }, "Database": { - "MySqlConnectionUri": "none", - "DownloadDirectory": "/home/lbry/Downloads/" + "MySqlConnectionUri": "none" }, "Logging": { "LogLevel": "verbose" diff --git a/config/test.json b/config/test.json index 22e9f419..f77b0219 100644 --- a/config/test.json +++ b/config/test.json @@ -6,8 +6,7 @@ "GoogleId": "UA-100747990-1" }, "Database": { - "MySqlConnectionUri": "none", - "DownloadDirectory": "/home/ubuntu/Downloads/" + "MySqlConnectionUri": "none" }, "Logging": { "LogLevel": "debug" From f3ce7957f392a6279bf49bdb3460c2469947b5ee Mon Sep 17 00:00:00 2001 From: bill bittner Date: Tue, 18 Jul 2017 09:59:40 -0700 Subject: [PATCH 3/4] small cosmetics in server.js --- server.js | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/server.js b/server.js index 0b0beead..0633a23a 100644 --- a/server.js +++ b/server.js @@ -8,17 +8,14 @@ const config = require('config'); const logger = require('winston'); const { getDownloadDirectory } = require('./helpers/libraries/lbryApi'); +const PORT = 3000; // set port +const app = express(); // create an Express application +const db = require('./models'); // require our models for syncing + // configure logging const logLevel = config.get('Logging.LogLevel'); require('./config/loggerSetup.js')(logger, logLevel); -// set port -const PORT = 3000; -// create an Express application -const app = express(); -// require our models for syncing -const db = require('./models'); - // serve static files from public directory (css/js/img) app.use(express.static(`${__dirname}/public`)); @@ -32,7 +29,7 @@ app.use((req, res, next) => { // logging middleware next(); }); -// configure handlebars & register it with Express app +// configure handlebars & register it with express app const hbs = expressHandlebars.create({ defaultLayout: 'main', // sets the default layout handlebars : Handlebars, // includes basic handlebars for access to that library From 5904a05fded3e9b2ce0e3024597be3249b54ad2a Mon Sep 17 00:00:00 2001 From: bill bittner Date: Tue, 18 Jul 2017 10:02:09 -0700 Subject: [PATCH 4/4] renamed server.js to speech.js --- README.md | 6 +++--- package.json | 4 ++-- server.js => speech.js | 0 3 files changed, 5 insertions(+), 5 deletions(-) rename server.js => speech.js (100%) diff --git a/README.md b/README.md index 6fe512a1..41da66f1 100644 --- a/README.md +++ b/README.md @@ -13,9 +13,9 @@ spee.ch is a single-serving site that reads and publishes images to and from the * clone this repo * customize `config/develpment.json` by replacing the value of `Database.DownloadDirectory` with a string representing the local path where you want uploaded files to be stored. * run `npm install` -* to start the server, from your command line run `node server.js` while passing three environmental variables: your lbry wallet address (`LBRY_CLAIM_ADDRESS`), your mysql connection uri (`MYSQL_CONNECTION_STRING`), and the environment to run (`NODE_ENV`). - * i.e. `LBRY_CLAIM_ADDRESS= MYSQL_CONNECTION_STRING= NODE_ENV=development node server.js` - * e.g. `LBRY_CLAIM_ADDRESS=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX MYSQL_CONNECTION_STRING=mysql://root:XXXXXX@localhost:3306/lbry NODE_ENV=development node server.js` +* to start the server, from your command line run `node speech.js` while passing three environmental variables: your lbry wallet address (`LBRY_CLAIM_ADDRESS`), your mysql connection uri (`MYSQL_CONNECTION_STRING`), and the environment to run (`NODE_ENV`). + * i.e. `LBRY_CLAIM_ADDRESS= MYSQL_CONNECTION_STRING= NODE_ENV=development node speech.js` + * e.g. `LBRY_CLAIM_ADDRESS=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX MYSQL_CONNECTION_STRING=mysql://root:XXXXXX@localhost:3306/lbry NODE_ENV=development node speech.js` * To run hot, use `nodemon` instead of `node` * visit [localhost:3000](http://localhost:3000) diff --git a/package.json b/package.json index b4706565..63aebbbf 100644 --- a/package.json +++ b/package.json @@ -2,10 +2,10 @@ "name": "spee.ch", "version": "0.0.1", "description": "a single-serving site that reads and publishes images to and from the LBRY blockchain", - "main": "server.js", + "main": "speech.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", - "start": "node server.js", + "start": "node speech.js", "lint": "eslint .", "fix": "eslint . --fix", "precommit": "eslint ." diff --git a/server.js b/speech.js similarity index 100% rename from server.js rename to speech.js