Skip to content
This repository was archived by the owner on May 31, 2022. It is now read-only.

Commit 7949966

Browse files
committed
Update dev deps; eslint and cs fixes; docker update; node update to v12; cs hooks; k8s resource declaration and probes
1 parent 0c14455 commit 7949966

35 files changed

+6749
-439
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ charset = utf-8
1616
# 2 space indentation
1717
[**/*.js]
1818
indent_style = space
19-
indent_size = 2
19+
indent_size = 4
2020

2121
[*.yml]
2222
indent_style = space

.eslintrc.yml

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,39 @@
11
---
2-
extends: airbnb
2+
extends: [airbnb, plugin:mocha/recommended]
33
env:
44
node: true
55
es6: true
6-
parser: babel-eslint
76
parserOptions:
87
sourceType: module
9-
ecmaVersion: 6
8+
ecmaVersion: 2018
9+
plugins: [mocha]
1010
globals:
1111
describe: true
1212
it: true
1313
before: true
1414
after: true
15+
beforeEach: true
16+
afterEach: true
1517
rules:
1618
max-len: [1, 200, 2, {"ignoreUrls":true}]
1719
curly: [2, "multi-line"]
1820
comma-dangle: [0, always-multiline]
19-
no-underscore-dangle: 0
21+
no-plusplus: 0
2022
eqeqeq: [2, "allow-null"]
2123
global-require: 0
2224
no-shadow: 1
2325
no-param-reassign: [2, { "props": false }]
24-
indent: [2, 4]
26+
indent: [2, 4, { "SwitchCase": 1 }]
2527
padded-blocks: [2, { "switches": "always", "classes": "always" }]
2628
quotes:
2729
- 2
2830
- single
2931
- allowTemplateLiterals: true
32+
no-underscore-dangle: ["error", { "allow": ["_id"] }]
33+
import/no-extraneous-dependencies: ["error", { devDependencies: ['app/test/**']}]
34+
no-await-in-loop: off
35+
mocha/no-mocha-arrows: [0]
36+
mocha/no-hooks-for-single-case: [0]
3037
settings:
3138
import/resolver:
3239
node:

.travis.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
language: node_js
2+
services:
3+
- mongodb
4+
- redis
5+
node_js:
6+
- "12"
7+
cache:
8+
directories:
9+
- "node_modules"
10+
env:
11+
global:
12+
- CC_TEST_REPORTER_ID=dabdd92208e41ebbec13b01f5e943a0dcf7ec40f4a98d6d0a8f811e8cca9b443
13+
- NODE_ENV=test
14+
- PORT=5037
15+
- API_VERSION=v1
16+
- CT_REGISTER_MODE=auto
17+
- LOCAL_URL=http://127.0.0.1:3001
18+
- CT_URL=http://127.0.0.1:9000
19+
- MONGO_PORT_27017_TCP_ADDR=127.0.0.1
20+
- CT_TOKEN=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6Im1pY3Jvc2VydmljZSIsImNyZWF0ZWRBdCI6IjIwMTYtMDktMTQifQ.IRCIRm1nfIQTfda_Wb6Pg-341zhV8soAgzw7dd5HxxQ
21+
- API_GATEWAY_URL=http://127.0.0.1:9000
22+
- API_GATEWAY_EXTERNAL_URL=http://127.0.0.1:9000
23+
- API_GATEWAY_QUEUE_URL=redis://127.0.0.1:6379
24+
- API_GATEWAY_QUEUE_NAME=mail
25+
26+
before_script:
27+
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
28+
- chmod +x ./cc-test-reporter
29+
- ./cc-test-reporter before-build
30+
script:
31+
- yarn test
32+
- yarn run coverage
33+
after_script:
34+
- ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT

Dockerfile

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
FROM node:8.1-alpine
2-
MAINTAINER raul.requero@vizzuality.com
1+
FROM node:12-alpine
2+
MAINTAINER info@vizzuality.com
33

44
ENV NAME fw-teams
55
ENV USER fw-teams
@@ -9,19 +9,20 @@ RUN apk update && apk upgrade && \
99

1010
RUN addgroup $USER && adduser -s /bin/bash -D -G $USER $USER
1111

12-
RUN npm install --unsafe-perm -g grunt-cli bunyan
12+
RUN yarn global add grunt-cli bunyan
1313

1414
RUN mkdir -p /opt/$NAME
1515
COPY package.json /opt/$NAME/package.json
16-
RUN cd /opt/$NAME && npm install
16+
COPY yarn.lock /opt/$NAME/yarn.lock
17+
RUN cd /opt/$NAME && yarn
1718

1819
COPY entrypoint.sh /opt/$NAME/entrypoint.sh
1920
COPY config /opt/$NAME/config
2021

2122
WORKDIR /opt/$NAME
2223

2324
COPY ./app /opt/$NAME/app
24-
RUN chown $USER:$USER /opt/$NAME
25+
RUN chown -R $USER:$USER /opt/$NAME
2526

2627
# Tell Docker we are going to use this ports
2728
EXPOSE 3035

README.md

Lines changed: 66 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,82 @@
1-
# Node Skeleton Microservice
1+
# FW teams Microservice
22

33

44
This repository is the node skeleton microservice to create node microservice for WRI API
55

6-
1. [Getting Started](#getting-started)
6+
## Dependencies
77

8-
## Getting Started
8+
You will need [Control Tower](https://github.com/control-tower/control-tower) up and running - either natively or with Docker. Refer to the project's README for information on how to set it up.
99

10-
### OS X
10+
The FW teams microservice is built using [Node.js](https://nodejs.org/en/), and can be executed either natively or using Docker, each of which has its own set of requirements.
1111

12-
**First, make sure that you have the [API gateway running
13-
locally](https://github.com/control-tower/control-tower).**
12+
Native execution requires:
13+
- [Node.js](https://nodejs.org/en/)
14+
- [MongoDB](https://www.mongodb.com/)
1415

15-
We're using Docker which, luckily for you, means that getting the
16-
application running locally should be fairly painless. First, make sure
17-
that you have [Docker Compose](https://docs.docker.com/compose/install/)
18-
installed on your machine.
16+
Execution using Docker requires:
17+
- [Docker](https://www.docker.com/)
18+
- [Docker Compose](https://docs.docker.com/compose/)
19+
20+
Dependencies on other Microservices:
21+
- [GFW User](https://github.com/gfw-api/gfw-user-api/)
22+
23+
## Getting started
24+
25+
Start by cloning the repository from github to your execution environment
26+
27+
```
28+
git clone https://github.com/gfw-api/fw-teams.git && cd fw-team
29+
```
30+
31+
After that, follow one of the instructions below:
32+
33+
### Using native execution
34+
35+
1 - Set up your environment variables. See `dev.env.sample` for a list of variables you should set, which are described in detail in [this section](#environment-variables) of the documentation. Native execution will NOT load the `dev.env` file content, so you need to use another way to define those values
36+
37+
2 - Install node dependencies using yarn:
38+
```
39+
yarn
40+
```
41+
42+
3 - Start the application server:
43+
```
44+
yarn start
45+
```
46+
47+
The endpoints provided by this microservice should now be available through Control Tower's URL.
48+
49+
### Using Docker
50+
51+
1 - Create and complete your `dev.env` file with your configuration. The meaning of the variables is available in this [section](#configuration-environment-variables). You can find an example `dev.env.sample` file in the project root.
52+
53+
2 - Execute the following command to run Control tower:
1954

2055
```
21-
git clone https://github.com/gfw-api/fw-teams
22-
cd fw-teams
2356
./team.sh develop
24-
./team.sh test
25-
```text
57+
```
58+
59+
The endpoints provided by this microservice should now be available through Control Tower's URL.
60+
61+
## Testing
62+
63+
There are two ways to run the included tests:
2664

27-
You can now access the microservice through the CT gateway.
65+
### Using native execution
2866

67+
Follow the instruction above for setting up the runtime environment for native execution, then run:
68+
```
69+
yarn test
2970
```
3071

31-
### Configuration
72+
### Using Docker
73+
74+
Follow the instruction above for setting up the runtime environment for Docker execution, then run:
75+
```
76+
./team.sh test
77+
```
78+
79+
## Configuration
3280

3381
It is necessary to define these environment variables:
3482

@@ -38,9 +86,10 @@ It is necessary to define these environment variables:
3886
* API_GATEWAY_URI => Gateway Service API URL
3987
* API_GATEWAY_EXTERNAL_URI
4088
* API_GATEWAY_QUEUE_URL => Url of async queue
41-
* API_GATEWAY_QUEUE_PROVIDER => redis (only support redis)
4289
* API_GATEWAY_QUEUE_NAME => mail
4390

91+
You can optionally set other variables, see [this file](config/custom-environment-variables.json) for an extended list.
92+
4493
## Quick Overview
4594

4695
### Teams Entity

app/Gruntfile.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
21
module.exports = (grunt) => {
32

43
grunt.file.setBase('..');
4+
// eslint-disable-next-line import/no-extraneous-dependencies
55
require('load-grunt-tasks')(grunt);
66

77
grunt.initConfig({
@@ -14,14 +14,6 @@ module.exports = (grunt) => {
1414
port: process.env.PORT,
1515
output: 'started'
1616
}
17-
},
18-
test: {
19-
options: {
20-
script: 'app/index.js',
21-
node_env: 'test',
22-
port: 5000,
23-
output: 'started'
24-
}
2517
}
2618
},
2719

@@ -30,6 +22,7 @@ module.exports = (grunt) => {
3022
options: {
3123
reporter: 'spec',
3224
quiet: false,
25+
timeout: 10000,
3326
clearRequireCache: true,
3427
},
3528
src: ['app/test/e2e/**/*.spec.js']
@@ -59,15 +52,33 @@ module.exports = (grunt) => {
5952
}
6053
},
6154

55+
},
56+
57+
nyc: {
58+
cover: {
59+
options: {
60+
include: ['app/src/**'],
61+
exclude: '*.test.*',
62+
reporter: ['lcov', 'text-summary'],
63+
reportDir: 'coverage',
64+
all: true
65+
},
66+
cmd: false,
67+
args: ['grunt', '--gruntfile', 'app/Gruntfile.js', 'mochaTest:e2e']
68+
}
6269
}
6370
});
6471

65-
grunt.registerTask('e2eTest', ['express:test', 'mochaTest:e2e']);
72+
grunt.registerTask('e2eTest', ['mochaTest:e2e']);
73+
74+
grunt.registerTask('e2eTestCoverage', ['mocha_nyc:coverage']);
6675

6776
grunt.registerTask('e2eTest-watch', ['watch:e2eTest']);
6877

6978
grunt.registerTask('serve', ['express:dev', 'watch']);
7079

7180
grunt.registerTask('default', 'serve');
7281

82+
grunt.loadNpmTasks('grunt-simple-nyc');
83+
7384
};

app/src/app.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ const koaLogger = require('koa-logger');
44
const config = require('config');
55
const loader = require('loader');
66
const mongoose = require('mongoose');
7+
const koaSimpleHealthCheck = require('koa-simple-healthcheck');
8+
79
mongoose.Promise = Promise;
810
const ctRegisterMicroservice = require('ct-register-microservice-node');
911
const ErrorSerializer = require('serializers/error.serializer');
12+
1013
const mongoUri = process.env.MONGO_URI || `mongodb://${config.get('mongodb.host')}:${config.get('mongodb.port')}/${config.get('mongodb.database')}`;
1114
const validate = require('koa-validate');
1215

@@ -39,11 +42,16 @@ app.use(async (ctx, next) => {
3942
try {
4043
error = JSON.parse(inErr);
4144
} catch (e) {
42-
logger.error('Error parse');
45+
logger.debug('Could not parse error message - is it JSON?: ', inErr);
4346
error = inErr;
4447
}
4548
ctx.status = error.status || ctx.status || 500;
46-
logger.error(error);
49+
if (ctx.status >= 500) {
50+
logger.error(error);
51+
} else {
52+
logger.info(error);
53+
}
54+
4755
ctx.body = ErrorSerializer.serializeError(ctx.status, error.message);
4856
if (process.env.NODE_ENV === 'prod' && ctx.status === 500) {
4957
ctx.body = 'Unexpected error';
@@ -53,6 +61,7 @@ app.use(async (ctx, next) => {
5361
});
5462

5563
app.use(koaLogger());
64+
app.use(koaSimpleHealthCheck());
5665

5766
loader.loadRoutes(app);
5867

@@ -69,7 +78,8 @@ const instance = app.listen(process.env.PORT, () => {
6978
url: process.env.LOCAL_URL,
7079
token: process.env.CT_TOKEN,
7180
active: true,
72-
}).then(() => {}, (error) => {
81+
}).then(() => {
82+
}, (error) => {
7383
logger.error(error);
7484
process.exit(1);
7585
});

app/src/loader.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
/* eslint-disable import/no-dynamic-require */
12
const fs = require('fs');
3+
24
const routersPath = `${__dirname}/routes`;
35
const logger = require('logger');
46
const mount = require('koa-mount');

app/src/models/team.model.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
'use strict';
1+
22
const mongoose = require('mongoose');
3-
const Schema = mongoose.Schema;
3+
4+
const { Schema } = mongoose;
45

56
const Team = new Schema({
67
name: { type: String, required: false, trim: true },

0 commit comments

Comments
 (0)