Spaces:
Paused
Paused
| // Require necessary modules | |
| const path = require('path'); | |
| const fs = require('fs'); | |
| const crypto = require('crypto'); | |
| const fastify = require('fastify')({ | |
| logger: true, | |
| bodyLimit: 15 * 1048576, | |
| }); | |
| const { PKPass } = require('passkit-generator'); | |
| // Load certificates | |
| const certDirectory = path.resolve(process.cwd(), 'cert'); | |
| const wwdr = fs.readFileSync(path.join(certDirectory, 'wwdr.pem')); | |
| const signerCert = fs.readFileSync(path.join(certDirectory, 'signerCert.pem')); | |
| const signerKey = fs.readFileSync(path.join(certDirectory, 'signerKey.key')); | |
| // Define the route to handle POST requests | |
| fastify.post('/', async (request, reply) => { | |
| try { | |
| // Destructure request body | |
| const { | |
| name, | |
| cardNr, | |
| dateFrom, | |
| dateTo, | |
| dateBirth, | |
| aztecCode, | |
| type, | |
| image | |
| } = request.body; | |
| // Generate pass ID | |
| const passID = crypto.createHash('md5').update(`${name}_${Date.now()}`).digest('hex'); | |
| // Generate the pass | |
| const pass = await PKPass.from( | |
| { | |
| model: path.resolve(process.cwd(), 'transit.pass'), | |
| certificates: { | |
| wwdr, | |
| signerCert, | |
| signerKey, | |
| }, | |
| }, | |
| { | |
| serialNumber: passID, | |
| }, | |
| ); | |
| // Set barcode | |
| pass.setBarcodes({ | |
| format: "PKBarcodeFormatAztec", | |
| message: aztecCode, | |
| messageEncoding: "iso-8859-1" | |
| }); | |
| // Set expiration date | |
| const [day, month, year] = dateTo.split('.'); | |
| const expirationDate = new Date(`${year}-${month}-${day}T23:59:00+01:00`); | |
| pass.setExpirationDate(expirationDate); | |
| // Add fields | |
| pass.headerFields.push({ key: 'type', value: type }); | |
| pass.primaryFields.push({ key: 'name', value: name }); | |
| pass.secondaryFields.push({ key: 'cardNr', label: "Kartennummer", value: cardNr }, { key: 'dateTo', label: "Ablaufdatum", value: dateTo }); | |
| pass.auxiliaryFields.push({ key: 'dateBirth', label: "Geburtsdatum", value: dateBirth }, { key: 'dateFrom', label: "Beginn", value: dateFrom }); | |
| // Add image | |
| const imageBuffer = Buffer.from(image, "base64"); | |
| pass.addBuffer("thumbnail.png", imageBuffer); | |
| pass.addBuffer("thumbnail@2x.png", imageBuffer); | |
| // Set response headers and send pass as buffer | |
| reply.header('Content-Type', 'application/vnd-apple.pkpass'); | |
| reply.send(pass.getAsBuffer()); | |
| } catch (error) { | |
| // Log and handle errors | |
| console.error('Error occurred during request processing:', error); | |
| console.error('Error stack trace:', error.stack); | |
| reply.code(500).send({ error: 'Internal Server Error' }); | |
| } | |
| }); | |
| // Start the server | |
| const port = 7860; | |
| fastify.listen(port, '0.0.0.0', (err, address) => { | |
| if (err) { | |
| console.error(err); | |
| process.exit(1); | |
| } | |
| console.log(`Server listening on ${address}`); | |
| }); | |