Skip to content

Commit

Permalink
Adição do projeto de Web API com Serverless da Ana Neri no Youtube
Browse files Browse the repository at this point in the history
  • Loading branch information
dsfb committed Mar 7, 2024
1 parent 9a4d804 commit c38844d
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# package directories
node_modules
jspm_packages

# Serverless directories
.serverless
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
FROM node:16-alpine

RUN mkdir -p /home/node/app/node_modules && chown -R node:node /home/node/app

WORKDIR /home/node/app

COPY --chown=node:node package*.json ./

USER node

RUN ls -la /home/node/app

RUN npm install serverless

RUN npm install serverless-offline

RUN npm install mongodb

COPY --chown=node:node . .

EXPOSE 3000

CMD ["npm", "run", "start"]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
8
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
version: '3'
services:
app:
build: .
volumes:
- .:/app
networks:
- mongodb_network
ports:
- 3000:3000
mongodb:
container_name: mongodb
image: mongodb/mongodb-community-server:6.0-ubi8
volumes:
- mongodata:/data/db
networks:
- mongodb_network
ports:
- 27017:27017
networks:
mongodb_network:
driver: bridge
volumes:
mongodata:
driver: local
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
https://stackoverflow.com/questions/44781889/permission-error-with-mongo-when-running-docker/73565165#73565165

https://stackoverflow.com/questions/49937772/docker-volume-must-be-a-mapping-not-a-string

localhost:3000/dev/data

https://www.mongodb.com/community/forums/t/asp-net-mongo-db-system-timeoutexception-a-timeout-occurred-after-30000ms-in-docker-compose/180483

https://stackoverflow.com/questions/74165616/timeout-mongodb-with-docker

Link do video deste projeto no youtube: https://www.youtube.com/watch?v=3A8APu9FiSI
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "my-service",
"version": "1.0.0",
"description": "",
"main": "handler.js",
"scripts": {
"start": "npx serverless offline start --host 0.0.0.0",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
serverless create --template aws-nodejs --path my-service
npm init --y
npm install -g serverless serverless-offline mongodb
npm run start
docker-compose up --build
docker system prune -a --volumes

docker system prune -a --volumes -f; clear; docker-compose up --build
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
service: my-service
frameworkVersion: "3"

plugins:
- serverless-offline

provider:
name: aws
runtime: nodejs18.x
timeout: 30

functions:
putData:
handler: src/handler.putData
events:
- http:
path: data
method: put
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';
const MongoClient = require('mongodb').MongoClient;

module.exports.putData = async(event) => {
const client = new MongoClient('mongodb://mongodb:27017', {useUnifiedTopology: true});
await client.connect();

const db = client.db('mydb');
const collection = db.collection('mycollection');

const data = JSON.parse(event.body);
await collection.insertOne(data);

await client.close();

return {
statusCode: 200,
body: JSON.stringify({message: 'VEIO AQUIIIIIIII'})
};
};

0 comments on commit c38844d

Please sign in to comment.