-
Notifications
You must be signed in to change notification settings - Fork 14
/
gosenExpressApp.js
executable file
·108 lines (104 loc) · 3.4 KB
/
gosenExpressApp.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
const gosenExpressApp = (page) => {
const bodyParser = require('body-parser')
const debug = require('debug')('hcepPdfServer')
const express = require('express')
const ejs = require('ejs')
const morgan = require('morgan')
const timeout = require('connect-timeout')
const validator = require('validator')
const appTimeoutMsec = process.env.HCEP_APP_TIMEOUT_MSEC || 30000
const pageTimeoutMsec = process.env.HCEP_PAGE_TIMEOUT_MSEC || 10000
const listenPort = process.env.HCEP_PORT || 8000
/* bytes or string for https://www.npmjs.com/package/bytes */
const maxRquestSize = process.env.HCEP_MAX_REQUEST_SIZE || '10mb'
const templatePath = 'app/gosen.html'
const app = express()
const env = app.get('env')
console.log('env:', env)
if (env == 'production') {
app.use(morgan())
} else {
app.use(morgan('dev'))
}
app.use(bodyParser.urlencoded({ extended: false, limit: maxRquestSize }))
app.use(timeout(appTimeoutMsec))
app.listen(listenPort, () => {
console.log('Listening on:', listenPort)
})
// https://gist.github.com/malyw/b4e8284e42fdaeceab9a67a9b0263743
app.route('/')
.get(async (req, res) => {
let line1, line2, emoji, rarity
if (req.query.line1 && validator.isLength(req.query.line1, { min: 0, max: 20 })) {
line1 = req.query.line1
} else {
line1 = '5000兆円'
}
if (req.query.line2 && validator.isLength(req.query.line2, { min: 0, max: 20 })) {
line2 = req.query.line2
} else {
line2 = '欲しい!'
}
if (req.query.emoji && validator.isLength(req.query.emoji, { min: 0, max: 1 })) {
emoji = req.query.emoji
} else {
emoji = ''
}
if (req.query.rarity && validator.isInt(req.query.rarity, { min: 0, max: 10 })) {
rarity = req.query.rarity
} else {
rarity = 0
}
const data = { line1: line1, line2: line2, emoji: emoji, rarity:rarity }
const options = {}
const html = await ejs.renderFile(templatePath, data, options)
if (req.query.html) {
res.contentType("text/html")
res.status(200)
res.send(html)
res.end()
return
}
const selector = '.gosen-preview'
const padding = 0
try {
await page.setContent(html)
await page.setViewport({ width: 3000, height: 1000 })
const rect = await page.evaluate(selector => {
const element = document.querySelector(selector);
if (!element) return null;
const { x, y, width, height } = element.getBoundingClientRect();
return { left: x, top: y, width, height, id: element.id };
}, selector);
const buff = await page.screenshot({
omitBackground: true,
clip: {
x: rect.left - padding,
y: rect.top - padding,
width: rect.width + padding * 2 - 50,
height: rect.height + padding * 2
}
})
res.status(200)
res.header("Access-Control-Allow-Origin", "*");
res.contentType("image/png")
res.send(buff)
res.end()
} catch (e) {
console.error(e)
res.contentType("text/plain")
res.status(500)
res.end()
}
})
/**
* Health Check
*/
app.get('/hc', async (req, res) => {
debug('health check ok')
res.status(200)
res.end('ok')
})
return app
}
module.exports.gosenExpressApp = gosenExpressApp