Skip to content

Commit

Permalink
added config file
Browse files Browse the repository at this point in the history
  • Loading branch information
pan261 committed Nov 30, 2021
1 parent 79da169 commit f5e309d
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 75 deletions.
33 changes: 33 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Configures constants used in the other parts of this app
*/

require('dotenv').config();
const ForgeAuthClient = require('forge-apis').AuthClientThreeLegged;

const scopes = ['bucket:create', 'bucket:read', 'data:read', 'data:create', 'data:write'];

const authClient = new ForgeAuthClient(
process.env.FORGE_CLIENT_ID,
process.env.FORGE_CLIENT_SECRET,
process.env.FORGE_CALLBACK_URL,
scopes,
true
);

const folderIdMap = {
gantry: "GANTRY_FOLDER_ID",
lathe: "LATHE_FOLDER_ID",
mill: "MILL_FOLDER_ID",
waterjet: "WATERJET_FOLDER_ID"
};

module.exports = {
authClient: authClient,
scopes: scopes,
folderIdMap: folderIdMap,
port: process.env.PORT,
forgeClientId: process.env.FORGE_CLIENT_ID,
forgeClientSecret: process.env.FORGE_CLIENT_SECRET,
forgeCallbackURL: process.env.FORGE_CALLBACK_URL
}
50 changes: 8 additions & 42 deletions downloader.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,28 @@
const axios = require('axios');
const fs = require('fs').promises;
const ForgeSDK = require('forge-apis');
require('dotenv').config();

const config = require('./config');

const ObjectsApi = new ForgeSDK.ObjectsApi();

const scopes = ['bucket:create', 'bucket:read', 'data:read', 'data:create', 'data:write'];

const authClient = new ForgeSDK.AuthClientThreeLegged(
process.env.FORGE_CLIENT_ID,
process.env.FORGE_CLIENT_SECRET,
process.env.FORGE_CALLBACK_URL,
scopes,
true
);


/**
* Kevin Pan | pan261@purdue.edu | Last Modified: 11/22/2021
* Kevin Pan | pan261@purdue.edu | Last Modified: 11/30/2021
*
* Downloads a file by making a GET request on the storageUrl and then
* saves the file as `filename` under the `destination` directory
* @param {String} storageUrl
* Downloads a file by using the Forge SDK to make a GET request
* on the storageLocation and then saves the file as `filename` under the `destination` directory
* @param {String} storageLocation
* @param {String} fileName
* @param {String} destination
* @param {Object} credentials
*/
exports.download = (storageUrl, fileName, destination, credentials) => {
// var file = fs.createWriteStream(`./${destination}/${fileName}`);

// await axios({
// method: 'GET',
// url: storageUrl,
// headers: {
// Authorization: `Bearer ${credentials.access_token}`
// }
// })
// .then(res => {return res.data})
// .then(async res => {
// // console.log(res.data);
// // fs.writeFile(`./${destination}/${fileName}`, res.data, 'binary').then(() => {
// // console.log("finished downloading", fileName);
// // });
// fs.WriteStream(`./${destination}/${fileName}`).write(res);
// })
// .catch(err => {
// console.log("error while processing " + fileName);
// console.log(err);
// });
exports.download = (storageLocation, fileName, destination, credentials) => {

ObjectsApi.getObject('wip.dm.prod', storageUrl, {}, authClient, credentials)
ObjectsApi.getObject('wip.dm.prod', storageLocation, {}, config.authClient, credentials)
.then((res) => {
console.log(res)
fs.writeFile(`./${destination}/${fileName}`, res.body)
})
.catch((err) => {
console.log("error while processing " + storageUrl);
console.log("error while processing " + storageLocation);
console.log(err);
});
}
Expand Down
28 changes: 9 additions & 19 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const express = require('express');
require('dotenv').config();

const ForgeSDK = require('forge-apis');
const config = require('./config');

/**
* Kevin Pan | pan261@purdue.edu | Last Modified: 11/22/2021
Expand All @@ -14,18 +12,10 @@ const ForgeSDK = require('forge-apis');
* - FORGE_CALLBACK_URL
* - PORT (defaults to 3000)
*
* These are all configured in config.js
*
*/

const scopes = ['bucket:create', 'bucket:read', 'data:read', 'data:create', 'data:write'];

const oAuth2ThreeLegged = new ForgeSDK.AuthClientThreeLegged(
process.env.FORGE_CLIENT_ID,
process.env.FORGE_CLIENT_SECRET,
process.env.FORGE_CALLBACK_URL,
scopes,
true
);

let credentials = null;
let refreshTime = null;
let intervalID = null;
Expand All @@ -42,9 +32,9 @@ let intervalID = null;
*/
const createServer = () => {
const app = express();
const PORT = process.env.PORT || 3000;
const PORT = config.port || 3000;

if (process.env.FORGE_CLIENT_ID == null || process.env.FORGE_CLIENT_SECRET == null) {
if (config.forgeClientId == null || config.forgeClientSecret == null) {
console.error('Missing FORGE_CLIENT_ID or FORGE_CLIENT_SECRET env. variables.');
return;
}
Expand All @@ -55,12 +45,12 @@ const createServer = () => {
'\x1b[96mserver.js::createServer:',
'\x1b[0m/auth endpoint called'
);
res.redirect(oAuth2ThreeLegged.generateAuthUrl());
res.redirect(config.authClient.generateAuthUrl());
});

// Endpoint Forge redirects to after consent screen
app.get('/callback', function (req, res) {
oAuth2ThreeLegged.getToken(req.query.code)
config.authClient.getToken(req.query.code)
.then((creds) => {
credentials = creds;
refreshTime = creds.expires_in - 300;
Expand Down Expand Up @@ -113,7 +103,7 @@ const createServer = () => {
res.status(err.statusCode).json(err);
});

const server = app.listen(PORT, () => { console.log(`Server listening on port ${PORT}`); });
const server = app.listen(config.port, () => { console.log(`Server listening on port ${PORT}`); });

return server;
}
Expand All @@ -122,7 +112,7 @@ const createServer = () => {
* Used internally to refresh tokens automatically
*/
const refresh = () => {
oAuth2ThreeLegged.refreshToken(credentials, scopes)
config.authClient.refreshToken(credentials, scopes)
.then(creds => {
credentials = creds;
console.log(
Expand Down
17 changes: 5 additions & 12 deletions taskRunner.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const axios = require('axios');
const watcher = require('./watcher');
const downloader = require('./downloader');
require('dotenv').config();
const config = require('./config');

/**
* Kevin Pan | pan261@purdue.edu | Last Modified: 11/22/2021
* Kevin Pan | pan261@purdue.edu | Last Modified: 11/30/2021
*
* This file will be responsible for managing the watcher
* as well as fetching the credentials from the server
Expand All @@ -18,14 +18,8 @@ const projectID = ""
// TODO: create map from cloud directories to local

// parse command line arguments from docker-compose to get folder ID
const folderIdMap = {
gantry: "GANTRY_FOLDER_ID",
lathe: "LATHE_FOLDER_ID",
mill: "MILL_FOLDER_ID",
waterjet: "WATERJET_FOLDER_ID"
};

const folderID = folderIdMap[process.argv[2]];
const folderID = config.folderIdMap[process.argv[2]];
console.log(folderID);

const adminProjectID = 'a.YnVzaW5lc3M6cHVyZHVlMjY5NiMyMDIxMTEwODQ2NDQzMzk3Ng';
Expand Down Expand Up @@ -108,14 +102,13 @@ const dispatchWatcher = async (interval) => {

for (index in items) {
// dispatch the downloader here
console.log(items[index].name);
downloader.download(items[index].url, items[index].name, 'download_tests', credentials);
downloader.download(items[index].location, items[index].name, 'download_tests', credentials);
}
};

getCredentials();

// There should be a buffer between how far back the watcher checks
// and how often it runs. We may need to test this out.
setInterval(dispatchWatcher, 5000);
setInterval(dispatchWatcher, 5000, 7000);

5 changes: 3 additions & 2 deletions watcher.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const axios = require('axios');

/**
* Kevin Pan | pan261@purdue.edu | Last Modified: 11/22/2021
* Kevin Pan | pan261@purdue.edu | Last Modified: 11/30/2021
*
* Watches given folder id in the project, logs any files changed since
* `interval` ms ago
Expand Down Expand Up @@ -69,7 +69,7 @@ exports.watch = (projectID, folderID, credentials, interval) => {
// where time does not match with db time

// index will have to change because not all items may need to be added to this list
changedItems[index] = {name: saveName, url: storageID};
changedItems[index] = {name: saveName, location: storageID};
// console.log(changedItems);
}

Expand All @@ -80,6 +80,7 @@ exports.watch = (projectID, folderID, credentials, interval) => {
'\x1b[91mwatcher.js::watch:',
'\x1b[0mError accessing Forge folder contents API'
);
return {};
});

return items;
Expand Down

0 comments on commit f5e309d

Please sign in to comment.