Option to reload icons without restarting app

This commit is contained in:
Vjacheslav Trushkin 2017-11-20 17:51:50 +02:00
parent cc86f1c530
commit fdc00aaa8a
4 changed files with 72 additions and 41 deletions

1
.gitignore vendored
View File

@ -3,5 +3,6 @@
node_modules
package-lock.json
region.txt
.reload
*.log
.ssl/ssl.*

View File

@ -1,5 +1,6 @@
.idea
.git
.reload
node_modules
npm-debug.log
tests

109
app.js
View File

@ -51,50 +51,69 @@ if (region.length > 10 || !region.match(/^[a-z0-9_-]+$/i)) {
region = '';
}
// Load default collections
if (serveDefaultIcons) {
const icons = require('simple-svg-icons');
Object.keys(icons.collections()).forEach(prefix => {
let filename = icons.locate(prefix),
data;
try {
data = fs.readFileSync(filename, 'utf8');
data = JSON.parse(data);
cdn.deOptimize(data);
} catch (err) {
console.log(err);
// Reload key
let reloadKey = '';
try {
reloadKey = fs.readFileSync('.reload', 'utf8').trim();
} catch (err) {
}
if (reloadKey.length < 8 || reloadKey.length > 64) {
reloadKey = '';
}
// Icons module
const icons = serveDefaultIcons ? require('simple-svg-icons') : null;
function loadIcons() {
cdn.clearCollections();
console.log('Loading collections at ' + (new Date()).toString());
// Load default collections
if (icons !== null) {
Object.keys(icons.collections()).forEach(prefix => {
let filename = icons.locate(prefix),
data;
try {
data = fs.readFileSync(filename, 'utf8');
data = JSON.parse(data);
cdn.deOptimize(data);
} catch (err) {
console.log(err);
return;
}
console.log('Added premade collection: ' + prefix);
cdn.addCollection(prefix, data);
});
}
// Add collections from "json" directory
let files;
try {
files = fs.readdirSync(customIconsDirectory);
} catch (err) {
files = [];
}
files.forEach(file => {
let list = file.split('.');
if (list.length !== 2 || list[1] !== 'json') {
return;
}
console.log('Added premade collection: ' + prefix);
cdn.addCollection(prefix, data);
try {
let data = fs.readFileSync(customIconsDirectory + '/' + file, 'utf8');
data = JSON.parse(data);
cdn.deOptimize(data);
console.log('Added custom collection: ' + list[0]);
cdn.addCollection(list[0], data);
} catch (err) {
}
});
// Sort collections
cdn.sortPrefixes();
}
// Add collections from "json" directory
let files;
try {
files = fs.readdirSync(customIconsDirectory);
} catch (err) {
files = [];
}
files.forEach(file => {
let list = file.split('.');
if (list.length !== 2 || list[1] !== 'json') {
return;
}
try {
let data = fs.readFileSync(customIconsDirectory + '/' + file, 'utf8');
data = JSON.parse(data);
cdn.deOptimize(data);
console.log('Added custom collection: ' + list[0]);
cdn.addCollection(list[0], data);
} catch (err) {
}
});
// Sort collections
cdn.sortPrefixes();
// Load icons
loadIcons();
// Disable X-Powered-By header
app.disable('x-powered-by');
@ -157,6 +176,16 @@ app.get('/version', (req, res) => {
res.send(body);
});
// Reload collections without restarting app
app.get('/reload', (req, res) => {
if (reloadKey.length && req.query && req.query.key && req.query.key === reloadKey) {
// Reload collections
process.nextTick(loadIcons);
}
res.sendStatus(200);
});
// Redirect home page
app.get('/', (req, res) => {
res.redirect(301, 'https://simplesvg.com/');
@ -167,4 +196,4 @@ app.listen(port, () => {
console.log('Listening on port ' + port);
});
module.exports = app;
module.exports = app;

View File

@ -10,7 +10,7 @@
"license": "MIT",
"dependencies": {
"express": "^4.16.2",
"simple-svg-cdn": "^0.3.1",
"simple-svg-cdn": "^0.3.2",
"simple-svg-icons": "git+https://github.com/simplesvg/icons.git"
}
}