- node v7 https://nodejs.org/en/
- mongodb 3.2+ https://www.mongodb.com/
- open terminal to project root dir ,Install node dependencies using
npm install - check code with
npm lint - To create random test data run
npm run testdata - run server
npm start
Configuration files are located under config dir.
See https://github.com/lorenwest/node-config/wiki/Configuration-Files
Some settings (e.g AWS credentials) must be set only as Environment variables. Check file custom-environment-variables.json for full list.
| Name | Description |
|---|---|
LOG_LEVEL |
The logging level for the api's |
PORT |
The port to listen |
AUTH0_CLIENT_ID |
The auth0 client id |
JWT_SECRET |
The jwt secret |
mail.SMTP_HOST |
The smtp hostname |
mail.SMTP_PORT |
The smtp port |
mail.EMAIL_FROM |
The from email address |
mail.SMTP_USERNAME |
The smtp username |
mail.SMTP_PASSWORD |
The smtp password |
AWS_ACCESS_KEY |
The AWS Access Key see http://docs.aws.amazon.com/general/latest/gr/managing-aws-access-keys.html |
AWS_SECRET_KEY |
The AWS Secret Key |
AWS_REGION |
Your AWS access region. You should use all services from the same region (Currently only S3 is used). |
S3_BUCKET |
The S3 bucket name for file upload. |
AWS settings are optional to set, but file upload will not working (you can use other features).
mailgun is used as mail service.
- Create an account on mailgun.
- By default a sandbox domain is created. Click on sandbox domain and copy the
Default SMTP LoginandDefault Passwordand set them as environment variables. export SMTP_USERNAME= export SMTP_PASSWORD= - The host and port are configured for mailgun.
When we create an account on mailgun, by default only sandbox domain is enabled. To successfully send email from this sandbox domain you have to add the user to authorized recipients list.
#test
- open postman
- import
test/Provider-dsp.postman_collection,test/Provider-dsp-env.postman_environment.json. - test data create 6 provider with user , use username
provider1-provder6, password123456login , when login success ,the token will be injected to postman env. - test other api endpoints.
import test/NFZ.postman_collection.json
it contains only endpoints for No Fly Zone endpoints
I added three query parameters returnNFZ, nfzFields and nfzLimit to API PUT /drones/{id} for getting the array of violated No fly zones in the response data.
A Sample usage to get the violated no fly zones with fields description, isActive, isPermanent:
curl -X PUT -H "Content-Type: application/json" -d '{ "lat":38.90709540316193, "lng":-77.03920125961304 }' "$URL/drones/5866f36af66a5654a0816991?returnNFZ=true&nfzFields=description,isActive,isPermanent"
The response data will contain an extra field noFlyZones, which contains an array of NoFlyZone that the drone has violated.
You could specified the returned fields of noFlyZones by nfzFields parameter. If you omit the nfzFields, all fields except the location will be returned.
The parameter nfzLimit is for limit the number of returned NoFlyZones, if it is omitted, then all the NoFlyZones are returned.
Note: the _id of the NoFlyZone is always returned.
The approach to get the array of violated no fly zones is based on the NoFlyZoneService.search, the steps are:
- Define the search criteria: be active, matched time, geometry is "Point" type, the point coordinate is the drone's location.
{ isActive: true, matchTime: true, geometry: { type: 'Point', coordinates: drone.currentLocation, }, projFields: ['circle', 'description', 'startTime', 'endTime', 'isActive', 'isPermanent', 'mission'], } - Call
NoFlyZoneService.searchto search no fly zones. Since we only needs to get part of the fields ofNoFlyZone, I added a fieldprojFieldsto the search criteria. The fieldprojFieldscould search in MongoDB with projection, which helps to improve the performance. Additionally, theprojFieldscould be overrided bynfzFieldsif it is provided.nfzFields=description,isActive,isPermanentwill be converted to['description', 'isActive', 'isPermanent']. - Retrieve the array of no fly zones from the returned object, and add the
itemsfield to the response of this API. It is added to the fieldnoFlyZones.
I added another three query parameters nearDronesMaxDist, nearDroneFields and nearDronesLimit to API PUT /drones/{id} for getting the nearest drones.
A Sample usage to get 100 nearest drones within 1000 meters:
curl -X PUT -H "Content-Type: application/json" -d '{ "lat":38.90709540316193, "lng":-77.03920125961304 }' "$URL/drones/5866f36af66a5654a0816991?nearDronesMaxDist=1000&nearDronesLimit=100&nearDroneFields=currentLocation,status,name,description"
The response data will contain an extra field nearestDrones. This field contains an array of Drones (100 at most) that are ordered by distance.
To avoid return all fields of drones in the response data, you could specify the fields to be returned by nearDroneFields. The example above returns the fields currentLocation, status, name, description, _id.
If the nearDronesMaxDist is omitted or 0, no nearest drones will be contained in the response data.
If the nearDronesLimit is omitted or 0, only 1 nearest drone will be returned.
If the nearDroneFields is omitted, all the fields of drones will be returned, which is not necessary. So this field is suggested to be provided.
A new field dist is contained in the response data for indicating the distance (in meter) between the drone and the current drone.
Note: the _id of Drone is always returned.
The approach to get the array of nearest drones is based on the geospatial of MongoDB. The steps are:
- Add the
2dsphereindex toDrone.currentLocation, since it is not a required field, we need to specifysparse: true. - Rebuild the index. Since the data are for test, we could regenerate test data for rebuilding the index for simplicity.
- As we need to add
distfield to the response data, we use$geoNearinstead of$nearSphere. Set the$geoNearoption aswhich means to search by{ near: { type: 'Point', coordinates: drone.currentLocation, }, distanceField: 'dist', maxDistance: nearDronesMaxDist, spherical: true, query: { _id: { $ne: ObjectId(id) }, }, }Pointand filter the current drone itself. - Add
limitand$projectaccording to the query parameternearDronesLimit,nearDroneFields. - Aggregate
Droneand add returned array to the response of this API. The array is added to the fieldnearestDrones.
you also can export those values before run(data from forum).
export AUTH0_CLIENT_ID="3CGKzjS2nVSqHxHHE64RhvvKY6e0TYpK"
export JWT_SECRET="fJtXfFYt-F9iees7CSw8rOOr-tYsJocoZTz3pLF5NynamB07JFPeFOEuzfbcT7SD"
export MONGOLAB_URI="mongodb://topcoder:[email protected]:47777/dsp1"
export SMTP_HOST="smtp.gmail.com"
export SMTP_PORT="465"
export SMTP_USERNAME="youremail"
export SMTP_PASSWORD="yourpassword"
export EMAIL_FROM="[email protected]"
- Open S3 console https://console.aws.amazon.com/s3
- Create Bucket
- Check
Region NametoCodemapping here http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-regions-availability-zones.html and setAWS_REGION - Select tab Properties from the top right menu
- Expand Permissions tab
- Click on Add bucket policy and set below policy
{
"Version": "2008-10-17",
"Statement": [
{
"Sid": "AllowPublicRead",
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::dsp-21472/*"
}
]
}
Update dsp-21472 to your bucket name.
- Click on Add CORS Configuration and set below policy
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>PUT</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>DELETE</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>
In production mode, you should restrict AllowedOrigin.