Commit f1e1a650 authored by yPhil's avatar yPhil

Sys: New pm2 process management strategy

parent b637b19e
var express = require('express'),
path = require('path'),
index = require('./routes/index'),
fs = require('fs'),
app = express();
if (!fs.existsSync(process.env.FAVICONS_CACHE_DIR)){
fs.mkdirSync(process.env.FAVICONS_CACHE_DIR);
}
app.set('views', path.join(__dirname, 'views'));
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
......
{
"apps" : [{
"name" : "petrolette",
"script" : "./bin/www",
"watch" : true,
"ignore_watch" : ["favicons-cache"],
"favicons_cache" : "favicons-cache",
"log_date_format" : "YYYY-MM-DD HH:mm",
"env": {
"PORT": 8666,
"FAVICONS_CACHE_DIR": "favicons-cache"
},
"env_production" : {
"NODE_ENV": "production"
}
}]
}
......@@ -6,10 +6,7 @@ var express = require('express'),
feedrat = require('feedrat'),
Url = require('url'),
fs = require('fs'),
path = require('path'),
http = require('http'),
packageJson = require('../package.json'),
cacheDir = path.join(__dirname, packageJson.cacheDirName);
path = require('path');
// require('request').debug = true;
......@@ -98,57 +95,43 @@ router.get('/feed', function(req, res) {
});
// var p = Url.parse(req.query.url),
// fileName = p.host + "-" + p.path;
// fs.exists(path.join(cacheDir, fileName), (exists) => {
// if (!exists) {
// console.log('path.join(cacheDir, fileName) does not exist');
// // fs.mkdirSync(cacheDir);
// }
// });
console.log('####### START');
router.use('/favicon', function (req, res, next) {
console.log('Request:', req.method);
next();
});
router.get('/favicon', function(req, res) {
favicon(req.query.url, function(err, iconUrl) {
if (iconUrl) {
// if (!iconUrl.startsWith('..') && Url.parse(iconUrl)) {
// console.log('iconUrl OK: (%s)', iconUrl);
if (!iconUrl.startsWith('..') && Url.parse(iconUrl)) {
// var u = Url.parse(iconUrl);
var u = Url.parse(iconUrl),
h = u.host.replace(/\//g, ''),
p = u.path.replace(/\//g, ''),
fileName;
// var cleanHost = u.host.replace(/\//g, ''),
// cleanPath = u.path.replace(/\//g, ''),
// fileName;
fileName = path.join(process.env.FAVICONS_CACHE_DIR, h + '.' + p);
// fileName = path.join('/tmp/cache', cleanHost + '.' + cleanPath);
// // fileName = path.join(__dirname, '..', 'cache', cleanHost + '.' + cleanPath);
let stream = fs.createWriteStream(fileName);
stream.on('finish', function () {
console.log("SAVED %s (%s)", fileName, iconUrl);
}).on('error', function (err) {
console.log("NOT SAVED %s (%s)", fileName, err);
});
request(iconUrl).pipe(stream);
// let stream = fs.createWriteStream(fileName);
// stream.on('finish', function () {
// console.log("SAVED %s (%s)", fileName, iconUrl);
// }).on('error', function (err) {
// console.log("NOT SAVED %s (%s)", fileName, err);
// });
// request(iconUrl).pipe(stream);
// } else {
// console.log('iconUrl NOT OK: (%s)', iconUrl);
// }
} else {
console.log('iconUrl NOT OK: (%s)', iconUrl);
}
res.send(iconUrl);
} else {
res.status(500).send('No icon found');
}
});
});
......
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