-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'ramakevin' pdf-generator
- Loading branch information
Showing
13 changed files
with
254 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"unknown": { | ||
"versionSfCore": null, | ||
"versionFramework": "4.4.6", | ||
"isWithinCompose": false, | ||
"composeOrgName": null, | ||
"composeServiceName": null, | ||
"command": [], | ||
"options": {}, | ||
"error": null, | ||
"machineId": "55ce82351452d045dae40665b323edd6", | ||
"serviceProviderAwsCfStackId": null, | ||
"serviceProviderAwsCfStackCreated": null, | ||
"serviceProviderAwsCfStackUpdated": null, | ||
"serviceProviderAwsCfStackStatus": null, | ||
"serviceProviderAwsCfStackOutputs": null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules | ||
.serverless | ||
/yarn.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<!-- | ||
title: 'Serverless Framework Node Express API on AWS' | ||
description: 'This template demonstrates how to develop and deploy a simple Node Express API running on AWS Lambda using the Serverless Framework.' | ||
layout: Doc | ||
framework: v4 | ||
platform: AWS | ||
language: nodeJS | ||
priority: 1 | ||
authorLink: 'https://github.com/serverless' | ||
authorName: 'Serverless, Inc.' | ||
authorAvatar: 'https://avatars1.githubusercontent.com/u/13742415?s=200&v=4' | ||
--> | ||
|
||
# Serverless Framework Node Express API on AWS | ||
|
||
This template demonstrates how to develop and deploy a simple Node Express API service running on AWS Lambda using the Serverless Framework. | ||
|
||
This template configures a single function, `api`, which is responsible for handling all incoming requests using the `httpApi` event. To learn more about `httpApi` event configuration options, please refer to [httpApi event docs](https://www.serverless.com/framework/docs/providers/aws/events/http-api/). As the event is configured in a way to accept all incoming requests, the Express.js framework is responsible for routing and handling requests internally. This implementation uses the `serverless-http` package to transform the incoming event request payloads to payloads compatible with Express.js. To learn more about `serverless-http`, please refer to the [serverless-http README](https://github.com/dougmoscrop/serverless-http). | ||
|
||
## Usage | ||
|
||
### Deployment | ||
|
||
Install dependencies with: | ||
|
||
``` | ||
npm install | ||
``` | ||
|
||
and then deploy with: | ||
|
||
``` | ||
serverless deploy | ||
``` | ||
|
||
After running deploy, you should see output similar to: | ||
|
||
``` | ||
Deploying "aws-node-express-api" to stage "dev" (us-east-1) | ||
✔ Service deployed to stack aws-node-express-api-dev (96s) | ||
endpoint: ANY - https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com | ||
functions: | ||
api: aws-node-express-api-dev-api (2.3 kB) | ||
``` | ||
|
||
_Note_: In current form, after deployment, your API is public and can be invoked by anyone. For production deployments, you might want to configure an authorizer. For details on how to do that, refer to [`httpApi` event docs](https://www.serverless.com/framework/docs/providers/aws/events/http-api/). | ||
|
||
### Invocation | ||
|
||
After successful deployment, you can call the created application via HTTP: | ||
|
||
``` | ||
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/ | ||
``` | ||
|
||
Which should result in the following response: | ||
|
||
```json | ||
{ "message": "Hello from root!" } | ||
``` | ||
|
||
### Local development | ||
|
||
The easiest way to develop and test your function is to use the `dev` command: | ||
|
||
``` | ||
serverless dev | ||
``` | ||
|
||
This will start a local emulator of AWS Lambda and tunnel your requests to and from AWS Lambda, allowing you to interact with your function as if it were running in the cloud. | ||
|
||
Now you can invoke the function as before, but this time the function will be executed locally. Now you can develop your function locally, invoke it, and see the results immediately without having to re-deploy. | ||
|
||
When you are done developing, don't forget to run `serverless deploy` to deploy the function to the cloud. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
const serverless = require("serverless-http"); | ||
const express = require("express"); | ||
const AWS = require("aws-sdk"); | ||
const { PDFDocument } = require("pdf-lib"); | ||
|
||
const app = express(); | ||
const S3 = new AWS.S3(); | ||
|
||
app.use(express.json()); | ||
|
||
app.post("/generate", async (req, res) => { | ||
try { | ||
const { userData, matchData } = req.body; | ||
|
||
if (!userData || !matchData) { | ||
return res.status(400).json({ message: "Datos incompletos" }); | ||
} | ||
|
||
const { home, away } = matchData.teams; | ||
|
||
const pdfDoc = await PDFDocument.create(); | ||
const page = pdfDoc.addPage([600, 400]); | ||
|
||
page.drawText(`Usuario: ${userData.name}`, { x: 50, y: 330 }); | ||
page.drawText(`Correo: ${userData.email}`, { x: 50, y: 310 }); | ||
page.drawText(`Partido:`, { x: 50, y: 290 }); | ||
page.drawText(`Equipo Local: ${home.name}`, { x: 50, y: 270 }); | ||
page.drawText(`Equipo Visitante: ${away.name}`, { x: 50, y: 250 }); | ||
page.drawText(`Fecha del Partido: ${matchData.date}`, { x: 50, y: 230 }); | ||
page.drawText(`Monto de la compra: ${matchData.amount}`, { x: 50, y: 210 }); | ||
|
||
const pdfBytes = await pdfDoc.save(); | ||
|
||
const params = { | ||
Bucket: 'node-craft', | ||
Key: `boletas/${userData.name}_${Date.now()}.pdf`, | ||
Body: Buffer.from(pdfBytes), | ||
ContentType: 'application/pdf', | ||
}; | ||
|
||
const data = await S3.upload(params).promise(); | ||
|
||
res.status(200).json({ | ||
message: "Boleta generada con éxito", | ||
pdfUrl: data.Location | ||
}); | ||
} catch (error) { | ||
console.error("Error:", error); | ||
res.status(500).json({ message: "Error generando la boleta", error }); | ||
} | ||
}); | ||
|
||
module.exports.handler = serverless(app); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"name": "pdf-generator", | ||
"version": "1.0.0", | ||
"description": "", | ||
"dependencies": { | ||
"aws-sdk": "^2.1691.0", | ||
"express": "^4.19.2", | ||
"pdf-lib": "^1.17.1", | ||
"serverless-http": "^3.2.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
org: kev123 | ||
service: pdf-generator | ||
|
||
provider: | ||
name: aws | ||
runtime: nodejs20.x | ||
region: us-east-1 | ||
|
||
iamRoleStatements: | ||
- Effect: "Allow" | ||
Action: | ||
- "s3:PutObject" | ||
- "s3:GetObject" | ||
Resource: "arn:aws:s3:::node-craft/*" | ||
|
||
functions: | ||
api: | ||
handler: handler.handler | ||
events: | ||
- httpApi: | ||
path: /generate | ||
method: post |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import axios from 'axios'; | ||
|
||
export const generateInvoice = async (userData, matchData) => { | ||
try { | ||
|
||
const response = await axios.post('https://axp8mrrbk1.execute-api.us-east-1.amazonaws.com/generate', { | ||
userData: { | ||
name: userData.name, | ||
email: userData.email | ||
}, | ||
matchData: { | ||
teams: { | ||
home: { | ||
id: matchData.teams.home.id, | ||
name: matchData.teams.home.name, | ||
logo: matchData.teams.home.logo, | ||
winner: matchData.teams.home.winner | ||
}, | ||
away: { | ||
id: matchData.teams.away.id, | ||
name: matchData.teams.away.name, | ||
logo: matchData.teams.away.logo, | ||
winner: matchData.teams.away.winner | ||
} | ||
}, | ||
date: matchData.date, | ||
amount: matchData.amount | ||
} | ||
}); | ||
|
||
if (response.data && response.data.pdfUrl) { | ||
return response.data.pdfUrl; | ||
} else { | ||
throw new Error('Error generando la boleta'); | ||
} | ||
} catch (error) { | ||
console.error('Error generando la boleta:', error); | ||
throw error; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters