-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
117 lines (104 loc) · 4.77 KB
/
test.js
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
const http = require('http');
const url = require('url');
const {combineCanvases, generateBadgeMinterCanvas} = require("./generateChart");
// Set up HTTP server
const server = http.createServer(async (req, res) => {
const parsedUrl = url.parse(req.url, true); // Parse the URL including query strings
if (parsedUrl.pathname === '/image') {
const { data, score, labels, roles, title } = parseQueryParams(req);
try {
const imageData = await combineCanvases(data, labels, score, roles, title);
// Respond with JSON containing the image data URL
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ data: imageData }));
} catch (error) {
console.error("Error generating image:", error);
res.writeHead(500, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: "Failed to generate image" }));
}
} else if (parsedUrl.pathname === '/imageMinted') {
const query = parsedUrl.query;
const roles = query.role ? query.role.split(',').map(role => role.trim()) : [];
try {
const imageData = await generateBadgeMinterCanvas(roles);
// Respond with JSON containing the image data URL
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ data: imageData }));
} catch (error) {
console.error("Error generating image:", error);
res.writeHead(500, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: "Failed to generate image" }));
}
} else if (parsedUrl.pathname === '/html') {
const { data, score, labels, roles, title } = parseQueryParams(req);
try {
const imageData = await combineCanvases(data, labels, score, roles, title);
// Construct HTML response with embedded image
const htmlContent = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Generated Image</title>
</head>
<body>
<img src="${imageData}" alt="Generated Image">
</body>
</html>
`;
// Respond with HTML content
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(htmlContent);
} catch (error) {
console.error("Error generating image:", error);
res.writeHead(500, { 'Content-Type': 'text/html' });
res.end(`<html><body><h1>Error generating image</h1><p>${error.message}</p></body></html>`);
}
} else if (parsedUrl.pathname === '/htmlMinted') {
const query = parsedUrl.query;
const roles = query.role ? query.role.split(',').map(role => role.trim()) : [];
try {
const imageData = await generateBadgeMinterCanvas(roles);
// Construct HTML response with embedded image
const htmlContent = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Generated Image</title>
</head>
<body>
<img src="${imageData}" alt="Generated Image">
</body>
</html>
`;
// Respond with HTML content
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(htmlContent);
} catch (error) {
console.error("Error generating image:", error);
res.writeHead(500, { 'Content-Type': 'text/html' });
res.end(`<html><body><h1>Error generating image</h1><p>${error.message}</p></body></html>`);
}
} else {
// Handle root or other paths
res.writeHead(404, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Not Found' }));
}
});
function parseQueryParams(req) {
const { query } = url.parse(req.url, true);
return {
data: query.data ? query.data.split(',').map(Number) : [50, 50, 50, 50, 50],
score: query.score ? Number(query.score) : 0,
labels: query.labels ? query.labels.split(',') : ['Posts', 'Followers', 'Comments', 'Likes', 'NFTs'],
roles: query.role ? query.role.split(',').map(role => role.trim()) : [],
title: query.title ? query.title.trim() : 'Your Social Data'
};
}
const port = 3000;
server.listen(port, () => {
console.log(`Server running at http://localhost:${port}/`);
});