Skip to content

Commit

Permalink
Merge pull request #26 from urbanriskmap/dev
Browse files Browse the repository at this point in the history
Merge updates in dev into master
  • Loading branch information
tomasholderness authored Aug 22, 2017
2 parents 193c695 + 77a863e commit 4658a21
Show file tree
Hide file tree
Showing 180 changed files with 56,187 additions and 1,203 deletions.
18 changes: 18 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = {
"extends": ["eslint:recommended", "google"],
"parserOptions":
{
"ecmaVersion": 6,
"sourceType": "module"
},
"env":
{ "browser": false,
"es6": true,
"node": true,
"mocha": true
},
"rules":
{
"no-multi-str": "off" // not a problem in node apps
}
};
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
.DS_Store
.env
.idea
.swp
*.swp

# nyc test coverage
.nyc_output/

/coverage
/dist
/node_modules

*.log
package-lock.json
6 changes: 6 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"ignoreDelimiters": [
{ "start": "start-non-standard", "end": "end-non-standard" }
],
"esversion":6
}
18 changes: 14 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
dist: precise
language: node_js
node_js:
- "6.10.0"
sudo: false
sudo: true

addons:
postgresql: "9.5"
# apt:
# packages:
# - postgresql-9.5-postgis-2.3

branches:
only:
- master
- dev

notifications:
- server-object-refactor
- report-archive

before_install:
- if [[ `npm -v` != 3* ]]; then npm i -g npm@3; fi
- if [[ `npm -v` != 3* ]]; then ~/.nvm/versions/node/v4.1.1/bin/npm install -g [email protected]; fi
- export DATABASE=cognicity_server_testing ; git clone -b master https://github.com/urbanriskmap/cognicity-schema.git urbanriskmap/cognicity-schema && cd urbanriskmap/cognicity-schema && bash build/run.sh && cd -

install:
- npm install

after_success: npm run coverage
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ API Server for CogniCity
* API Documentation moved to dedicated wiki: https://docs.petabencana.id/

### v3.0.1
* Refactored server module for better testability
* Added tests for endpoints using unit.js and mocha
* Added coverage for tests using Istanbull, nyc and coveralls
* Added integration tests on Travis, including against cognicity-schema
* Extended data format of cards data object to collect different types of data
* Merged changes from Chennai deployment, for better deployment in new cities
55 changes: 46 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ API Server for CogniCity

[![Build Status](https://travis-ci.org/urbanriskmap/cognicity-server.svg?branch=master)](https://travis-ci.org/urbanriskmap/cognicity-server)

[![Coverage Status](https://coveralls.io/repos/github/urbanriskmap/cognicity-server/badge.svg?branch=server-object-refactor)](https://coveralls.io/github/urbanriskmap/cognicity-server?branch=server-object-refactor)

DOI for current stable release [v3.0.0](https://github.com/urbanriskmap/cognicity-server/releases/tag/v3.0.0)


[![DOI](https://zenodo.org/badge/73803254.svg)](https://zenodo.org/badge/latestdoi/73803254)

### Summary

This is the NodeJS server which runs the CogniCity Data API used by Urban Risk Map instances, such as [PetaBencana.id](https://petabencana.id) site.
Expand Down Expand Up @@ -31,28 +38,28 @@ Server configuration parameters are stored in a configuration file which is pars
* `AUTH0_AUDIENCE`: Data API to be authenticated
* `AUTH0_CLIENT_ID`: Auth0 client ID (NOTE: this is mandatory and no default value)
* `AUTH0_ISSUER`: Web address of Auth0 instance
* `AWS_REGION`: Region for AWS Infrastructure
* `AWS_S3_ACCESS_KEY_ID`: Access key ID for AWS S3 bucket
* `AWS_S3_SECRET_ACCESS_KEY`: Access key secret for AWS S3 bucket
* `AWS_S3_SIGNATURE_VERSION`: Version of AWS S3 signature to use
* `AUTH0_SECRET`: Auth0 secret (NOTE: this is mandatory and no default value)
* `BODY_LIMIT`: Maximum body size POST/PUT/PATCH (default: `100kb`)
* `CACHE`: Should caching be enabled? (default: `false`)
* `CACHE_DURATION_CARDS`: How long should cards be cached for? (default: '1 minute')
* `CACHE_DURATION_FLOODS`: How long should floods be cached for? (default: '1 hour')
* `CACHE_DURATION_FLOODS_STATES`: How long should flood states be cached for? (default: '1 hour')
* `CACHE_DURATION_INFRASTRUCTURE`: How long should infrastructure be cached for? (default: '1 hour')
* `COMPRESS`: Should the server gzip compress results? (default: `false`)
* `COMPRESS`: Should the server gzip compress results? Only works if CACHE is disabled. (default: `false`)
* `CORS`: Should Cross Object Resource Sharing (CORS) be enabled (default: `false`)
* `CORS_HEADERS`: CORS headers to use (default: `[Link]`)
* `PGHOST`: Postgres DB hostname (default: `127.0.0.1`)
* `PGDATABASE`: Postgres DB database name (default: `cognicity`)
* `PGPASSWORD`: Postgres DB password (default: `p@ssw0rd`)
* `PGPORT`: Postgres DB port (default: `5432`)
* `PGSSL`: SSL enabled on Postgres DB connection? (default: `false`)
* `PGTIMEOUT`: Max duration on DB calls before timeout (in milliseconds) (default: `5000` i.e. 5 seconds)
* `PGUSER`: Postgres DB username (default: `postgres`)
* `DISASTER_TYPES`: Disaster type keywords for report classification (default: `flood,prep`)
* `FORMAT_DEFAULT`: Which format to return results in by default (default: `json`)
* `FORMATS`: Formats supported by the system (as comma separated list) (default: `json,xml`)
* `GEO_FORMAT_DEFAULT`: Which format to return geographic results in by default (default: `topojson`)
* `GEO_FORMATS`: Geographic formats supported by the system (as comma separated list) (default: `topojson,geojson,cap`)
* `GEO_PRECISION`: Precision to use when rounding geographic coordinates (default: `10`)
* `IMAGES_BUCKET`: AWS S3 bucket for image uploads (default: `testing-riskmap-image-uploads`)
* `IMAGES_HOST`: Endpoint for image hosting (default: `images.petabencana.id`),
* `INFRASTRUCTURE_TYPES`: Infrastructure types supported (as comma separated list) (default: `floodgates,pumps,waterways`)
* `LANGUAGES`: Supported languages
* `LOG_CONSOLE`: In development mode we log to the console by default, in other environments this must be enabled if required by setting this parameter to `true` (default: `false`)
Expand All @@ -62,8 +69,16 @@ Server configuration parameters are stored in a configuration file which is pars
* `LOG_MAX_FILE_SIZE`: Maximum size of log file in bytes before rotating (default: `1024 * 1024 * 100` i.e. `100mb`)
* `LOG_MAX_FILES`: Maximum number of log files before rotation (default: `10`)
* `NODE_ENV`: Which environment are we in. Environments are: development, test, staging, production (default: `development`)
* `PGHOST`: Postgres DB hostname (default: `127.0.0.1`)
* `PGDATABASE`: Postgres DB database name (default: `cognicity`)
* `PGPASSWORD`: Postgres DB password (default: `p@ssw0rd`)
* `PGPORT`: Postgres DB port (default: `5432`)
* `PGSSL`: SSL enabled on Postgres DB connection? (default: `false`)
* `PGTIMEOUT`: Max duration on DB calls before timeout (in milliseconds) (default: `5000` i.e. 5 seconds)
* `PGUSER`: Postgres DB username (default: `postgres`)
* `PORT`: Which port should the application run on (default: `8001`)
* `REGION_CODES`: Which region codes are supported (as comma separated list) (default: `jbd,bdg,sby`)
* `REPORT_TYPES`: Classifiers for report types (default: `drain,desilting,canalrepair,treeclearing,flood`)
* `RESPONSE_TIME`: Should the server return an `X-Response-Time` header detailing the time taken to process the request. This is useful for both development to identify latency impact on testing and production for performance / health monitoring (default: `false`)
* `SECURE_AUTH0`: Whether Auth0 JWT token security should be applied to secure routes (default: `false`)
* `TABLE_FLOODGAUGE_REPORTS`: Postgres table name for flood-gauge reports
Expand All @@ -86,21 +101,43 @@ A few points to note on config:
Run `npm run -s build` to build.

### Testing
Testing is run by [Travis](https://travis-ci.org/urbanriskmap/cognicity-server). Currently tests only run eslint.
Testing is run by [Travis](https://travis-ci.org/urbanriskmap/cognicity-server). ESLint runs to check syntax. Integration tests, formed by chaining unit tests, are used to check the API. Coverage is provided by Istanbul and [Coveralls](https://coveralls.io/github/urbanriskmap/cognicity-server). See src/test/ for scripts. Beware that integration tests may pollute tables (e.g. user tables in feeds), it is not recommended to run tests against prod databases with live data. The default database (used for testing) is cognicity. Travis-ci creates a new schema instance for testing using https://github.com/urbanriskmap/cognicity-schema, see .travis.yml for more details.

To run tests locally a new database "cognicity_server_testing" is required on localhost.

Code follows the [Google JavaScript style](https://google.github.io/styleguide/jsguide.html).ESLint is run with tests to enfoce style.

### Issue Tracking
Issues are tracked using [GitHub](https://github.com/urbanriskmap/cognicity-server/issues)

### Code documentation

Internal code documentation is in the JSDoc format. Generated documentation is
in the jsdoc/ folder. To build documentation do:
```sh
npm run jsdoc
```

### Package management
Before deployment:
* Use NVM to switch to node and NPM versions specified in package.json for production
* Run npm install
* Run npm shrinkwrap --dev
* Commit changes to npm-shrinkwrap.json

### Release
The release procedure is as follows:
* Update the CHANGELOG.md file with the newly released version, date, and a high-level overview of changes. Commit the change.
* Build code documentation and commit changes.
* Create a tag in git from the current head of master. The tag version should be the same as the version specified in the package.json file - this is the release version.
* Update the version in the package.json file and commit the change.
* Further development is now on the updated version number until the release process begins again.

### API Notes
Full API documentation at https://docs.petabencana.id. This documentation is stored in the [petabencana-docs](https://github.com/urbanriskmap/petabencana-docs) repository.

[swagger-api.json](swagger-api.json) describes the API in [swagger](https://swagger.io/) format (with AWS API Gateway extensions).

- The dbgeo library expects timestamps from database to be in UTC (i.e. not a local timezone)

### License
Expand Down
127 changes: 127 additions & 0 deletions jsdoc/api_index.js.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: api/index.js</title>

<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>

<body>

<div id="main">

<h1 class="page-title">Source: api/index.js</h1>






<section>
<article>
<pre class="prettyprint source linenums"><code>/**
* CogniCity Server Data API
* @module src/api/index
**/
import {Router} from 'express';

// Import the dependencies we need to handle the request
import errorHandler from 'api-error-handler';

// Import validation dependencies
import validate from 'celebrate';

// Get the current version
import {version} from '../../package.json';

// Import our routes
import cards from './routes/cards';
import cities from './routes/cities';
import feeds from './routes/feeds';
import floodgauges from './routes/floodgauges';
import floods from './routes/floods';
import infrastructure from './routes/infrastructure';
import reports from './routes/reports';

/**
* Build CogniCity Server Data API
* @alias module:src/api/index
* @param {Object} config Server configuration
* @param {Object} db PG Promise database instance
* @param {Object} logger Configured Winston logger instance
* @return {Object} api Express router object for API routes
**/
export default ({config, db, logger}) => {
let api = Router(); // eslint-disable-line new-cap

// Return the API version
api.get('/', (req, res) => {
res.status(200).json({version});
});

// Mount the various endpoints
// api.use('/areas', cards({ config, db, logger }));// TODO: local_areas
api.use('/cards', cards({config, db, logger}));
api.use('/cities', cities({config, db, logger}));
api.use('/feeds', feeds({config, db, logger}));
api.use('/floodgauges', floodgauges({config, db, logger}));
api.use('/floods', floods({config, db, logger}));
api.use('/infrastructure', infrastructure({config, db, logger}));
api.use('/reports', reports({config, db, logger}));

// Handle validation errors (wording can be overridden using err.isJoi)
api.use(validate.errors());

// Handle not found errors
api.use((req, res) => {
res.status(404).json({message: 'URL not found', url: req.url});
});

// Handle errors gracefully returning nicely formatted json
api.use(errorHandler());

return api;
};
</code></pre>
</article>
</section>




</div>

<nav>
<h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="db%250ADatabase%2520initializermodule_.html">db
Database initializer</a></li><li><a href="module-lib_cap.html">lib/cap</a></li><li><a href="module-src_api_cards_index.html">src/api/cards/index</a></li><li><a href="module-src_api_cards_model.html">src/api/cards/model</a></li><li><a href="module-src_api_cities_index.html">src/api/cities/index</a></li><li><a href="module-src_api_cities_model.html">src/api/cities/model</a></li><li><a href="module-src_api_feeds_index.html">src/api/feeds/index</a></li><li><a href="module-src_api_feeds_model.html">src/api/feeds/model</a></li><li><a href="module-src_api_floodgauges_index.html">src/api/floodgauges/index</a></li><li><a href="module-src_api_floodgauges_model.html">src/api/floodgauges/model</a></li><li><a href="module-src_api_floods_index.html">src/api/floods/index</a></li><li><a href="module-src_api_floods_model.html">src/api/floods/model</a></li><li><a href="module-src_api_index.html">src/api/index</a></li><li><a href="module-src_api_infrastructure_index.html">src/api/infrastructure/index</a></li><li><a href="module-src_api_infrastructure_model.html">src/api/infrastructure/model</a></li><li><a href="module-src_api_reports_archive_index.html">src/api/reports/archive/index</a></li><li><a href="module-src_api_reports_archive_model.html">src/api/reports/archive/model</a></li><li><a href="module-src_api_reports_index.html">src/api/reports/index</a></li><li><a href="module-src_api_reports_model.html">src/api/reports/model</a></li><li><a href="server%250ACore%2520server%2520modulemodule_.html">server
Core server module</a></li><li><a href="src_test_testCAP%250AA%2520module%2520to%2520test%2520the%2520CAP%2520data%2520format%2520utilitymodule_.html">src/test/testCAP
A module to test the CAP data format utility</a></li><li><a href="src_test_testCards%250AA%2520module%2520to%2520test%2520the%2520_cards%2520endpointmodule_.html">src/test/testCards
A module to test the /cards endpoint</a></li><li><a href="test_testCities%250AA%2520module%2520to%2520test%2520the%2520_cities%2520endpointmodule_.html">test/testCities
A module to test the /cities endpoint</a></li><li><a href="test_testDB%250AA%2520module%2520to%2520test%2520the%2520db%2520utility%2520modulemodule_.html">test/testDB
A module to test the db utility module</a></li><li><a href="test_testFeeds%250AA%2520module%2520to%2520test%2520the%2520_feeds%2520endpointmodule_.html">test/testFeeds
A module to test the /feeds endpoint</a></li><li><a href="test_testFloods%250AA%2520module%2520to%2520test%2520the%2520_floods%2520endpointmodule_.html">test/testFloods
A module to test the /floods endpoint</a></li><li><a href="test_testFloodsgauges%250AA%2520module%2520to%2520test%2520the%2520_floodgauges%2520endpointmodule_.html">test/testFloodsgauges
A module to test the /floodgauges endpoint</a></li><li><a href="test_testInfrastructure%250AA%2520module%2520to%2520test%2520the%2520_infrastructure%2520endpointmodule_.html">test/testInfrastructure
A module to test the /infrastructure endpoint</a></li><li><a href="test_testReports%250AA%2520module%2520to%2520test%2520the%2520_reports%2520endpointmodule_.html">test/testReports
A module to test the /reports endpoint</a></li><li><a href="test_testReportsArchive%250AA%2520module%2520to%2520test%2520the%2520_reports_archive%2520endpointmodule_.html">test/testReportsArchive
A module to test the /reports/archive endpoint</a></li><li><a href="test_testServer%250AA%2520module%2520to%2520test%2520top-level%2520API%2520routes%2520from%2520the%2520servermodule_.html">test/testServer
A module to test top-level API routes from the server</a></li></ul><h3>Classes</h3><ul><li><a href="module-lib_cap.html">lib/cap</a></li></ul>
</nav>

<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Fri Jun 30 2017 11:48:01 GMT-0400 (EDT)
</footer>

<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>
Loading

0 comments on commit 4658a21

Please sign in to comment.