const express = require("@runkit/runkit/express-endpoint/1.0.0");
const bodyParser = require('body-parser')
const cors = require('cors');
const cache = require('memory-cache');
const textToImage = require("text-to-image");
const Prando = require("prando");
const wordList = require("wordlist-english");
const app = express(exports);
const HEX_CHARS = "ABCDEF0123456789";
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.get("/images/:id", async (req, res) => {
const { id } = req.params;
const rng = new Prando(id);
const fromCache = cache.get(id);
let image = cache.get(id) || await textToImage.generate(id, {
width: 400,
customHeight: 400,
fontSize: 18,
fontFamily: 'Arial',
lineHeight: 400,
margin: 5,
bgColor: `#${rng.nextString(6, HEX_CHARS)}`,
textColor: `#${rng.nextString(6, HEX_CHARS)}`,
textAlign: "center"
})
.then(b64Str => b64Str.substring(22))
.then(res => cache.put(id, res))
res.setHeader('Content-Type', 'image/png');
return res.send(Buffer.from(image, "base64"));
});
app.get("/metadata/:id", async (req, res) => {
const { id } = req.params;
const rng = new Prando(id);
const md = cache.get("meta" + id) || cache.put("meta" + id, {
description: "This is the best nft you'll ever have.",
external_url: "",
image: `${process.env.RUNKIT_ENDPOINT_URL}images/${id}`,
attributes: [
{
trait_type: "background",
value: `#${rng.nextString(6, HEX_CHARS)}`
},
{
trait_type: "text",
value: `#${rng.nextString(6, HEX_CHARS)}`
}
],
name: `${rng.nextArrayItem(wordList['english'])} ${rng.nextArrayItem(wordList['english'])}`,
});
return res.send(md);
});