diff --git a/DOCKER.md b/DOCKER.md new file mode 100644 index 0000000..f88424a --- /dev/null +++ b/DOCKER.md @@ -0,0 +1,129 @@ +# Docker readme + +## Get the source code + +Get the code and checkout the branch + +```sh +git clone https://github.com/rastaman/bon-appetit-server.git +git checkout docker_integration +``` + +## Build and run + +```sh +$ docker-compose up +... +``` + +In another shell: + +```sh +$ curl -s -H 'Content-Type: application/json' http://127.0.0.1:3001/bon-appetit/api/v1/restaurant | jq . +{ + "restaurants": [] +} +``` + +### Using the real ip + +Either find it or you can try this: + +```sh +$ export IPV4=$(ifconfig | grep "inet " | grep -v 127 | cut -d " " -f 2 | head -1) +... +$ curl -s -H 'Content-Type: application/json' http://${IPV4}:3001/bon-appetit/api/v1/restaurant | jq . +{ + "restaurants": [] +} +``` + +## Feeding datas + +```sh +$ for i in restaurant review event; do + curl -i -H 'Content-Type: application/json' -X POST -d @src/json-models/${i}s.json http://${IPV4}:3001/bon-appetit/api/v1/${i}/batch +done +HTTP/1.1 100 Continue + +HTTP/1.1 201 Created +X-Powered-By: Express +Access-Control-Allow-Origin: * +Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept +Content-Type: application/json; charset=utf-8 +Content-Length: 46 +ETag: W/"2e-+BC4cVbW34Arrk0xXWN0mHD7ugM" +Date: Wed, 20 Nov 2019 19:22:51 GMT +Connection: keep-alive + +{"message":"Restaurant created with Success!"}HTTP/1.1 100 Continue + +HTTP/1.1 201 Created +X-Powered-By: Express +Access-Control-Allow-Origin: * +Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept +Content-Type: application/json; charset=utf-8 +Content-Length: 42 +ETag: W/"2a-xDE1zrIrgK8aQf9y+iu4dwQByb4" +Date: Wed, 20 Nov 2019 19:22:52 GMT +Connection: keep-alive + +{"message":"Review created with Success!"}HTTP/1.1 100 Continue + +HTTP/1.1 201 Created +X-Powered-By: Express +Access-Control-Allow-Origin: * +Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept +Content-Type: application/json; charset=utf-8 +Content-Length: 42 +ETag: W/"2a-Mfe60Vp8r62DX2PlSD3YLDPmzQM" +Date: Wed, 20 Nov 2019 19:22:52 GMT +Connection: keep-alive + +{"message":"Events created with Success!"} +$ export DISHES=$(find src/json-models/dishes -name "*.json" | gpaste -d" " -s) +... +for d in $DISHES; do + curl -i -H 'Content-Type: application/json' -X POST -d @${d} http://${IPV4}:3001/bon-appetit/api/v1/dish/batch +done +``` + +### Checking that dishes exists + +```sh +$ curl -s -H 'Content-Type: application/json' http://${IPV4}:3001/bon-appetit/api/v1/dish | jq . | head -20 +{ + "dishes": [ + { + "ingredients": [ + "200g risotto rice", + "1 large garlic clove", + "2 spring onions", + "900ml low-salt chicken stock", + "120g frozen peas", + "1 large courgette", + "50g grated medium", + "140g cooked prawns" + ], + "imageURL": "https://s3-sa-east-1.amazonaws.com/bon-appetit-resources/dishes/homemade/large/toddler-recipe-microwave-courgette-and-pea-risotto-prawns.jpeg", + "mediumImageURL": "https://s3-sa-east-1.amazonaws.com/bon-appetit-resources/dishes/homemade/medium/toddler-recipe-microwave-courgette-and-pea-risotto-prawns.jpeg", + "thumbnailImageURL": "https://s3-sa-east-1.amazonaws.com/bon-appetit-resources/dishes/homemade/thumbnail/toddler-recipe-microwave-courgette-and-pea-risotto-prawns.jpeg", + "title": "Microwave courgette and pea risotto with prawns", + "description": "If you're after a family-friendly meal that takes under 30 minutes, try this courgette and pea risotto.", + "type": "Homemade", + "stars": 4, +... +``` + +### Rebuilding the backend + +```sh +docker-compose build backend +docker-compose up +``` + +## References + +- [Managing MongoDB on docker with docker-compose - Faun - Medium](https://medium.com/faun/managing-mongodb-on-docker-with-docker-compose-26bf8a0bbae3) +- [Use multi-stage builds | Docker Documentation](https://docs.docker.com/develop/develop-images/multistage-build/) +- [Dockerizing a Node.js web app | Node.js](https://nodejs.org/de/docs/guides/nodejs-docker-webapp/) diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..bd0603c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,30 @@ +FROM node:10 +# Create app directory +WORKDIR /usr/src/app +# Install app dependencies +# A wildcard is used to ensure both package.json AND package-lock.json are copied +# where available (npm@5+) +COPY package*.json ./ + +RUN npm install +# If you are building your code for production +# RUN npm ci --only=production + +# Bundle app source +COPY . . + +EXPOSE 3001 + +CMD [ "node", "./node_modules/.bin/pm2-runtime", "./src/bin" ] + +# FROM go +# WORKDIR /go/src/github.com/alexellis/href-counter/ +# RUN go get -d -v golang.org/x/net/html +# COPY app.go . +# RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app . + +# FROM alpine:latest +# RUN apk --no-cache add ca-certificates +# WORKDIR /root/ +# COPY --from=0 /go/src/github.com/alexellis/href-counter/app . +# CMD ["./app"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..25d18aa --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,12 @@ +version: '3' +services: + datastore: + image: "mongo:latest" + backend: + build: . + environment: + DATABASE_URL: mongodb://datastore:27017/bon-appetit + PORT: "3001" + NODE_ENV: production + ports: + - "3001:3001" diff --git a/src/controllers/dish-controller.js b/src/controllers/dish-controller.js index 19331d9..d29e88f 100644 --- a/src/controllers/dish-controller.js +++ b/src/controllers/dish-controller.js @@ -42,12 +42,15 @@ exports.createInBatch = async (req, res, next) => { try { await DishDAO.createInBatch(req.body); - return res.status(201); + return res.status(201).json({ + message: "Dish created with Success!" + }); } catch (error) { - return res.status(500).send({ - error + debug(error); + + return res.status(500).json({ + message: "Error when trying to Create Dishes." }); - } }; exports.readAll = async (req, res, next) => { diff --git a/src/controllers/review-controller.js b/src/controllers/review-controller.js index 2896283..f3b1f62 100644 --- a/src/controllers/review-controller.js +++ b/src/controllers/review-controller.js @@ -18,11 +18,16 @@ exports.createInBatch = async (req, res, next) => { try { await ReviewDAO.createInBatch(req.body); - return res.status(201); + return res.status(201).json({ + message: "Review created with Success!" + }); } catch (error) { - return res.status(500).send({ - error + debug(error); + + return res.status(500).json({ + message: "Error when trying to Create Reviews." }); + } };