Skip to content

Commit a3f8e5b

Browse files
committed
First commit
0 parents  commit a3f8e5b

19 files changed

+4982
-0
lines changed

.eslintrc.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"plugins": ["node"],
3+
"extends": ["eslint:recommended", "plugin:node/recommended"],
4+
"rules": {
5+
"node/exports-style": ["error", "module.exports"]
6+
},
7+
"globals": {
8+
"describe": true,
9+
"it": true,
10+
"expect": true,
11+
"app": true,
12+
"beforeEach": true,
13+
"request": true,
14+
"before": true,
15+
"supertest": true,
16+
"afterEach": true,
17+
"context": true,
18+
"setupApp": true
19+
}
20+
}

.gitignore

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
8+
# Runtime data
9+
pids
10+
*.pid
11+
*.seed
12+
*.pid.lock
13+
14+
# Directory for instrumented libs generated by jscoverage/JSCover
15+
lib-cov
16+
17+
# Coverage directory used by tools like istanbul
18+
coverage
19+
20+
# nyc test coverage
21+
.nyc_output
22+
23+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
24+
.grunt
25+
26+
# Bower dependency directory (https://bower.io/)
27+
bower_components
28+
29+
# node-waf configuration
30+
.lock-wscript
31+
32+
# Compiled binary addons (https://nodejs.org/api/addons.html)
33+
build/Release
34+
35+
# Dependency directories
36+
node_modules/
37+
jspm_packages/
38+
39+
# Typescript v1 declaration files
40+
typings/
41+
42+
# Optional npm cache directory
43+
.npm
44+
45+
# Optional eslint cache
46+
.eslintcache
47+
48+
# Optional REPL history
49+
.node_repl_history
50+
51+
# Output of 'npm pack'
52+
*.tgz
53+
54+
# Yarn Integrity file
55+
.yarn-integrity
56+
57+
# dotenv environment variables file
58+
.env

.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
8.4

Dockerfile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM node:8.4.0
2+
3+
ENV HOME=/home/app
4+
5+
COPY package.json package-lock.json $HOME/node_docker/
6+
7+
WORKDIR $HOME/node_docker
8+
9+
RUN npm install --silent --progress=false
10+
11+
COPY . $HOME/node_docker
12+
13+
CMD ["npm", "start"]

README.md

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
# NASA NEO (Near Earth Objects) API
2+
3+
This API is built on top of official [NASA NEO API](https://api.nasa.gov/neo/?api_key=N7LkblDsc5aen05FJqBQ8wU4qSdmsftwJagVK7UD) allowing custom queries about hazardous asteroids.
4+
5+
## Example
6+
7+
### GET all possible hazardous asteroids
8+
9+
```shell
10+
curl --request GET \
11+
--url http://localhost:3000/neo/hazardous \
12+
--header 'content-type: application/json' \
13+
```
14+
15+
response:
16+
17+
```json
18+
[
19+
{
20+
"_id": "59bafeb5fea87d001c3c011e",
21+
"__v": 0,
22+
"date": "2017-09-14T00:00:00.000Z",
23+
"reference": "2088254",
24+
"speed": 59540.7422645489,
25+
"is_hazardous": true
26+
},
27+
{
28+
"_id": "59bafeb5fea87d001c3c011f",
29+
"__v": 0,
30+
"date": "2017-09-14T00:00:00.000Z",
31+
"reference": "2310560",
32+
"speed": 77331.2021001263,
33+
"is_hazardous": true
34+
},
35+
{
36+
"_id": "59bafeb5fea87d001c3c0135",
37+
"__v": 0,
38+
"date": "2017-09-11T00:00:00.000Z",
39+
"reference": "3507718",
40+
"speed": 43820.0818093472,
41+
"is_hazardous": true
42+
}
43+
]
44+
```
45+
46+
## Running the application
47+
48+
The application is built with Node.js and already has all environment configured with docker. To start the application you will need `docker` and `docker-compose` installed on the machine. Having that you may run:
49+
50+
```shell
51+
docker-compose up
52+
```
53+
54+
And then the application and database will be started:
55+
56+
```shell
57+
Starting nodenasa_mongodb_1 ...
58+
Starting nodenasa_mongodb_1 ... done
59+
Starting nodenasa_app_1 ...
60+
Starting nodenasa_app_1 ... done
61+
```
62+
63+
The application will be avaible on *PORT 3000* by default, but it's configurable via `docker-compose.yml` file as an environment variable.
64+
65+
### Running the application in development mode
66+
67+
Sometimes you would like to run the application in development mode. This project already has a prepared enviroment for that, you have to run:
68+
69+
```shell
70+
docker-compose -f docker-compose.development.yml up
71+
```
72+
73+
This docker compose file will start the application using [`nodemon`](https://github.com/remy/nodemon) and will share the local application code with the container. Any change on the hosts code will restart the application (whitout restarting the container because nodemon is taking care of that).
74+
75+
### Running the tests
76+
77+
To run the tests using docker you just have to run the following command:
78+
79+
```shell
80+
docker-compose -f docker-compose.test.yml up
81+
```
82+
83+
And then all tests will be ran and the status exit code will be 0 or 1 that means true or false.
84+
85+
86+
## Routes
87+
88+
This application includes many routes ralated to NEO operations that are:
89+
90+
### List all NEO Hazardous asteroids
91+
92+
The following request will filter all possible hazardous asteroids that includes the flag `is_hazardous: true`.
93+
94+
```curl
95+
curl --request GET \
96+
--url http://localhost:3000/neo/hazardous \
97+
```
98+
99+
### List the fastest asteroid
100+
101+
This route will return the fastest asteroid based on its `speed`
102+
103+
```curl
104+
curl --request GET \
105+
--url http://localhost:3000/neo/fastest \
106+
--header 'content-type: application/json' \
107+
```
108+
109+
The response will be something like:
110+
111+
```json
112+
[
113+
{
114+
"_id": "59bb010df4d9b7001c68a1d9",
115+
"__v": 0,
116+
"date": "2017-09-14T00:00:00.000Z",
117+
"reference": "3685816",
118+
"speed": 124405.6335547075,
119+
"is_hazardous": false
120+
}
121+
]
122+
```
123+
124+
Also is possible to filter only hazardous asteroids passing the parameter `hazardous=true` as the example below:
125+
126+
```curl
127+
curl --request GET \
128+
--url 'http://localhost:3000/neo/fastest?hazardous=true' \
129+
--header 'content-type: application/json' \
130+
```
131+
132+
### List the year with most asteroids
133+
134+
This route will return the year with most asteroids:
135+
136+
```curl
137+
curl --request GET \
138+
--url http://localhost:3000/neo/best-year \
139+
--header 'content-type: application/json' \
140+
```
141+
142+
This also suports `hazards=true|false` filter parameter.
143+
144+
### List the month with most asteroids
145+
146+
This route will return the month with most asteroids
147+
148+
```curl
149+
curl --request GET \
150+
--url http://localhost:3000/neo/best-month \
151+
--header 'content-type: application/json' \
152+
```
153+
154+
This also supports `hazards=true|false` filter parameter.
155+
156+
157+
## Populating the local database
158+
159+
By default `every time` the application is started it fetches the NASA api and stores the data from NEOs from the last **3 days**. It's configurable via enviroment variable: `FETCH_DAYS_FROM_NOW` (NASA app is limited to 7 days).
160+

config/database.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const mongoose = require('mongoose');
2+
const config = require('./environment');
3+
4+
mongoose.Promise = Promise;
5+
6+
const mongodbUrl = config.mongodbUrl;
7+
8+
const connect = () => mongoose.connect(mongodbUrl, {
9+
useMongoClient: true
10+
});
11+
12+
module.exports = {
13+
connect
14+
};

config/environment.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const config = {
2+
development: {
3+
mongodbUrl: process.env.MONGODB_URL || 'mongodb://mongodb/development',
4+
port: process.env.PORT || 3000
5+
},
6+
test: {
7+
mongodbUrl: process.env.MONGODB_URL || 'mongodb://mongodb/test',
8+
port: process.env.PORT || 3000
9+
}
10+
11+
};
12+
13+
const env = process.env.NODE_ENV;
14+
15+
module.exports = config[env];

docker-compose.development.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
version: '2'
2+
services:
3+
app:
4+
build: .
5+
environment:
6+
NODE_ENV: development
7+
command: node_modules/.bin/nodemon --exec npm start
8+
ports:
9+
- '3000:3000'
10+
tty: true
11+
depends_on:
12+
- mongodb
13+
volumes:
14+
- .:/home/app/node_docker
15+
- /home/app/node_docker/node_modules
16+
mongodb:
17+
image: mongo:3.2.5

docker-compose.test.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
version: '2'
2+
services:
3+
app:
4+
build: .
5+
environment:
6+
NODE_ENV: test
7+
command: npm test
8+
ports:
9+
- '3000:3000'
10+
tty: true
11+
depends_on:
12+
- mongodb
13+
mongodb:
14+
image: mongo:3.2.5

docker-compose.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
version: '2'
2+
services:
3+
app:
4+
build: .
5+
environment:
6+
NODE_ENV: development
7+
ports:
8+
- '3000:3000'
9+
tty: true
10+
depends_on:
11+
- mongodb
12+
mongodb:
13+
image: mongo:3.2.5

0 commit comments

Comments
 (0)