Cleaned up index imports

This commit is contained in:
Carlo Morgenstern 2021-10-24 14:48:38 +02:00
parent 3e17cbd90d
commit 73b396c05e

301
index.js
View file

@ -1,152 +1,151 @@
const fetch = require("node-fetch"); const cacheManager = require('cache-manager');
const express = require('express'); const express = require('express');
const app = express(); const fetch = require('node-fetch');
const port = process.env.PORT || 5000; const fsStore = require('cache-manager-fs-binary');
const { CardCreator } = require('./create-card'); const { CardCreator } = require('./create-card');
const creator = new CardCreator(); const port = process.env.PORT || 5000;
// node cachemanager const app = express();
var cacheManager = require('cache-manager'); const creator = new CardCreator();
// storage for the cachemanager
var fsStore = require('cache-manager-fs-binary'); // Initialize caching on disk
// initialize caching on disk const diskCache = cacheManager.caching({
var diskCache = cacheManager.caching({ store: fsStore,
store: fsStore, options: {
options: { reviveBuffers: true,
reviveBuffers: true, binaryAsStream: false,
binaryAsStream: false, ttl: 60 * 60 * 4 /* seconds */,
ttl: 60 * 60 * 4 /* seconds */, maxsize: 1000 * 1000 * 1000 /* max size in bytes on disk */,
maxsize: 1000 * 1000 * 1000 /* max size in bytes on disk */, path: 'diskcache',
path: 'diskcache', preventfill: true
preventfill: true }
} });
});
async function getCharIdByName(world, name, retries = 1) {
async function getCharIdByName(world, name, retries = 1) { if (retries === -1) return undefined;
if (retries === -1) return undefined;
const response = await fetch(`https://xivapi.com/character/search?name=${name}&server=${world}`);
const response = await fetch(`https://xivapi.com/character/search?name=${name}&server=${world}`); const data = await response.json();
const data = await response.json();
if (data.Results[0] === undefined)
if (data.Results[0] === undefined) return getCharIdByName(world, name, --retries);
return getCharIdByName(world, name, --retries);
return data.Results[0].ID;
return data.Results[0].ID; }
}
app.get('/prepare/id/:charaId', async (req, res) => {
app.get('/prepare/id/:charaId', async (req, res) => { var cacheKey = `img:${req.params.charaId}`;
var cacheKey = `img:${req.params.charaId}`; var ttl = 60 * 60 * 4; // 4 hours
var ttl = 60 * 60 * 4; // 4 hours
diskCache.wrap(cacheKey,
diskCache.wrap(cacheKey, // called if the cache misses in order to generate the value to cache
// called if the cache misses in order to generate the value to cache function (cb) {
function (cb) { creator.ensureInit().then(() => creator.createCard(req.params.charaId), (reason) => cb('Init failed: ' + reason, null)).then(image => cb(null, {
creator.ensureInit().then(() => creator.createCard(req.params.charaId), (reason) => cb('Init failed: ' + reason, null)).then(image => cb(null, { binary: {
binary: { image: image,
image: image, }
} })).catch((reason) => cb('createCard failed: ' + reason, null));
})).catch((reason) => cb('createCard failed: ' + reason, null)); },
}, // Options, see node-cache-manager for more examples
// Options, see node-cache-manager for more examples { ttl: ttl },
{ ttl: ttl }, function (err, result) {
function (err, result) { if (err !== null) {
if (err !== null) { console.error(err);
console.error(err); res.status(500).send({status: "error", reason: err});
res.status(500).send({status: "error", reason: err}); return;
return; }
}
res.status(200).send({status: "ok", url: `/characters/id/${req.params.charaId}.png`});
res.status(200).send({status: "ok", url: `/characters/id/${req.params.charaId}.png`}); }
} );
); })
})
app.get('/prepare/name/:world/:charName', async (req, res) => {
app.get('/prepare/name/:world/:charName', async (req, res) => { var id = await getCharIdByName(req.params.world, req.params.charName);
var id = await getCharIdByName(req.params.world, req.params.charName);
if (id === undefined) {
if (id === undefined) { res.status(404).send({status: "error", reason: "Character not found."});
res.status(404).send({status: "error", reason: "Character not found."}); return;
return; }
}
res.redirect(`/prepare/id/${id}`);
res.redirect(`/prepare/id/${id}`); })
})
app.get('/characters/id/:charaId.png', async (req, res) => {
app.get('/characters/id/:charaId.png', async (req, res) => { var cacheKey = `img:${req.params.charaId}`;
var cacheKey = `img:${req.params.charaId}`; var ttl = 60 * 60 * 4; // 4 hours
var ttl = 60 * 60 * 4; // 4 hours
diskCache.wrap(cacheKey,
diskCache.wrap(cacheKey, // called if the cache misses in order to generate the value to cache
// called if the cache misses in order to generate the value to cache function (cb) {
function (cb) { creator.ensureInit().then(() => creator.createCard(req.params.charaId), (reason) => cb('Init failed: ' + reason, null)).then(image => cb(null, {
creator.ensureInit().then(() => creator.createCard(req.params.charaId), (reason) => cb('Init failed: ' + reason, null)).then(image => cb(null, { binary: {
binary: { image: image,
image: image, }
} })).catch((reason) => cb('createCard failed: ' + reason, null));
})).catch((reason) => cb('createCard failed: ' + reason, null)); },
}, // Options, see node-cache-manager for more examples
// Options, see node-cache-manager for more examples { ttl: ttl },
{ ttl: ttl }, function (err, result) {
function (err, result) { if (err !== null) {
if (err !== null) { console.error(err);
console.error(err); res.status(500).send({status: "error", reason: err});
res.status(500).send({status: "error", reason: err}); return;
return; }
}
var image = result.binary.image;
var image = result.binary.image;
res.writeHead(200, {
res.writeHead(200, { 'Content-Type': 'image/png',
'Content-Type': 'image/png', 'Content-Length': image.length,
'Content-Length': image.length, 'Cache-Control': 'public, max-age=14400'
'Cache-Control': 'public, max-age=14400' });
});
res.end(image, 'binary');
res.end(image, 'binary');
var usedStreams = ['image'];
var usedStreams = ['image']; // you have to do the work to close the unused files
// you have to do the work to close the unused files // to prevent file descriptors leak
// to prevent file descriptors leak for (var key in result.binary) {
for (var key in result.binary) { if (!result.binary.hasOwnProperty(key)) continue;
if (!result.binary.hasOwnProperty(key)) continue; if (usedStreams.indexOf(key) < 0
if (usedStreams.indexOf(key) < 0 && result.binary[key] instanceof Stream.Readable) {
&& result.binary[key] instanceof Stream.Readable) { if (typeof result.binary[key].close === 'function') {
if (typeof result.binary[key].close === 'function') { result.binary[key].close(); // close the stream (fs has it)
result.binary[key].close(); // close the stream (fs has it) } else {
} else { result.binary[key].resume(); // resume to the end and close
result.binary[key].resume(); // resume to the end and close }
} }
} }
} }
} );
); })
})
app.get('/characters/id/:charaId', async (req, res) => {
app.get('/characters/id/:charaId', async (req, res) => { res.redirect(`/characters/id/${req.params.charaId}.png`);
res.redirect(`/characters/id/${req.params.charaId}.png`); })
})
app.get('/characters/name/:world/:charName.png', async (req, res) => {
app.get('/characters/name/:world/:charName.png', async (req, res) => { var id = await getCharIdByName(req.params.world, req.params.charName);
var id = await getCharIdByName(req.params.world, req.params.charName);
if (id === undefined) {
if (id === undefined) { res.status(404).send({status: "error", reason: "Character not found."});
res.status(404).send({status: "error", reason: "Character not found."}); return;
return; }
}
res.redirect(`/characters/id/${id}.png`);
res.redirect(`/characters/id/${id}.png`); })
})
app.get('/characters/name/:world/:charName', async (req, res) => {
app.get('/characters/name/:world/:charName', async (req, res) => { res.redirect(`/characters/name/${req.params.world}/${req.params.charName}.png`);
res.redirect(`/characters/name/${req.params.world}/${req.params.charName}.png`); })
})
app.get('/', async (req, res) => {
app.get('/', async (req, res) => { res.redirect('https://github.com/ArcaneDisgea/XIV-Character-Cards');
res.redirect('https://github.com/ArcaneDisgea/XIV-Character-Cards'); })
})
app.listen(port, () => {
app.listen(port, () => { console.log(`Listening at http://localhost:${port}`)
console.log(`Listening at http://localhost:${port}`)
}) })