forked from reddavis/Crypto-Icons-API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
148 lines (125 loc) · 3.99 KB
/
server.js
File metadata and controls
148 lines (125 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
const express = require('express');
const app = express();
const { convert } = require('convert-svg-to-png');
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const { document } = (new JSDOM('')).window;
const fs = require('fs');
const path = require('path');
const debug = true;
const hitCountKey = 'apiHitCount';
app.use(express.static(__dirname + '/public'));
// GET Home page
app.get('/', function(req, res) {
res.sendFile('index.html');
});
// GET png
app.get('/api/:style/:currency/:size/:color?', async(req, res) => {
// Params
const style = req.params.style;
const currency = req.params.currency;
const size = req.params.size;
const cacheKey = req.path;
const filename = currency + '-' + style + '-' + size + '.png';
// 0 <= Valid size <= 1000
if (size <= 0 || size >= 1000) {
res.status(400).send({'error' : 'Invalid size'});
return
}
// Redis
var redisRetryStrategy = function(options) {
if (options.error.code === 'ECONNREFUSED') {
}
};
const redisURL = process.env.REDIS_URL || 'http://127.0.0.1:6379';
var client = require('redis').createClient({
url : process.env.REDIS_URL,
return_buffers : true
});
client.on('error', function (err) {
client.quit();
generatePNG(req, res, null)
});
client.on('connect', function (err) {
// Check cache
client.get(cacheKey, async(error, result) => {
if (result == null) {
console.log("Cache miss");
generatePNG(req, res, client)
} else {
client.quit();
console.log("Cache hit");
sendPNG(res, result, filename)
}
})
})
});
// Functions
function sendPNG(response, png, filename) {
response.set('Content-Type', 'image/png');
response.header('Content-disposition', 'inline; filename=' + filename);
response.send(png)
}
async function generatePNG(req, res, redis) {
// Params
const style = req.params.style;
const currency = req.params.currency;
const size = req.params.size;
const color = req.params.color;
const cacheKey = req.path;
const filename = currency + '-' + style + '-' + size + '.png';
// SVG file path
if (debug) console.log('1');
const svgPath = path.join(__dirname, 'public', 'cryptocurrency-icons', 'svg', style, currency + '.svg');
console.log('svgPath: ', svgPath);
// Check if file exists
if (debug) console.log('2');
if (!fs.existsSync(svgPath)) {
res.status(404).send(null);
return
}
if (debug) console.log('3');
const svg = fs.readFileSync(svgPath, 'utf8');
const element = document.createElement('div');
element.innerHTML = svg;
if (debug) console.log('4');
const svgElement = element.getElementsByTagName("svg")[0];
// Define the circles
const colorCircle = element.getElementsByTagName("circle")[0];
const iconCircle = element.getElementsByTagName("use")[1];
if (debug) console.log('5');
// Set viewBox so SVG resizes correctly
const originalSize = svgElement.getAttribute('width');
svgElement.setAttribute('viewBox', '0 0 ' + originalSize + ' ' + originalSize);
// Set requested size
svgElement.setAttribute('width', size);
svgElement.setAttribute('height', size);
if (debug) console.log('6');
// Set circle color, if `color` or `icon`
if (color != null && style == 'color') {
const colorString = '#' + color;
colorCircle.setAttribute('fill', colorString)
} else if (color != null && style == 'icon') {
const colorString = '#' + color;
iconCircle.setAttribute('fill', colorString)
}
if (debug) console.log('7');
// Convert to PNG
const png = await convert(element.innerHTML, {
'height' : size,
'width' : size,
'puppeteer' : {'args' : ['--no-sandbox', '--disable-setuid-sandbox']}
});
// Save to redis
if (redis != null) {
redis.set(cacheKey, png, function(err) {
redis.quit()
});
redis.incr(hitCountKey);
}
// Return response
sendPNG(res, png, filename)
}
// Listen
var port = process.env.PORT || 3000;
app.listen(port, () => console.log('Our app is running on http://localhost:' + port));