Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

standard format #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
{
"name": "is-now-illegal",
"version": "1.0.0",
"description": "A NERD protest against Thrump's Immigration ban!",
"description": "A NERD protest against Trump's Immigration ban!",
"main": "index.js",
"cacheDirectories": ["node_modules", "share/node_modules"],
"cacheDirectories": [
"node_modules",
"share/node_modules"
],
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server",
"heroku-prebuild": "cd share && npm install --dev && npm run build"
"heroku-prebuild": "cd share && npm install --dev && npm run build",
"lint": "standard --fix"
},
"repository": {
"type": "git",
Expand All @@ -28,5 +32,8 @@
"firebase-queue": "^1.6.1",
"lodash": "^4.17.4",
"python-shell": "^0.4.0"
},
"devDependencies": {
"standard": "^8.6.0"
}
}
14 changes: 7 additions & 7 deletions server/GifGenerator.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
var path = require('path')
var PythonShell = require('python-shell');
var PythonShell = require('python-shell')

exports.generateWithWord = function (word, output, next) {
let execOptions = {
scriptPath: path.join(__dirname , '../rotoscope'),
args: [word, path.join(__dirname , '../GIF/Trump'), output]
};
let execOptions = {
scriptPath: path.join(__dirname, '../rotoscope'),
args: [word, path.join(__dirname, '../GIF/Trump'), output]
}

PythonShell.run('generate.py', execOptions, next)
}
PythonShell.run('generate.py', execOptions, next)
}
25 changes: 13 additions & 12 deletions server/PathLoader.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';
var fs = require('fs');
var path = require('path');
'use strict'
var fs = require('fs')
var path = require('path')

//
// Loads all files and returns in an object-form like:
Expand All @@ -10,20 +10,21 @@ var path = require('path');
// }
//

exports.load = function loadModules (dir, loaded){
exports.load = function loadModules (dir, loaded) {
// Default to creat a new object
loaded = (typeof loaded) == 'object' ? loaded : {};
loaded = (typeof loaded) === 'object' ? loaded : {}

// Read all files from that path and load into modules
fs.readdirSync(dir).forEach(function (file) {
if (file.indexOf('.js') < 0)
return;
if (file.indexOf('.js') < 0) {
return
}

var mod = require(path.join(dir, file));
var name = path.basename(file, '.js');
var mod = require(path.join(dir, file))
var name = path.basename(file, '.js')

loaded[name] = mod;
});
loaded[name] = mod
})

return loaded;
return loaded
}
62 changes: 14 additions & 48 deletions server/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
const fs = require('fs')
const path = require('path')
const async = require('async')
const express = require('express');
const express = require('express')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused libs


const Queue = require('firebase-queue')
const admin = require('firebase-admin')
const storage = require('@google-cloud/storage');
const storage = require('@google-cloud/storage')

const PathLoader = require('./PathLoader')
const GifGenerator = require('./GifGenerator')

// Expose global app object
var app = {}
Expand All @@ -20,8 +17,7 @@ var credentials = require(credentialsPath)

// Initialize tmp path
app.TMP_PATH = path.join(__dirname, '../tmp')
if (!fs.existsSync(app.TMP_PATH))
fs.mkdirSync(app.TMP_PATH)
fs.existsSync(app.TMP_PATH) || fs.mkdirSync(app.TMP_PATH)

// Initialize Firebase connection
app.admin = admin
Expand All @@ -37,58 +33,28 @@ app.admin.initializeApp({
app.gcs = storage({
projectId: credentials.project_id,
keyFilename: credentialsPath
});

})

//
//
// Start HTTP server
//
app.express = express();
//
app.express = express()

app.express.get('/', (req, res) => {
res.status(200).send('home');
});
res.status(200).send('home')
})

app.express.get('/_ah/health', (req, res) => {
res.status(200).send('OK '+Date.now());
});
res.status(200).send('OK ' + Date.now())
})

const PORT = process.env.PORT || 8080;
const PORT = process.env.PORT || 8080
app.express.listen(PORT, () => {
console.log(`App listening on port ${PORT}`);
});


//
// Start Firebase Queue processing
//
console.log(`App listening on port ${PORT}`)
})

// Load tasks
app.TASKS = PathLoader.load(path.join(__dirname, 'tasks'))

// Load Firebase Queue
app.queueRef = app.admin.database().ref('queue')

var queueOptions = {
numWorkers: 3,
}
var queue = new Queue(app.queueRef, queueOptions, handleQueueTask)
function handleQueueTask(data, progress, resolve, reject) {
// Check if task type exists
if ( !(data.task in app.TASKS)) {
console.log(`Task [${data.task}] does not exist`)
return reject()
}

// Load task to execute
let task = app.TASKS[data.task]

// Delegate call
task(...arguments)
};

// Log server start
console.log('Queue worker is alive')

// Test
// handleQueueTask({task: 'gif', word: 'test'}, console.log, console.log, console.log)
22 changes: 12 additions & 10 deletions server/tasks/gif.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* global app */

const fs = require('fs')
const path = require('path')
const async = require('async')
Expand All @@ -17,7 +19,7 @@ module.exports = (data, progress, resolve, reject) => {

// Validate gif word
let validChars = /^[a-zA-Z0-9\s]+$/i
if(!gifWord || gifWord.length > 10 || !validChars.test(gifWord)){
if (!gifWord || gifWord.length > 10 || !validChars.test(gifWord)) {
console.log(`[${gifWord}] Rejecting(validation): `, data)
return reject()
}
Expand All @@ -43,9 +45,9 @@ module.exports = (data, progress, resolve, reject) => {

gifRef.once('value', (snapshot) => {
if (snapshot.val() && snapshot.val().url) {
gifRef.child('views').transaction(function (current_value) {
return (current_value || 0) + 1;
});
gifRef.child('views').transaction(function (currVal) {
return (currVal || 0) + 1
})

console.log(`[${gifWord}] Gif already cached. skipping`)
return next('ok')
Expand All @@ -69,21 +71,21 @@ module.exports = (data, progress, resolve, reject) => {

gifsStorage.upload(filePath, {
destination: 'gifs/' + fileName,
gzip: true,
}, next);
gzip: true
}, next)
},

// Create object in database
(next) => {
console.log(`[${gifWord}] Saving to database`)

gifRef.set({
url: 'https://storage.googleapis.com/is-now-illegal.appspot.com/gifs/'+fileName,
url: 'https://storage.googleapis.com/is-now-illegal.appspot.com/gifs/' + fileName,
views: 1,
created: new Date(),
processTime: Date.now() - startTime
}, next)
},
}

], function (err) {
// Get final timestamp
Expand All @@ -96,12 +98,12 @@ module.exports = (data, progress, resolve, reject) => {
fs.unlink(filePath, () => {})

// Check for errors
if (err && err != 'ok') {
if (err && err !== 'ok') {
console.log(`[${gifWord}] Error processing gif:`, err)
return reject()
}

console.log(`[${gifWord}] Complete`)
resolve()
})
}
}
71 changes: 29 additions & 42 deletions share/server.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,46 @@
const fs = require('fs');
const path = require('path');
const async = require('async');
const express = require('express');
const compression = require('compression');
const apicache = require('apicache').middleware;
const handlebars = require('handlebars');
const request = require('request');
const fs = require('fs')
const express = require('express')
const compression = require('compression')
const apicache = require('apicache').middleware
const handlebars = require('handlebars')

// Expose global app object
var app = {};
global.app = app;
var app = {}
global.app = app

// Load template
var template = handlebars.compile(fs.readFileSync('index.html').toString());

// Home Page
var home = template({
url: 'http://isnowillegal.com',
site: 'IsNowIllegal',
title: 'Is Now Illegal',
gif_url: 'http://isnowillegal.com',
description: 'Declare things illegal and trump will sign it',
content_type: 'website',
});
var template = handlebars.compile(fs.readFileSync('index.html').toString())

//
// Start HTTP server
//
app.express = express();
app.express.use(compression());
app.express = express()
app.express.use(compression())
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused var

app.express.use(express.static('public'))

// Set engine
app.express.set('view engine', 'handlebars');
app.express.set('view engine', 'handlebars')

app.express.get('/', (req, res) => {
res.redirect('http://isnowillegal.com');
});
res.redirect('http://isnowillegal.com')
})

app.express.get('/:gif.gif', apicache('10 minutes'), (req, res) => {
const word = req.params.gif || '';
const filename = word.toUpperCase();
const uri = `https://storage.googleapis.com/is-now-illegal.appspot.com/gifs/${filename}.gif`;
const word = req.params.gif || ''
const filename = word.toUpperCase()
const uri = `https://storage.googleapis.com/is-now-illegal.appspot.com/gifs/${filename}.gif`

res.setHeader('Content-Type', 'image/gif');
res.setHeader('Access-Control-Allow-Origin', '*');
res.redirect(301, uri);
});
res.setHeader('Content-Type', 'image/gif')
res.setHeader('Access-Control-Allow-Origin', '*')
res.redirect(301, uri)
})

app.express.get('/:gif', apicache('10 minutes'), (req, res) => {
const word = req.params.gif || '';
const filename = word.toUpperCase();
const word = req.params.gif || ''
const filename = word.toUpperCase()

// const uri = `http://share.isnowillegal.com/${filename}.gif`
const uri = `https://storage.googleapis.com/is-now-illegal.appspot.com/gifs/${filename}.gif`;
const uri = `https://storage.googleapis.com/is-now-illegal.appspot.com/gifs/${filename}.gif`

res.status(200).send(
template({
Expand All @@ -65,12 +52,12 @@ app.express.get('/:gif', apicache('10 minutes'), (req, res) => {
gif_preview_url: 'http://share.isnowillegal.com/preview.png',
description: 'Declare things illegal and trump will sign it.',
content_type: 'video.other',
share_url: `http://isnowillegal.com/?${word}`,
share_url: `http://isnowillegal.com/?${word}`
})
);
});
)
})

const PORT = process.env.PORT || 8080;
const PORT = process.env.PORT || 8080
app.express.listen(PORT, () => {
console.log(`App listening on port ${PORT}`);
});
console.log(`App listening on port ${PORT}`)
})
Loading