Skip to content

Commit 71d9852

Browse files
authored
Merge pull request #2 from nightscout/dev
Dev
2 parents f801477 + 64c3407 commit 71d9852

272 files changed

Lines changed: 31268 additions & 3835 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.dockerignore

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Don't include the .git history in the Docker image:
2+
.git
3+
4+
# Items from .gitignore
5+
bower_components/
6+
node_modules/
7+
bundle/bundle.out.js
8+
.idea/
9+
*.iml
10+
my.env
11+
*.env
12+
static/bower_components/
13+
.*.sw?
14+
.DS_Store
15+
.vagrant
16+
/iisnode
17+
coverage/
18+
npm-debug.log

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,6 @@ static/bower_components/
2020
coverage/
2121

2222
npm-debug.log
23+
*.heapsnapshot
24+
25+
/tmp

.nvmrc

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

.travis.yml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
language: node_js
2-
sudo: false
2+
sudo: required
33
node_js:
4-
- "0.10"
5-
- "0.12"
6-
services: mongodb
4+
- 6
5+
matrix:
6+
fast_finish: true
7+
services:
8+
- mongodb
9+
- docker
710
script: make travis
11+
after_success:
12+
- nvm version
13+
- if [[ ! -z "$DOCKER_USER" ]]; then docker login -u ${DOCKER_USER} -p ${DOCKER_PASS} && git checkout -- . && git clean -fd . && make docker_release; fi
814
after_script: make report

CONTRIBUTING.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Some simple rules, that will make it easier to maintain our codebase:
5454
* A space before function parameters, such as: `function boom (name, callback) { }`, this makes searching for calls easier
5555
* Name your callback functions, such as `boom('the name', function afterBoom ( result ) { }`
5656
* Don't include author names in the header of your files, if you need to give credit to someone else do it in the commit comment.
57+
* Use single quotes.
5758
* Use the comma first style, for example:
5859

5960
```javascript
@@ -95,10 +96,8 @@ appropriate.
9596

9697
## Co-ordination
9798

98-
There is a google groups nightscout-core developers list where lots of
99-
people discuss Nightscout. Most cgm-remote-monitor hackers use
100-
github's ticketing system, along with Facebook cgm-in-the-cloud, and
101-
gitter system.
99+
Most cgm-remote-monitor hackers use github's ticketing system, along with Facebook cgm-in-the-cloud, and
100+
gitter.
102101

103102
We use git-flow, with `master` as our production, stable branch, and
104103
`dev` is used to queue up for upcoming releases. Everything else is

COPYRIGHT

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
Copyright (C) 2015 The Nightscout Foundation, http://www.nightscoutfoundation.org
1+
2+
We track contributions on a per-patch basis using git.
3+
Please see our published git log:
4+
* https://github.com/nightscout/cgm-remote-monitor/commits/master
5+

Dockerfile.example

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
FROM node:8.1.4
2+
3+
MAINTAINER Nightscout Contributors
4+
5+
RUN apt-get update && \
6+
apt-get -y dist-upgrade
7+
8+
RUN mkdir -p /opt/app
9+
ADD . /opt/app
10+
WORKDIR /opt/app
11+
12+
RUN npm install && \
13+
npm run postinstall && \
14+
npm run env
15+
16+
EXPOSE 1337
17+
18+
CMD ["node", "server.js"]

Makefile

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ ISTANBUL=./node_modules/.bin/istanbul
2424
ANALYZED=./coverage/lcov.info
2525
export CODACY_REPO_TOKEN=e29ae5cf671f4f918912d9864316207c
2626

27+
DOCKER_IMAGE=nightscout/cgm-remote-monitor-travis
28+
2729
all: test
2830

2931
coverage:
@@ -35,9 +37,6 @@ report:
3537
(npm install coveralls && cat ${ANALYZED} | \
3638
./node_modules/.bin/coveralls) || echo "NO COVERAGE"
3739
test -f ${ANALYZED} && \
38-
(npm install codecov.io && cat ${ANALYZED} | \
39-
./node_modules/codecov.io/bin/codecov.io.js) || echo "NO COVERAGE"
40-
test -f ${ANALYZED} && \
4140
(npm install codacy-coverage && cat ${ANALYZED} | \
4241
YOURPACKAGE_COVERAGE=1 ./node_modules/codacy-coverage/bin/codacy-coverage.js) || echo "NO COVERAGE"
4342

@@ -48,4 +47,31 @@ travis:
4847
NODE_ENV=test ${MONGO_SETTINGS} \
4948
${ISTANBUL} cover ${MOCHA} --report lcovonly -- -R tap ${TESTS}
5049

51-
.PHONY: all coverage report test travis
50+
docker_release:
51+
# Get the version from the package.json file
52+
$(eval DOCKER_TAG=$(shell cat package.json | jq '.version' | tr -d '"'))
53+
$(eval NODE_VERSION=$(shell cat .nvmrc))
54+
#
55+
# Create a Dockerfile that contains the correct NodeJS version
56+
cat Dockerfile.example | sed -e "s/^FROM node:.*/FROM node:${NODE_VERSION}/" > Dockerfile
57+
#
58+
# Rebuild the image. We do this with no-cache so that we have all security upgrades,
59+
# since that's more important than fewer layers in the Docker image.
60+
docker build --no-cache=true -t $(DOCKER_IMAGE):$(DOCKER_TAG) .
61+
# Push an image to Docker Hub with the version from package.json:
62+
docker push $(DOCKER_IMAGE):$(DOCKER_TAG)
63+
#
64+
# Push the master branch to Docker hub as 'latest'
65+
if [ "$(TRAVIS_BRANCH)" = "master" ]; then \
66+
docker tag $(DOCKER_IMAGE):$(DOCKER_TAG) $(DOCKER_IMAGE):latest && \
67+
docker push $(DOCKER_IMAGE):latest; \
68+
fi
69+
#
70+
# Push the dev branch to Docker Hub as 'latest_dev'
71+
if [ "$(TRAVIS_BRANCH)" = "dev" ]; then \
72+
docker tag $(DOCKER_IMAGE):$(DOCKER_TAG) $(DOCKER_IMAGE):latest_dev && \
73+
docker push $(DOCKER_IMAGE):latest_dev; \
74+
fi
75+
rm -f Dockerfile
76+
77+
.PHONY: all coverage docker_release report test travis

README.md

Lines changed: 72 additions & 33 deletions
Large diffs are not rendered by default.

app.js

Lines changed: 149 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,166 @@
1+
'use strict';
12

3+
var _ = require('lodash');
24
var express = require('express');
35
var compression = require('compression');
46
var bodyParser = require('body-parser');
5-
function create (env, ctx) {
6-
///////////////////////////////////////////////////
7-
// api and json object variables
8-
///////////////////////////////////////////////////
9-
var api = require('./lib/api/')(env, ctx);
7+
var prettyjson = require('prettyjson');
108

11-
var app = express();
12-
var appInfo = env.name + ' ' + env.version;
13-
app.set('title', appInfo);
14-
app.enable('trust proxy'); // Allows req.secure test on heroku https connections.
159

16-
app.use(compression({filter: function shouldCompress(req, res) {
17-
//TODO: return false here if we find a condition where we don't want to compress
18-
// fallback to standard filter function
19-
return compression.filter(req, res);
20-
}}));
21-
// app.use(bodyParser({limit: 1048576 * 50, extended: true }));
10+
function create(env, ctx) {
11+
var app = express();
12+
var appInfo = env.name + ' ' + env.version;
13+
app.set('title', appInfo);
14+
app.enable('trust proxy'); // Allows req.secure test on heroku https connections.
2215

23-
//if (env.api_secret) {
24-
// console.log("API_SECRET", env.api_secret);
25-
//}
26-
app.use('/api/v1', bodyParser({limit: 1048576 * 50 }), api);
16+
if (ctx.bootErrors && ctx.bootErrors.length > 0) {
17+
app.get('*', require('./lib/booterror')(ctx));
18+
return app;
19+
}
2720

21+
if (env.settings.isEnabled('cors')) {
22+
var allowOrigin = _.get(env, 'extendedSettings.cors.allowOrigin') || '*';
23+
console.info('Enabled CORS, allow-origin:', allowOrigin);
24+
app.use(function allowCrossDomain(req, res, next) {
25+
res.header('Access-Control-Allow-Origin', allowOrigin);
26+
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
27+
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
2828

29-
// pebble data
30-
app.get('/pebble', ctx.pebble);
29+
// intercept OPTIONS method
30+
if ('OPTIONS' === req.method) {
31+
res.send(200);
32+
} else {
33+
next();
34+
}
35+
});
36+
}
3137

32-
// expose swagger.yaml
33-
app.get('/swagger.yaml', function (req, res) {
34-
res.sendFile(__dirname + '/swagger.yaml');
35-
});
38+
///////////////////////////////////////////////////
39+
// api and json object variables
40+
///////////////////////////////////////////////////
41+
var api = require('./lib/api/')(env, ctx);
42+
var ddata = require('./lib/data/endpoints')(env, ctx);
3643

37-
//app.get('/package.json', software);
44+
app.use(compression({
45+
filter: function shouldCompress(req, res) {
46+
//TODO: return false here if we find a condition where we don't want to compress
47+
// fallback to standard filter function
48+
return compression.filter(req, res);
49+
}
50+
}));
51+
// app.use(bodyParser({limit: 1048576 * 50, extended: true }));
3852

39-
// define static server
40-
//TODO: JC - changed cache to 1 hour from 30d ays to bypass cache hell until we have a real solution
41-
var staticFiles = express.static(env.static_files, {maxAge: 60 * 60 * 1000});
53+
//if (env.api_secret) {
54+
// console.log("API_SECRET", env.api_secret);
55+
//}
56+
app.use('/api/v1', bodyParser({
57+
limit: 1048576 * 50
58+
}), api);
4259

43-
// serve the static content
44-
app.use(staticFiles);
60+
app.use('/api/v2/properties', ctx.properties);
61+
app.use('/api/v2/authorization', ctx.authorization.endpoints);
62+
app.use('/api/v2/ddata', ddata);
4563

46-
var bundle = require('./bundle')();
47-
app.use(bundle);
64+
// pebble data
65+
app.get('/pebble', ctx.pebble);
4866

49-
// Handle errors with express's errorhandler, to display more readable error messages.
50-
var errorhandler = require('errorhandler');
51-
//if (process.env.NODE_ENV === 'development') {
67+
// expose swagger.yaml
68+
app.get('/swagger.yaml', function(req, res) {
69+
res.sendFile(__dirname + '/swagger.yaml');
70+
});
71+
72+
if (env.settings.isEnabled('dumps')) {
73+
var heapdump = require('heapdump');
74+
app.get('/api/v2/dumps/start', function(req, res) {
75+
var path = new Date().toISOString() + '.heapsnapshot';
76+
path = path.replace(/:/g, '-');
77+
console.info('writing dump to', path);
78+
heapdump.writeSnapshot(path);
79+
res.send('wrote dump to ' + path);
80+
});
81+
}
82+
83+
84+
//app.get('/package.json', software);
85+
86+
// Allow static resources to be cached for week
87+
var maxAge = 7 * 24 * 60 * 60 * 1000;
88+
89+
if (process.env.NODE_ENV === 'development') {
90+
maxAge = 10;
91+
console.log('Development environment detected, setting static file cache age to 10 seconds');
92+
93+
app.get('/nightscout.appcache', function(req, res) {
94+
res.sendStatus(404);
95+
});
96+
}
97+
98+
//TODO: JC - changed cache to 1 hour from 30d ays to bypass cache hell until we have a real solution
99+
var staticFiles = express.static(env.static_files, {
100+
maxAge: maxAge
101+
});
102+
103+
// serve the static content
104+
app.use(staticFiles);
105+
106+
var tmpFiles = express.static('tmp', {
107+
maxAge: maxAge
108+
});
109+
110+
// serve the static content
111+
app.use(tmpFiles);
112+
113+
if (process.env.NODE_ENV !== 'development') {
114+
115+
console.log('Production environment detected, enabling Minify');
116+
117+
var minify = require('express-minify');
118+
var myUglifyJS = require('uglify-js');
119+
var myCssmin = require('cssmin');
120+
121+
app.use(minify({
122+
js_match: /\.js/,
123+
css_match: /\.css/,
124+
sass_match: /scss/,
125+
less_match: /less/,
126+
stylus_match: /stylus/,
127+
coffee_match: /coffeescript/,
128+
json_match: /json/,
129+
uglifyJS: myUglifyJS,
130+
cssmin: myCssmin,
131+
cache: __dirname + '/cache',
132+
onerror: undefined,
133+
}));
134+
135+
}
136+
137+
// if this is dev environment, package scripts on the fly
138+
// if production, rely on postinstall script to run packaging for us
139+
140+
if (process.env.NODE_ENV === 'development') {
141+
142+
var webpack = require("webpack");
143+
var webpack_conf = require('./webpack.config');
144+
145+
webpack(webpack_conf, function(err, stats) {
146+
147+
var json = stats.toJson() // => webpack --json
148+
149+
var options = {
150+
noColor: true
151+
};
152+
153+
console.log(prettyjson.render(json.errors, options));
154+
console.log(prettyjson.render(json.assets, options));
155+
156+
});
157+
}
158+
159+
// Handle errors with express's errorhandler, to display more readable error messages.
160+
var errorhandler = require('errorhandler');
161+
//if (process.env.NODE_ENV === 'development') {
52162
app.use(errorhandler());
53-
//}
54-
return app;
163+
//}
164+
return app;
55165
}
56-
module.exports = create;
57-
166+
module.exports = create;

0 commit comments

Comments
 (0)