Skip to content

Commit

Permalink
Merge pull request #504 from garden-io/simple-project-node-service
Browse files Browse the repository at this point in the history
docs(examples): add node-service back to simple-project
  • Loading branch information
edvald authored Feb 1, 2019
2 parents 386350d + d256659 commit a50f2fd
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 0 deletions.
4 changes: 4 additions & 0 deletions examples/simple-project/services/node-service/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
Dockerfile
garden.yml
app.yaml
12 changes: 12 additions & 0 deletions examples/simple-project/services/node-service/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM node:9-alpine

ENV PORT=8080
EXPOSE ${PORT}
WORKDIR /app

ADD package.json /app
RUN npm install

ADD . /app

CMD ["npm", "start"]
27 changes: 27 additions & 0 deletions examples/simple-project/services/node-service/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const express = require('express');
const request = require('request-promise')
const app = express();

// Unless configured otherwise, the hostname is simply the service name
const goServiceEndpoint = `http://go-service/hello-go`;

app.get('/hello-node', (req, res) => res.send('Hello from Node service!'));

app.get('/call-go-service', (req, res) => {
// Query the go-service and return the response
request.get(goServiceEndpoint)
.then(message => {
res.json({
message,
})
})
.catch((err) => {
res.statusCode = 500
res.json({
error: err,
message: "Unable to reach service at " + goServiceEndpoint,
})
});
});

module.exports = { app }
24 changes: 24 additions & 0 deletions examples/simple-project/services/node-service/garden.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module:
name: node-service
description: Node service container
type: container
services:
- name: node-service
command: [npm, start]
ports:
- name: http
containerPort: 8080
ingresses:
- path: /hello-node
port: http
- path: /call-go-service
port: http
dependencies:
- go-service
tests:
- name: unit
args: [npm, test]
- name: integ
args: [npm, run, integ]
dependencies:
- go-service
3 changes: 3 additions & 0 deletions examples/simple-project/services/node-service/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const { app } = require('./app');

app.listen(process.env.PORT, '0.0.0.0', () => console.log('Node service started'));
22 changes: 22 additions & 0 deletions examples/simple-project/services/node-service/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "node-service",
"version": "1.0.0",
"description": "Simple Node.js docker service",
"main": "main.js",
"scripts": {
"start": "node main.js",
"test": "echo OK",
"integ": "node_modules/mocha/bin/mocha test/integ.js"
},
"author": "garden.io <[email protected]>",
"license": "ISC",
"dependencies": {
"express": "^4.16.2",
"request": "^2.83.0",
"request-promise": "^4.2.2"
},
"devDependencies": {
"mocha": "^5.1.1",
"supertest": "^3.0.0"
}
}
17 changes: 17 additions & 0 deletions examples/simple-project/services/node-service/test/integ.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const supertest = require("supertest")
const { app } = require("../app")

describe('GET /call-go-service', () => {
const agent = supertest.agent(app)

it('should respond with a message from go-service', (done) => {
agent
.get("/call-go-service")
.expect(200, { message: "Hello from Go!" })
.end((err) => {
if (err) return done(err)
done()
})
})
})

0 comments on commit a50f2fd

Please sign in to comment.