Skip to content

Commit

Permalink
feat(Pdf): add SCREENSHOT_SERVER_BASE_URL variable
Browse files Browse the repository at this point in the history
  • Loading branch information
annatraussnig committed Feb 3, 2025
1 parent 12da493 commit 74d28ed
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
6 changes: 6 additions & 0 deletions apps/api/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,12 @@ DEFAULT_MAIL_FROM_NAME="Republik"
#MAILCHIMP_URL=https://us9.api.mailchimp.com
#MAILCHIMP_API_KEY=

#############
# screenshots
#############

SCREENSHOT_SERVER_BASE_URL=http://localhost:3000

#############
# assets
#############
Expand Down
17 changes: 10 additions & 7 deletions packages/backend-modules/assets/express/pdf.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
const { returnImage } = require('../lib')
const { SCREENSHOT_BASE_URL, FRONTEND_BASE_URL } = process.env
const { SCREENSHOT_SERVER_BASE_URL, FRONTEND_BASE_URL } = process.env

if (!SCREENSHOT_BASE_URL) {
if (!SCREENSHOT_SERVER_BASE_URL) {
console.warn(
'missing env SCREENSHOT_BASE_URL, the /pdf endpoint will not work',
'missing env SCREENSHOT_SERVER_BASE_URL, the /pdf endpoint will not work',
)
}

module.exports = (server) => {
server.get('/pdf/:path(*)', async (req, res) => {
if (!SCREENSHOT_BASE_URL) {
console.warn('SCREENSHOT_BASE_URL not set unable to handle request')
if (!SCREENSHOT_SERVER_BASE_URL) {
console.warn(
'SCREENSHOT_SERVER_BASE_URL not set unable to handle request',
)
return res.status(403).end()
}

Expand All @@ -22,14 +24,15 @@ module.exports = (server) => {
const { images, download, format, version, ...options } = req.query

// build URL to fetch PDF from
const fetchUrl = new URL(`${SCREENSHOT_BASE_URL}/api/pdf`)
const fetchUrl = new URL(`${SCREENSHOT_SERVER_BASE_URL}/api/pdf`)

// add params
fetchUrl.searchParams.set('url', `${FRONTEND_BASE_URL}/${path}`)
images && fetchUrl.searchParams.set('images', images)
download && fetchUrl.searchParams.set('download', download)
format && fetchUrl.searchParams.set('format', format)
// we regenerate the PDF if the version changes
// we regenerate the PDF if the "version" changes
// (the version is the publication date of the document)
version && fetchUrl.searchParams.set('version', version)

const result = await fetch(fetchUrl, { method: 'GET' }).catch((error) => {
Expand Down

0 comments on commit 74d28ed

Please sign in to comment.