diff --git a/.gitignore b/.gitignore index a67c5d9..2da326b 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,6 @@ node_modules package-lock.json region.txt +.reload *.log .ssl/ssl.* \ No newline at end of file diff --git a/.npmignore b/.npmignore index 3b6c027..bce8d91 100644 --- a/.npmignore +++ b/.npmignore @@ -1,5 +1,6 @@ .idea .git +.reload node_modules npm-debug.log tests diff --git a/app.js b/app.js index 012bcda..560dcda 100644 --- a/app.js +++ b/app.js @@ -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; \ No newline at end of file +module.exports = app; diff --git a/package.json b/package.json index 5c1bd41..55e512a 100644 --- a/package.json +++ b/package.json @@ -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" } }