From ec52ee2c2ad3fb89ac4ac92f597b6c00130f1070 Mon Sep 17 00:00:00 2001 From: Roman Timashev Date: Fri, 25 Mar 2016 13:36:00 +0300 Subject: [PATCH] Instagram Service update --- services/Instagram/.gitignore | 1 + services/Instagram/deploy.sh | 9 ++++ services/Instagram/install.sh | 1 + services/Instagram/nginx.conf | 4 +- services/Instagram/package.json | 13 +++-- services/Instagram/src/config.js | 23 +++++--- services/Instagram/src/core/update.js | 6 --- services/Instagram/src/index.js | 60 +++++++++++++++++++-- services/Instagram/src/main.js | 44 --------------- services/Instagram/src/routers/callback.js | 4 +- services/Instagram/src/routers/index.js | 11 ++-- services/Instagram/src/server.js | 8 ++- services/Instagram/src/templates/index.html | 2 +- services/Instagram/webpack.config.babel.js | 9 ++-- 14 files changed, 120 insertions(+), 75 deletions(-) delete mode 100644 services/Instagram/src/main.js diff --git a/services/Instagram/.gitignore b/services/Instagram/.gitignore index 9e9159fe..4876777d 100644 --- a/services/Instagram/.gitignore +++ b/services/Instagram/.gitignore @@ -7,4 +7,5 @@ npm-debug.log /node_modules /build +/out /coverage diff --git a/services/Instagram/deploy.sh b/services/Instagram/deploy.sh index 31ffa7ce..083b65dc 100755 --- a/services/Instagram/deploy.sh +++ b/services/Instagram/deploy.sh @@ -8,6 +8,11 @@ server="art.shburg.org" tarfile="build.tar.gz" tarpath=$TMPDIR${tarfile} destpath="~/www/art.shburg.org/services/instagram" +private=".private" + +echo "Building..." +NODE_ENV=production npm run webpack +npm run build echo "Packing..." env GZIP=-9 tar -czf ${tarpath} \ @@ -16,6 +21,8 @@ env GZIP=-9 tar -czf ${tarpath} \ --exclude=.git \ --exclude=.idea \ --exclude=.DS_Store \ + --exclude=.private* \ + --exclude=src \ --exclude=deploy.sh \ --exclude=install.sh \ . @@ -26,6 +33,8 @@ echo "Build file: $tarpath" scp ${tarpath} hoster@${server}:${destpath} echo "Build file successfully uploaded on ${server}" +scp ${private} hoster@${server}:${destpath} + echo "Installing..." ssh hoster@${server} "bash -s" -- < ./install.sh ${destpath} ${tarfile} diff --git a/services/Instagram/install.sh b/services/Instagram/install.sh index faf68545..a65c7432 100755 --- a/services/Instagram/install.sh +++ b/services/Instagram/install.sh @@ -2,6 +2,7 @@ dir_path="$1" build_file="$2" +script="out/index.js" cd ${dir_path} gunzip -c ${build_file} | tar xopf - diff --git a/services/Instagram/nginx.conf b/services/Instagram/nginx.conf index d63efe8e..2fbf035c 100644 --- a/services/Instagram/nginx.conf +++ b/services/Instagram/nginx.conf @@ -9,8 +9,8 @@ server { ssl_prefer_server_ciphers on; ssl_ciphers AES256+EECDH:AES256+EDH:!aNULL; - location /api/instagram { - proxy_pass http://hudozka_instagram; + location /service/instagram { + proxy_pass http://hudozka_instagram/; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; diff --git a/services/Instagram/package.json b/services/Instagram/package.json index 6dfc3742..7c51acd9 100644 --- a/services/Instagram/package.json +++ b/services/Instagram/package.json @@ -3,13 +3,12 @@ "version": "0.0.0", "private": true, "scripts": { - "webpack": "webpack" + "webpack": "webpack", + "start": "babel-node src/index.js", + "build": "babel src -d out" }, "dependencies": { - "babel-core": "^6.7.2", "babel-polyfill": "^6.7.2", - "babel-preset-es2015": "^6.6.0", - "babel-preset-stage-0": "^6.5.0", "co": "^4.6.0", "ejson": "^2.1.2", "instagram-node": "^0.5.8", @@ -19,6 +18,7 @@ "koa-bodyparser": "^3.0.0", "koa-compose": "^3.1.0", "koa-convert": "^1.2.0", + "koa-mount": "^2.0.0", "koa-route": "^3.0.0", "koa-session-mongo": "^1.1.0", "koa-session-store": "^1.1.1", @@ -28,9 +28,14 @@ "react": "^0.14.7", "react-dom": "^0.14.7", "react-router": "^2.0.1", + "request-promise": "^2.0.1", "thunkify": "^2.1.2" }, "devDependencies": { + "babel-core": "^6.7.2", + "babel-preset-es2015": "^6.6.0", + "babel-preset-stage-0": "^6.5.0", + "babel-cli": "^6.6.5", "babel-loader": "^6.2.4", "babel-preset-react": "^6.5.0", "html-webpack-plugin": "^2.10.0", diff --git a/services/Instagram/src/config.js b/services/Instagram/src/config.js index d762a042..9cb0b435 100644 --- a/services/Instagram/src/config.js +++ b/services/Instagram/src/config.js @@ -1,16 +1,26 @@ +import path from 'path'; import {readFileSync} from 'fs'; const env = process.env['NODE_ENV'] || 'production'; const isProduction = env === 'production'; -let privatePath = process.env['PRIVATE'] || '../.private'; +let privatePath = path.join( + __dirname, + process.env['PRIVATE'] || '../.private' +); let privateData = JSON.parse( readFileSync(privatePath, 'utf-8') ); export const port = isProduction ? 18000 : 3000; -export const pathPrefix = isProduction ? '/service/instagram' : ''; -export const indexPath = '../build/main.html'; +// export const pathPrefix = isProduction ? '/service/instagram' : ''; +export const pathPrefix = ''; +export const indexPath = path.join( + __dirname, + './templates/index.html' + // '../build/main.html' +); +// export const indexPath = '../build/main.html'; export const clientId = privateData['clientId']; export const clientSecret = privateData['clientSecret']; @@ -43,8 +53,9 @@ export const database = { host: 'localhost', port: 27017, db: 'hudozka', - username: '', - password: '' + username: privateData['databaseUser'], + password: privateData['databasePassword'] }; -export const databaseUri = `mongodb://${database.host}:${database.port}/${database.db}`; +const databaseAuth = database.username && database.password ? `${database.username}:${database.password}@` : ''; +export const databaseUri = `mongodb://${databaseAuth}${database.host}:${database.port}/${database.db}`; diff --git a/services/Instagram/src/core/update.js b/services/Instagram/src/core/update.js index 1107d43c..5a999e85 100644 --- a/services/Instagram/src/core/update.js +++ b/services/Instagram/src/core/update.js @@ -34,8 +34,6 @@ export async function updateSubscription(callbackUrl) { let data = await instagram.subscriptions.list(); let activeSubscriptions = data.result; - console.log(data); - let objectSubscriptions = activeSubscriptions.filter(i => i.object === 'user' && !i.object_id && i.callback_url === callbackUrl) if (!objectSubscriptions.length) { @@ -65,10 +63,6 @@ export async function updateSubscription(callbackUrl) { // limit: 5000 // } await instagram.subscriptions.subscribeUser(callbackUrl); - // let data = await instagram.subscriptions.subscribeTag('love', callbackUrl); - console.log(data); - - return data; } } diff --git a/services/Instagram/src/index.js b/services/Instagram/src/index.js index 3f3cb90e..19c188f7 100644 --- a/services/Instagram/src/index.js +++ b/services/Instagram/src/index.js @@ -1,3 +1,57 @@ -require('babel-core/register'); -require('babel-polyfill'); -require('./main'); +import 'babel-polyfill'; + +import {databaseUri, port, updateSubscriptionDelay, updateSubscriptionCallbackUrl} from './config' +import {connect} from './core/db'; +import server from './server'; +// import update from './core/update'; +import {updateSubscription, clearSubscriptions} from './core/update'; + +let repeat = time => fn => setInterval(fn, time); + +function main(){ + async function loop(){ + try{ + await connect(databaseUri); + }catch(error){ + console.log(error); + } + + try{ + let app = server(); + app.listen(port); + console.log(`Start listening ${port}`); + }catch(error){ + console.log(error); + } + + await clearSubscriptions(); + + await updateSubscription(updateSubscriptionCallbackUrl); + repeat(updateSubscriptionDelay)(() => { + updateSubscription(updateSubscriptionCallbackUrl); + }); + } + + loop(); +} + +main(); + +// .then(() => { +// return update('hudozka', { +// "changed_aspect": "media", +// "object": "tag", +// "object_id": "love", +// // "time": 1458675620, +// "time": 1458594000, +// "subscription_id": 22487204, +// "data": {} +// }) +// .catch(e => { +// console.log(e); +// }) +// .then(result => { +// console.log('update done'); +// console.log(result); +// }); +// }) diff --git a/services/Instagram/src/main.js b/services/Instagram/src/main.js deleted file mode 100644 index 433f3c55..00000000 --- a/services/Instagram/src/main.js +++ /dev/null @@ -1,44 +0,0 @@ -import {databaseUri, port, updateSubscriptionDelay, updateSubscriptionCallbackUrl} from './config' -import {connect} from './core/db'; -import server from './server'; -import update from './core/update'; -import {updateSubscription, clearSubscriptions} from './core/update'; - -connect(databaseUri) - .catch(error => { - console.log(error); - }) - .then(() => { - let app = server(); - app.listen(port); - }) - .then(() => { - console.log(`Start listening ${port}`); - }) - .then(clearSubscriptions) - .then(() => { - let repeat = time => fn => setInterval(fn, time); - - updateSubscription(updateSubscriptionCallbackUrl); - repeat(updateSubscriptionDelay)(() => { - updateSubscription(updateSubscriptionCallbackUrl); - }); - }); - // .then(() => { - // return update('hudozka', { - // "changed_aspect": "media", - // "object": "tag", - // "object_id": "love", - // // "time": 1458675620, - // "time": 1458594000, - // "subscription_id": 22487204, - // "data": {} - // }) - // .catch(e => { - // console.log(e); - // }) - // .then(result => { - // console.log('update done'); - // console.log(result); - // }); - // }) diff --git a/services/Instagram/src/routers/callback.js b/services/Instagram/src/routers/callback.js index e7a3a72b..1a9a3474 100644 --- a/services/Instagram/src/routers/callback.js +++ b/services/Instagram/src/routers/callback.js @@ -3,7 +3,7 @@ import update from '../core/update'; export function callbackGet() { return get('/callback/:id', async (ctx, id) => { - console.log('challenge', id); + // console.log('challenge', id); if(ctx.query['hub.mode'] == 'subscribe'){ ctx.body = ctx.query['hub.challenge']; @@ -16,7 +16,7 @@ export function callbackGet() { export function callbackPost() { return post('/callback/:id', async (ctx, id) => { let body = ctx.request.body; - console.log(id, body); + // console.log(id, body); update(id, body); ctx.status = 200; diff --git a/services/Instagram/src/routers/index.js b/services/Instagram/src/routers/index.js index a918ebb1..35e40692 100644 --- a/services/Instagram/src/routers/index.js +++ b/services/Instagram/src/routers/index.js @@ -30,13 +30,18 @@ export function routers() { } export const url = path => `${root}${path}`; +// export const url = path => path; export const get = (path, fn) => route['get'](url(path), fn); export const post = (path, fn) => route['post'](url(path), fn); export function index(fn){ - return async function (ctx){ - ctx.type = 'text/html'; - ctx.body = await fs.createReadStream(indexPath); + return async (ctx) => { + try{ + ctx.type = 'text/html'; + ctx.body = await fs.createReadStream(indexPath); + }catch(e){ + console.log(e); + } } } diff --git a/services/Instagram/src/server.js b/services/Instagram/src/server.js index fe139491..b673877f 100644 --- a/services/Instagram/src/server.js +++ b/services/Instagram/src/server.js @@ -1,4 +1,5 @@ import Koa from 'koa'; +import mount from 'koa-mount'; import serve from 'koa-static'; import convert from 'koa-convert'; import sessionStore from 'koa-session-store'; @@ -8,7 +9,7 @@ import bodyParser from 'koa-bodyparser'; import {db} from './core/db'; import instagram from './middlewares/instagram'; import {routers, statelessRouters} from './routers'; -import {sessionSecret, sessionExpirationTime} from './config'; +import {pathPrefix, sessionSecret, sessionExpirationTime} from './config'; export default function(){ const app = new Koa(); @@ -24,6 +25,11 @@ export default function(){ app.use(error()); return app; + + // const root = new Koa(); + // root.use(mount(pathPrefix, app)); + + // return root; } function session(name, exp){ diff --git a/services/Instagram/src/templates/index.html b/services/Instagram/src/templates/index.html index 166bab25..9e190052 100644 --- a/services/Instagram/src/templates/index.html +++ b/services/Instagram/src/templates/index.html @@ -2,7 +2,7 @@ - Title + Instagram Control
diff --git a/services/Instagram/webpack.config.babel.js b/services/Instagram/webpack.config.babel.js index d37b77d8..1c226d0c 100644 --- a/services/Instagram/webpack.config.babel.js +++ b/services/Instagram/webpack.config.babel.js @@ -1,11 +1,14 @@ import webpack from'webpack'; -import HtmlWebpackPlugin from 'html-webpack-plugin'; +import HTML from 'html-webpack-plugin'; + +const env = process.env['NODE_ENV'] || 'production'; +const isProduction = env === 'production'; // import autoprefixer from 'autoprefixer'; module.exports = { entry: './src/app.js', - watch: true, + watch: !isProduction, //resolve: { // modulesDirectories: [ // './node_modules' @@ -25,7 +28,7 @@ module.exports = { // }, plugins: [ // new webpack.HotModuleReplacementPlugin(), - new HtmlWebpackPlugin({ + new HTML({ filename: 'main.html', title: 'Instagram', template: 'src/templates/index.html',