-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
116 lines (96 loc) · 3.35 KB
/
index.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
import express from 'express';
import bodyParser from 'body-parser';
import { exec } from 'child_process';
import { readFile, writeFile, unlink } from 'fs/promises';
import crypto from 'crypto';
const app = express();
const PORT = process.env.PORT || 3000;
// Middleware to parse JSON bodies
app.use(bodyParser.json({ limit: '1mb' }));
// host the openai.yaml file and logo
app.use(express.static('public'));
// Function to generate a unique file name
const generateFileName = () => crypto.randomBytes(16).toString('hex');
// Function to execute mermaid CLI and generate diagram
const generateDiagram = (inputFile, outputFile) => new Promise((resolve, reject) => {
exec(`/home/mermaidcli/node_modules/.bin/mmdc -p /puppeteer-config.json -i ${inputFile} -o ${outputFile}`, (error) => {
if (error) {
reject(error);
} else {
resolve();
}
});
});
app.get('/.well-known/ai-plugin.json', (req, res) => {
const pluginInfo = {
schema_version: 'v1',
name_for_human: 'Mermaid Plugin',
name_for_model: 'mermaid-plugin',
description_for_human: 'Create images using the mermaid diagram code.',
description_for_model: 'Help the user with creating diagrams based off the mermaid syntax. You can create diagrams based on provided mermaid code.',
auth: {
type: 'none'
},
api: {
type: 'openapi',
url: `http://localhost:${PORT}/openapi.yaml`
},
logo_url: `http://localhost:${PORT}/logo.svg`,
contact_email: 'support@example.com',
legal_info_url: 'http://www.example.com/legal'
};
res.json(pluginInfo);
});
// Handle POST requests to /png endpoint
app.post('/png', async (req, res) => {
try {
const { code } = req.body;
if (!code) {
return res.status(400).json({ error: 'Missing `code` in request body' });
}
const fileName = generateFileName();
const inputFile = `/tmp/${fileName}.mmd`;
const outputFile = `/tmp/${fileName}.png`;
// Write the code to an input file
await writeFile(inputFile, code);
// Generate the PNG
await generateDiagram(inputFile, outputFile);
// Read the PNG file and send it as a response
const pngBuffer = await readFile(outputFile);
res.set('Content-Type', 'image/png');
res.send(pngBuffer);
// Clean up the temporary files
await Promise.all([unlink(inputFile), unlink(outputFile)]);
} catch (err) {
console.error(err);
res.status(500).json({ error: err.message });
}
});
app.post('/svg', async (req, res) => {
try {
const { code } = req.body;
if (!code) {
return res.status(400).json({ error: 'Missing `code` in request body' });
}
const fileName = generateFileName();
const inputFile = `/tmp/${fileName}.mmd`;
const outputFile = `/tmp/${fileName}.svg`;
// Write the code to an input file
await writeFile(inputFile, code);
// Generate the SVG
await generateDiagram(inputFile, outputFile);
// Read the SVG file and send it as a response
const svgBuffer = await readFile(outputFile);
res.set('Content-Type', 'image/svg+xml');
res.send(svgBuffer);
// Clean up the temporary files
await Promise.all([unlink(inputFile), unlink(outputFile)]);
} catch (err) {
console.error(err);
res.status(500).json({ error: err.message });
}
});
// Start the server
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`);
});