1+ 'use strict' ;
12
3+ var _ = require ( 'lodash' ) ;
24var express = require ( 'express' ) ;
35var compression = require ( 'compression' ) ;
46var 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 : / \. j s / ,
123+ css_match : / \. c s s / ,
124+ sass_match : / s c s s / ,
125+ less_match : / l e s s / ,
126+ stylus_match : / s t y l u s / ,
127+ coffee_match : / c o f f e e s c r i p t / ,
128+ json_match : / j s o n / ,
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