-
Notifications
You must be signed in to change notification settings - Fork 0
/
screenshot-service.js
103 lines (83 loc) · 2.48 KB
/
screenshot-service.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
const express = require('express')
const expressWinston = require('express-winston')
const hljs = require('highlight.js')
const { nanoid } = require('nanoid')
const nunjucks = require('nunjucks')
const puppeteer = require('puppeteer')
const winston = require('winston')
const serveStatic = require('serve-static')
const { addLoggersToPage } = require('./screenshot-service/utils')
require('express-async-errors')
const DEBUG = true
const PORT = 8080
const HOST_URL = `http://127.0.0.1:${ PORT }`
const ROOT_PATH = __dirname
const SCREENSHOTS_DIR = ROOT_PATH + '/screenshots'
const LOG_FILE = ROOT_PATH + '/logs/screenshot-service.log'
const HL_THEME = 'github'
const app = express()
app.use(express.json())
app.set('view engine', 'html')
app.use(serveStatic(ROOT_PATH + '/assets', {
immutable: true,
maxAge: 5 * 60 * 1000,
}))
nunjucks.configure('views', { autoescape: true })
let browser
puppeteer
.launch({
args: [
// disable CORS security to load assets from local server
'--disable-web-security',
],
headless: true,
})
.then(instance => {
browser = instance
console.log('Browser launched!')
})
app.post('/generate-image', async (req, res) => {
console.time('Generate image')
const screenshotPath = SCREENSHOTS_DIR + `/${ nanoid() }.png`
// generate HTML
const { code } = req.body
const highlightedCodeHtml = hljs.highlightAuto(code).value
const html = nunjucks.render('template.html', {
code: highlightedCodeHtml,
cssUrl: HOST_URL + '/css/styles.css',
hljsCssUrl: HOST_URL + `/hljs-styles/${ HL_THEME }.css`,
})
// generate screenshot
const page = await browser.newPage()
if (DEBUG) {
addLoggersToPage(page)
}
await page.setContent(html)
await page.screenshot({ path: screenshotPath })
await page.close()
console.timeEnd('Generate image')
res.json({
path: screenshotPath,
})
})
app.use((req, res, next) => {
res.status(404).json({
message: 'Page not found.',
})
})
app.use(expressWinston.logger({
transports: [
new winston.transports.File({
filename: LOG_FILE,
level: 'error',
}),
new winston.transports.Console(),
],
meta: false,
msg: "HTTP {{res.statusCode}} {{req.method}} {{res.responseTime}}ms {{req.url}}",
expressFormat: true,
colorize: false,
}))
app.listen(PORT, () => {
console.log(`Listening port ${ PORT }...`)
})