-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
90 lines (81 loc) · 3 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
const fastify = require('fastify')({ logger: true })
const getScreenshot = require('./functions/rayso')
const path = require('path')
fastify.register(require('fastify-static'), {
root: path.join(__dirname, 'public'),
prefix: '/public/',
})
fastify.get('/', async (req, reply) => {
return reply.sendFile('index.html')
})
fastify.get('/generate', async (request, reply) => {
var darkMode = (String(request.query.darkMode).toLowerCase() === 'true');
var title = request.query.title || "RaySo"
var theme = request.query.theme || "raindrop"
var lang = request.query.lang || "auto"
var bg = request.query.bg ? (String(request.query.bg).toLowerCase() === 'true') : true
var padding = request.query.padding ? JSON.parse(Number(request.query.padding)) : 64
var text = request.query.text || ""
if (!text) {
reply.send({ "error": true, "message": "Provide text" })
return reply
}
if (![16, 32, 64, 128].includes(padding)) {
reply.send({ "error": true, "message": "padding must be one of 16, 32, 64, 128" })
return reply
}
if (!["breeze", "candy", "crimson", "falcon", "meadow", "raindrop", "sunset", "midnight"].includes(theme)) {
reply.send({ "error": true, "message": "Available themes: breeze, candy, crimson, falcon, meadow, midnight, raindrop, sunset!" })
return reply
}
try {
var image = await getScreenshot(title, text, theme, padding, bg, darkMode, lang)
reply.type('image/png')
reply.send(image)
} catch (error) {
reply.type('application/json')
reply.send({ "error": true, "message": error })
}
})
fastify.post('/generate', async (request, reply) => {
var darkMode = (String(request.query.darkMode).toLowerCase() === 'true');
var title = request.query.title || "RaySo"
var theme = request.query.theme || "raindrop"
var lang = request.query.lang || "auto"
var bg = (String(request.query.bg).toLowerCase() === 'true')
var padding = request.query.padding ? JSON.parse(Number(request.query.padding)) : 64
if (![16, 32, 64, 128].includes(padding)) {
return { "error": true, "message": "padding must be one of 16, 32, 64, 128" }
}
if (!["breeze", "candy", "crimson", "falcon", "meadow", "raindrop", "sunset", "midnight"].includes(theme)) {
return { "error": true, "message": "Available themes: breeze, candy, crimson, falcon, meadow, midnight, raindrop, sunset!" }
}
var raySo = new RaySo({
"title": title,
"darkMode": darkMode,
"theme": theme,
"language": lang,
"background": bg,
"padding": padding,
"browserPath": process.env.BROWSER_PATH
});
raySo
.cook(request.body.code || "Give Some codes DUDE!")
.then((response) => {
reply.type('image/png');
reply.send(response)
})
.catch((err) => {
console.error(err);
});
})
const start = async () => {
try {
var image = await getScreenshot("RaySo", "Give Some codes DUDE!", "raindrop", 64, true, false, "auto")
await fastify.listen(process.env.PORT || 3000)
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()