-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
89 lines (75 loc) · 2.31 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
const express = require('express')
const bodyParser = require('body-parser')
const PDF = require('./pdf')
const app = express()
/**
* Server port
*/
const port = 3010
/**
* Comma seperated list of allowed IP addresses
*/
// const allowedOrigins = process.env.ALLOWED_ORIGINS || '::1,127.0.0.1'
// const AllowedOriginMiddleware = (req, res, next) => {
// let ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress
// const timestamp = new Date().toISOString()
// if (ip.includes(',')) {
// ip = ip.split(', ')[0]
// }
// if (!allowedOrigins.split(',').includes(ip)) {
// // console.log(`[${timestamp}] Blocking IP: ${ip}`)
// res.send({ notice: 'Forbidden ' + ip })
// return
// }
// console.log(`[${timestamp}] Allowing IP: ${ip}`)
// next()
// }
// app.use(AllowedOriginMiddleware)
app.use(bodyParser.urlencoded({ limit: '50mb', extended: false }))
app.use(bodyParser.json({ limit: '50mb' }))
/**
* PDF generation route
*/
app.get('/', (req, res) => {
const url = req.query.url
const title = req.query.title
if (!url) {
res.status(200).send({ version: 1.4 })
return
}
PDF.generate(url, { title }).then(file => {
res.set({
'Content-Type': 'application/pdf',
'Content-Length': file.length,
'Content-Disposition': `inline; filename="${title || 'file'}.pdf"`
}).send(file)
}).catch(err => {
res.status(500).send({ error: err.message })
})
})
app.post('/', (req, res) => {
const options = req.body.options
const title = req.body.title
const contents = req.body.contents
const header = req.body.header || null
const footer = req.body.footer || null
if (!contents) {
res.status(422).send({ error: 'Missing parameter contents' })
return
}
PDF.generate(null, contents, { title, header, footer, ...options }).then(file => {
res.set({
'Content-Type': 'application/pdf',
'Content-Length': file.length,
'Content-Disposition': `inline; filename="${title || 'file'}.pdf"`
}).send(file)
}).catch(err => {
res.status(500).send({ error: err.message })
})
})
/**
* Start server
*/
app.listen(port, () => {
console.log(`App listening now on port ${port}`)
})