Skip to content

Commit

Permalink
moving around stuff, huge README update
Browse files Browse the repository at this point in the history
  • Loading branch information
ubergesundheit committed Sep 6, 2017
1 parent 500a70a commit bd54804
Show file tree
Hide file tree
Showing 114 changed files with 4,971 additions and 1,986 deletions.
3 changes: 3 additions & 0 deletions .scripts/npm_start.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

require('@sensebox/opensensemap-api');
23 changes: 23 additions & 0 deletions .scripts/npm_tag-container.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash

set -euo pipefail
IFS=$'\n\t'

currentBranch=$(git branch | grep -e "^*" | cut -d' ' -f 2)

if [ "$currentBranch" != "master" ]; then
echo "Tags for containers can only be made from 'master' branch"
exit 1
fi

# Extract latest git tag
currentVersion=$(git tag -l --sort=-v:refname | grep dockerhub-v | head -n 1 | tr -d 'dockerhub\-v')

# Increment
newVersion=$(($currentVersion+1))

# tag new version
git tag "dockerhub-v$newVersion"

echo "Creation of new tag for docker hub was successful. New tag is 'dockerhub-v$newVersion'."
echo "Please run 'git push origin master' to trigger the build on docker hub."
28 changes: 28 additions & 0 deletions .scripts/npm_version-update_changelog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

/*
NPM package.json version script. Do not run manually!
Only tested on Linux. Node v6.
This script overwrites the first Markdown headline "## Unreleased" with the
version in the package.json and prepends a new "## Unreleased" headline.
*/

const { version } = require('../package.json'),
fs = require('fs');

const filename = 'CHANGELOG.md';

fs.readFile(filename, 'utf-8', function(err, data) {
if (err) return console.log(err);
const result = data.replace(
/## Unreleased/i,
`## Unreleased\n\n## v${version}`
);

fs.writeFile(filename, result, 'utf8', function(err) {
if (err) return console.log(err);
});
});
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ node_js:
- "6"

install: true
script: ./utils/deploy-docs.sh
script: ./api/.scripts/deploy-docs.sh

env:
global:
Expand Down
9 changes: 6 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
FROM node:6-alpine

ENV NODE_ENV=production

RUN apk --no-cache --virtual .build add python make g++ git

# taken from node:6-onbuild
Expand All @@ -8,15 +10,16 @@ WORKDIR /usr/src/app

COPY package.json /usr/src/app/
COPY yarn.lock /usr/src/app/
RUN yarn install --pure-lockfile
RUN yarn install --pure-lockfile --production
# required because the prebuilt binaries are not compatible with musl
# remove when https://github.com/kelektiv/node.bcrypt.js/issues/528 is resolved
RUN npm rebuild bcrypt --build-from-source
COPY . /usr/src/app
COPY .scripts /usr/src/app/

COPY .git /usr/src/app/
# for git 2.1.4
RUN echo -n $(git rev-parse --abbrev-ref HEAD) $(TZ=UTC git log --date=local --pretty=format:"%ct %h" -n 1) > revision; rm -rf .git

RUN apk del .build

CMD [ "npm", "start" ]
CMD [ "yarn", "start" ]
152 changes: 88 additions & 64 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,72 +1,96 @@
openSenseMap-API
================

[![Join the chat at https://gitter.im/sensebox/openSenseMap-API](https://badges.gitter.im/sensebox/openSenseMap-API.svg)](https://gitter.im/sensebox/openSenseMap-API?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
This is the back-end for [openSenseMap](https://opensensemap.org).

openSenseMap is part of the [senseBox](https://sensebox.de) project.
To get more information about openSenseMap and senseBox visit the before mentioned links or have a look at this [video](https://www.youtube.com/watch?v=uTOWYa42_rI).

Originally, this API has been built as part the bachelor thesis of @mpfeil at the ifgi (Institute for Geoinformatics, WWU Münster).

### Technologies

* [node.js]
* [MongoDB]

### Development

#### Node.js
It is assumed that you have installed node.js Version 6 LTS. Install dependencies with [yarn](https://yarnpkg.com/)
```
yarn install
```

There are several config keys in `config/index.js`. These can also be configured through environment keys. Just prefix your environment keys with `OSEM_` to be read by the api.

#### Database
The API needs a running MongoDB instance with a `OSeM-api` database and credentials.

The best way to run the database for development is to use the supplied `docker-compose.yml` and run
```
docker-compose up -d db
```

### `require()` openSenseMap-API for other projects

You can require several parts of the API in other projects.

Install it as dependency
```
# for specific branch/commit/tag append #<branch/commit/tag>
yarn add git://github.com/sensebox/openSenseMap-API.git
```

This allows you to use parts like models and decoding in your own project. See `index.js`.

### Create the JSDoc pages

To create the documentation you need [apidocjs](http://apidocjs.com/) and run:
```
apidoc -e node_modules/
```

### Running Tests
You can run the tests in containers using docker and docker-compose.
# openSenseMap API

[openSenseMap] is part of the [senseBox] project. To get more information about openSenseMap and senseBox visit the before mentioned links or have a look at this [video](https://www.youtube.com/watch?v=uTOWYa42_rI), read the [API docs](https://docs.opensensemap.org) or the [openSenseMap](https://osem.books.sensebox.de/) chapter in our [books](https://books.sensebox.de/).

## Organization

This repository is a monorepo for the packages

- [![@sensebox/opensensemap-api-models NPM Version](https://img.shields.io/npm/v/@sensebox/opensensemap-api-models.svg)](https://www.npmjs.com/package/@sensebox/opensensemap-api-models) [@sensebox/opensensemap-api-models](models) The data models and database connection of openSenseMap.
- [![@sensebox/opensensemap-api NPM Version](https://img.shields.io/npm/v/@sensebox/opensensemap-api.svg)](https://www.npmjs.com/package/@sensebox/opensensemap-api) [@sensebox/opensensemap-api](api) The REST API used by [https://opensensemap.org](https://opensensemap.org) running at [https://api.opensensemap.org](https://api.opensensemap.org).

### Directories
- root directory
- not published on NPM
- is used to build a Docker container image
- contains tests (run with `./run-tests`)
- [api](api) directory
- NPM [@sensebox/opensensemap-api](https://www.npmjs.com/package/@sensebox/opensensemap-api)
- [Changelog](api/CHANGELOG.md)
- should not appear as dependency of other packages
- [models](models) directory
- NPM [@sensebox/opensensemap-api-models](https://www.npmjs.com/package/@sensebox/opensensemap-api-models)
- [Changelog](models/CHANGELOG.md)
- `require('@sensebox/opensensemap-api-models')` for access to data models

### Branches
- master (runs in production)
- Is never used to publish versions of subpackages
- [package.json](package.json) contains specific versions from NPM
- Is used for container build tags
- development (runs on testing server)
- Releases for subpackages are done from here
- [package.json](package.json) contains `file:./api` and `file:./models` as versions
- gh-pages
- Hosts API docs for [https://docs.opensensemap.org/](https://docs.opensensemap.org/)
- Is generated and pushed to github by Travis CI [![sensebox/openSenseMap-API Build Status](https://travis-ci.org/sensebox/openSenseMap-API.svg?branch=master)](https://travis-ci.org/sensebox/openSenseMap-API)

### Tags and Versions
Git Tags are used for:
- Docker hub builds `dockerhub-v1`. Version number is increased by one for each new version.
- NPM tags for subpackages. [Uses Semantic Versioning 2.0.0](http://semver.org/spec/v2.0.0.html)
- `@sensebox/opensensemap-api-v1.0.0`
- `@sensebox/opensensemap-api-models-v1.0.0`

### Release new NPM versions
Make sure you've documented your changes in the `CHANGELOG.md` file of the respective package and have comitted everything to the development branch.

To create a new NPM version, use `npm version`.
1. Have everything commited for the release
1. Document your changes in `CHANGELOG.md` under `## Unreleased`. Do not `git add` the file
1. Run `npm version [ major | minor | patch ] -m "[<PACKAGENAME>-v%s] <Your commit message>"`
1. `git push origin development`
1. `npm publish`

### Docker container images
#### Development images
Every commit on branch `development` will be built with the tag `development`. Versions of `@sensebox/opensensemap-api` and `@sensebox/opensensemap-api-models` will be the current development snapshot.

#### Versioned container images
- Check out `master` branch
- Go to root directory
- Modify version of `@sensebox/opensensemap-api` and `@sensebox/opensensemap-api-models` to desired versions
- Make sure versions of `@sensebox/opensensemap-api` and `@sensebox/opensensemap-api-models` are published on NPM
- Optional
- Run tests
- Build docker image locally
- Run `yarn` to update `yarn.lock`
- Commit `package.json` and `yarn.lock`
- Run `npm run tag-container`
- Run `git push origin master`
- Docker images are built automatically by the Docker hub for all tags starting with `dockerhub-v`

## Development
- Have [Node.js] v6, [yarn](https://yarnpkg.com/), [Docker](https://docs.docker.com/engine/installation/) and [docker-compose](https://docs.docker.com/compose/install/) installed
- Go to the subdirectory in which you want to develop
- Only for [api](api): Run `yarn install`
- Start your development database (`docker-compose up -d db`)

## Running Tests
You can run the tests in containers using Docker and docker-compose.
```
# Only run this once or every time you change dependencies in package.json
docker-compose -p osemapitest -f tests-docker-compose.yml build osem-api
./run-tests.sh build
./run-tests.sh
```

### Running in Production

Look at the [OSeM-compose](https://github.com/sensebox/osem-compose) repository. It contains a deployment with docker compose.

### License
## Running the API
### Configuration
These packages read their configuration either from their respective `src/config.js` file or environment variables starting with `OSEM_`. Environment variables override values in `src/config.js`.

[MIT](license.md) - Matthias Pfeil 2015 - 2017
### Container based
The repository [OSeM-compose](https://github.com/sensebox/osem-compose) contains a deployment of all services with docker compose. The image built from this repository is called [`sensebox/opensensemap-api`](https://hub.docker.com/r/sensebox/opensensemap-api/).

[node.js]:http://nodejs.org/
[MongoDB]:http://www.mongodb.com/
[openSenseMap]:https://opensensemap.org/
[senseBox]:https://sensebox.de/
3 changes: 2 additions & 1 deletion .eslintrc.js → api/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ module.exports = {
'error',
'never'
],
'no-warning-comments': 'error'
'no-warning-comments': 'error',
'prefer-promise-reject-errors': 'error'
}
};
10 changes: 3 additions & 7 deletions utils/deploy-docs.sh → api/.scripts/deploy-docs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@
set -euo pipefail
IFS=$'\n\t'


# move to the build dir..
cd $TRAVIS_BUILD_DIR
cd "$TRAVIS_BUILD_DIR"

# install apidocs
npm install -g [email protected]

# deploy!
npm install -g [email protected]

# run apidoc
apidoc -i . -f js -e node_modules
Expand Down Expand Up @@ -42,6 +39,5 @@ git config user.email "[email protected]"
# commit
git commit -m "apidoc build from ${TRAVIS_COMMIT} by Travis"

# push to github!
# push to github
git push "https://${GITHUB_TOKEN}@github.com/${GITHUB_REPO}.git" gh-pages

4 changes: 4 additions & 0 deletions api/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# @sensebox/opensensemap-api Changelog

## Unreleased
- Initial Release after splitting api and models
33 changes: 33 additions & 0 deletions api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# @sensebox/opensensemap-api

This package contains the HTTP REST API for the openSenseMap. Originally, this API has been built as part the bachelor thesis of [@mpfeil](https://github.com/mpfeil) at the ifgi (Institute for Geoinformatics, WWU Münster).

The package should never be required by any other package. If you want to use the openSenseMap models, look at [@sensebox/opensensemap-api-models](../models).

## Development

- Install Docker, Node.js v6, yarn
- Run `yarn` in this directory
- Run development database `docker-compose up -d db`

Also see [README](../README.md)


## Create the API documentation pages

To create the documentation you need [apidocjs](http://apidocjs.com/) and run:
```
apidoc -e node_modules/
```

## Technologies

* [Node.js]
* [MongoDB]

## License

[MIT](license.md) - Matthias Pfeil 2015 - 2017

[Node.js]:http://nodejs.org/
[MongoDB]:http://www.mongodb.com/
23 changes: 12 additions & 11 deletions app.js → api/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,24 @@
* Booya!
*/

/**
* define for Datetype parameters
* @apiDefine ISO8601Date A ISO8601 formatted timestamp. Will be parsed by MomentJS with enabled strict mode
*/

'use strict';

const restify = require('restify'),
{ fullResponse, queryParser, jsonBodyParser, pre: { sanitizePath } } = restify.plugins,
utils = require('./lib/utils'),
db = require('./lib/db'),
{ preRequest, preCors } = require('./lib/helpers/apiUtils'),
Box = require('./lib/models').Box,
routes = require('./lib/routes'),
config = require('./src/config'),
{ preRequest, preCors, Honeybadger, softwareRevision, postToSlack } = require('./src/helpers/apiUtils'),
routes = require('./src/routes'),
passport = require('passport'),
log = require('./lib/log');

const { config, Honeybadger } = utils;
log = require('./src/log'),
{ db } = require('@sensebox/opensensemap-api-models');

const server = restify.createServer({
name: `opensensemap-api (${utils.softwareRevision})`,
name: `opensensemap-api (${softwareRevision})`,
log: log
});

Expand All @@ -45,8 +47,7 @@ db.connect()
.then(function () {
server.listen(config.port, function () {
log.info(`${server.name} listening at ${server.url}`);
utils.postToSlack(`openSenseMap API started. Revision: ${utils.softwareRevision}`);
Box.connectMQTTBoxes();
postToSlack(`openSenseMap API started. Revision: ${softwareRevision}`);
});
})
.catch(function (err) {
Expand Down
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit bd54804

Please sign in to comment.